Sync foto: generate embedding dan simpan ke student_faces, opsi no-embedding, update docs
This commit is contained in:
@@ -2,15 +2,19 @@
|
||||
<?php
|
||||
/**
|
||||
* Sync foto wajah dari folder (hasil download Google Drive) ke writable/faces/{student_id}.jpg
|
||||
* Pemakaian: php sync_face_photos.php --source=C:\path\to\folder
|
||||
* dan generate embedding (vektor) lalu simpan ke student_faces — alur proper untuk verifikasi wajah.
|
||||
*
|
||||
* Pemakaian: php sync_face_photos.php --source=C:\path\to\folder [--no-embedding]
|
||||
* Konvensi: nama file mengandung NISN (contoh: 1234567890.jpg atau 01. 1234567890.jpg)
|
||||
* Lihat: backend/docs/SYNC_FOTO_WAJAH.md
|
||||
*/
|
||||
|
||||
$options = getopt('', ['source:']);
|
||||
$options = getopt('', ['source:', 'no-embedding']);
|
||||
$source = $options['source'] ?? null;
|
||||
$noEmbedding = isset($options['no-embedding']);
|
||||
if (empty($source) || ! is_dir($source)) {
|
||||
echo "Pemakaian: php sync_face_photos.php --source=/path/to/folder\n";
|
||||
echo "Pemakaian: php sync_face_photos.php --source=/path/to/folder [--no-embedding]\n";
|
||||
echo " --no-embedding Skip generate vektor (hanya copy file + face_hash).\n";
|
||||
echo "Folder harus berisi subfolder (XII-1, XII-2, dll) dengan file gambar bernama NISN.jpg\n";
|
||||
exit(1);
|
||||
}
|
||||
@@ -62,10 +66,75 @@ if (! is_dir($facesDir)) {
|
||||
echo "Folder dibuat: {$facesDir}\n";
|
||||
}
|
||||
|
||||
$faceServiceUrl = rtrim($env['FACE_SERVICE_URL'] ?? $env['face_service_url'] ?? 'http://localhost:5000', '/');
|
||||
$embeddingDim = (int) ($env['FACE_EMBEDDING_DIM'] ?? $env['face_embedding_dim'] ?? 512);
|
||||
$minFaceSize = (float) ($env['FACE_MIN_SIZE'] ?? $env['face_min_size'] ?? 80);
|
||||
$minBlur = (float) ($env['FACE_MIN_BLUR'] ?? $env['face_min_blur'] ?? 30);
|
||||
$minBrightness = (float) ($env['FACE_MIN_BRIGHTNESS'] ?? $env['face_min_brightness'] ?? 0.2);
|
||||
|
||||
/**
|
||||
* Generate embedding dari file gambar lalu simpan ke student_faces (source=formal).
|
||||
* Menggantikan embedding formal yang sudah ada untuk student_id ini.
|
||||
* Return: 'ok' | 'skip' | 'fail'
|
||||
*/
|
||||
$generateAndSaveEmbedding = function (PDO $pdo, string $destPath, int $studentId) use ($faceServiceUrl, $embeddingDim, $minFaceSize, $minBlur, $minBrightness): string {
|
||||
if (! is_file($destPath)) {
|
||||
return 'fail';
|
||||
}
|
||||
$mime = mime_content_type($destPath) ?: 'image/jpeg';
|
||||
$ch = curl_init($faceServiceUrl . '/embed');
|
||||
if ($ch === false) {
|
||||
return 'fail';
|
||||
}
|
||||
$cfile = new CURLFile($destPath, $mime, basename($destPath));
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_POST => true,
|
||||
CURLOPT_POSTFIELDS => ['image' => $cfile],
|
||||
CURLOPT_RETURNTRANSFER => true,
|
||||
CURLOPT_TIMEOUT => 30,
|
||||
CURLOPT_HTTPHEADER => [],
|
||||
]);
|
||||
$response = curl_exec($ch);
|
||||
$status = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE);
|
||||
curl_close($ch);
|
||||
|
||||
if ($response === false || $status !== 200) {
|
||||
return 'fail';
|
||||
}
|
||||
$data = json_decode($response, true);
|
||||
if (! is_array($data) || empty($data['embedding']) || ! is_array($data['embedding'])) {
|
||||
return 'fail';
|
||||
}
|
||||
$embedding = $data['embedding'];
|
||||
if (count($embedding) !== $embeddingDim) {
|
||||
return 'skip';
|
||||
}
|
||||
$facesCount = (int) ($data['faces_count'] ?? 0);
|
||||
$faceSize = (float) ($data['face_size'] ?? 0);
|
||||
$blur = (float) ($data['blur'] ?? 0);
|
||||
$brightness = (float) ($data['brightness'] ?? 0);
|
||||
if ($facesCount !== 1) {
|
||||
return 'skip';
|
||||
}
|
||||
if ($faceSize < $minFaceSize || $blur < $minBlur || $brightness < $minBrightness) {
|
||||
return 'skip';
|
||||
}
|
||||
$qualityScore = isset($data['quality_score']) ? (float) $data['quality_score'] : null;
|
||||
$embeddingJson = json_encode(array_map('floatval', $embedding));
|
||||
|
||||
$pdo->prepare('DELETE FROM student_faces WHERE student_id = ? AND source = ?')->execute([$studentId, 'formal']);
|
||||
$ins = $pdo->prepare('INSERT INTO student_faces (student_id, embedding, source, quality_score, created_at, updated_at) VALUES (?, ?, ?, ?, NOW(), NOW())');
|
||||
$ins->execute([$studentId, $embeddingJson, 'formal', $qualityScore]);
|
||||
return 'ok';
|
||||
};
|
||||
|
||||
$extensions = ['jpg', 'jpeg', 'png'];
|
||||
$countOk = 0;
|
||||
$countSkip = 0;
|
||||
$countFail = 0;
|
||||
$countEmbedOk = 0;
|
||||
$countEmbedSkip = 0;
|
||||
$countEmbedFail = 0;
|
||||
|
||||
$it = new RecursiveIteratorIterator(
|
||||
new RecursiveDirectoryIterator($source, RecursiveDirectoryIterator::SKIP_DOTS),
|
||||
@@ -125,8 +194,15 @@ foreach ($it as $file) {
|
||||
$hash = md5_file($dest);
|
||||
$update = $pdo->prepare('UPDATE students SET face_hash = ? WHERE id = ?');
|
||||
$update->execute([$hash, $studentId]);
|
||||
echo " [ok-copy] {$nisn} -> student_id {$studentId}\n";
|
||||
echo " [ok-copy] {$nisn} -> student_id {$studentId}";
|
||||
$countOk++;
|
||||
if (! $noEmbedding) {
|
||||
$emb = $generateAndSaveEmbedding($pdo, $dest, $studentId);
|
||||
if ($emb === 'ok') { $countEmbedOk++; echo ' + embedding'; }
|
||||
elseif ($emb === 'skip') { $countEmbedSkip++; echo ' (embed skip)'; }
|
||||
else { $countEmbedFail++; echo ' (embed fail)'; }
|
||||
}
|
||||
echo "\n";
|
||||
} else {
|
||||
echo " [fail] Gagal copy (GD error): {$path} -> {$dest}\n";
|
||||
$countFail++;
|
||||
@@ -155,8 +231,15 @@ foreach ($it as $file) {
|
||||
$hash = md5_file($dest);
|
||||
$update = $pdo->prepare('UPDATE students SET face_hash = ? WHERE id = ?');
|
||||
$update->execute([$hash, $studentId]);
|
||||
echo " [ok] {$nisn} -> student_id {$studentId}\n";
|
||||
echo " [ok] {$nisn} -> student_id {$studentId}";
|
||||
$countOk++;
|
||||
if (! $noEmbedding) {
|
||||
$emb = $generateAndSaveEmbedding($pdo, $dest, $studentId);
|
||||
if ($emb === 'ok') { $countEmbedOk++; echo ' + embedding'; }
|
||||
elseif ($emb === 'skip') { $countEmbedSkip++; echo ' (embed skip)'; }
|
||||
else { $countEmbedFail++; echo ' (embed fail)'; }
|
||||
}
|
||||
echo "\n";
|
||||
} else {
|
||||
imagedestroy($srcImage);
|
||||
imagedestroy($dstImage);
|
||||
@@ -166,3 +249,6 @@ foreach ($it as $file) {
|
||||
}
|
||||
|
||||
echo "\nSelesai. OK: {$countOk}, Skip (NISN tidak ada): {$countSkip}, Gagal: {$countFail}\n";
|
||||
if (! $noEmbedding) {
|
||||
echo "Embedding: OK: {$countEmbedOk}, Skip (kualitas/tidak 1 wajah): {$countEmbedSkip}, Gagal: {$countEmbedFail}\n";
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user