Upload api.py
Browse files
api.py
CHANGED
|
@@ -246,24 +246,59 @@ def process_video_job(job_id: str):
|
|
| 246 |
})
|
| 247 |
saved_count += 1
|
| 248 |
else:
|
| 249 |
-
# DeepFace fallback
|
| 250 |
if DeepFace is None:
|
| 251 |
pass
|
| 252 |
else:
|
| 253 |
try:
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 267 |
except Exception as _e_df:
|
| 268 |
print(f"[{job_id}] DeepFace fallback error: {_e_df}")
|
| 269 |
frame_idx += 1
|
|
|
|
| 246 |
})
|
| 247 |
saved_count += 1
|
| 248 |
else:
|
| 249 |
+
# DeepFace fallback con detecci贸n de bounding boxes v铆a Haar Cascade (OpenCV)
|
| 250 |
if DeepFace is None:
|
| 251 |
pass
|
| 252 |
else:
|
| 253 |
try:
|
| 254 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 255 |
+
try:
|
| 256 |
+
haar_path = getattr(cv2.data, 'haarcascades', None) or ''
|
| 257 |
+
face_cascade = cv2.CascadeClassifier(os.path.join(haar_path, 'haarcascade_frontalface_default.xml'))
|
| 258 |
+
except Exception:
|
| 259 |
+
face_cascade = None
|
| 260 |
+
boxes_haar = []
|
| 261 |
+
if face_cascade is not None and not face_cascade.empty():
|
| 262 |
+
faces_haar = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(40, 40))
|
| 263 |
+
for (x, y, w, h) in faces_haar:
|
| 264 |
+
top, left, bottom, right = max(0, y), max(0, x), min(frame.shape[0], y+h), min(frame.shape[1], x+w)
|
| 265 |
+
boxes_haar.append((top, right, bottom, left))
|
| 266 |
+
if boxes_haar:
|
| 267 |
+
for (top, right, bottom, left) in boxes_haar:
|
| 268 |
+
crop = frame[top:bottom, left:right]
|
| 269 |
+
if crop.size == 0:
|
| 270 |
+
continue
|
| 271 |
+
fn = f"face_{frame_idx:06d}_{saved_count:03d}.jpg"
|
| 272 |
+
crop_path = faces_root / fn
|
| 273 |
+
cv2.imwrite(str(crop_path), crop)
|
| 274 |
+
reps = DeepFace.represent(img_path=str(crop_path), model_name="Facenet512", enforce_detection=False)
|
| 275 |
+
for r in (reps or []):
|
| 276 |
+
emb = r.get("embedding") if isinstance(r, dict) else r
|
| 277 |
+
if emb is None:
|
| 278 |
+
continue
|
| 279 |
+
emb = np.array(emb, dtype=float)
|
| 280 |
+
emb = emb / (np.linalg.norm(emb) + 1e-9)
|
| 281 |
+
embeddings.append(emb.astype(float).tolist())
|
| 282 |
+
crops_meta.append({
|
| 283 |
+
"file": fn,
|
| 284 |
+
"frame": frame_idx,
|
| 285 |
+
"box": [int(top), int(right), int(bottom), int(left)],
|
| 286 |
+
})
|
| 287 |
+
saved_count += 1
|
| 288 |
+
else:
|
| 289 |
+
# Fallback m铆nimo: emb del frame completo
|
| 290 |
+
tmp_path = faces_root / f"frame_{frame_idx:06d}.jpg"
|
| 291 |
+
cv2.imwrite(str(tmp_path), frame)
|
| 292 |
+
reps = DeepFace.represent(img_path=str(tmp_path), model_name="Facenet512", enforce_detection=False)
|
| 293 |
+
for r in (reps or []):
|
| 294 |
+
emb = r.get("embedding") if isinstance(r, dict) else r
|
| 295 |
+
if emb is None:
|
| 296 |
+
continue
|
| 297 |
+
emb = np.array(emb, dtype=float)
|
| 298 |
+
emb = emb / (np.linalg.norm(emb) + 1e-9)
|
| 299 |
+
embeddings.append(emb.astype(float).tolist())
|
| 300 |
+
crops_meta.append({"file": tmp_path.name, "frame": frame_idx, "box": None})
|
| 301 |
+
saved_count += 1
|
| 302 |
except Exception as _e_df:
|
| 303 |
print(f"[{job_id}] DeepFace fallback error: {_e_df}")
|
| 304 |
frame_idx += 1
|