|
from deepface import DeepFace |
|
import cv2 |
|
import os |
|
import tempfile |
|
|
|
|
|
def extract_frames(video_path, interval=30): |
|
""" |
|
Extracts frames from a video at a specified interval. |
|
""" |
|
|
|
cap = cv2.VideoCapture(video_path) |
|
frames = [] |
|
count = 0 |
|
|
|
|
|
temp_dir = tempfile.gettempdir() |
|
|
|
|
|
while cap.isOpened(): |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
|
|
if count % interval == 0: |
|
frame_path = os.path.join(temp_dir, f"frame_{count}.jpg") |
|
|
|
success = cv2.imwrite(frame_path, frame) |
|
if success: |
|
frames.append(frame_path) |
|
count += 1 |
|
|
|
|
|
cap.release() |
|
|
|
return frames |
|
|
|
|
|
def verify_faces_in_video(video_path, reference_img, interval=30, threshold=0.7): |
|
""" |
|
Verifies if faces in a video match a reference image. |
|
""" |
|
results = [] |
|
|
|
frames = extract_frames(video_path, interval) |
|
|
|
|
|
for frame_path in frames: |
|
|
|
try: |
|
|
|
result = DeepFace.verify( |
|
img1_path=reference_img, img2_path=frame_path, enforce_detection=False |
|
) |
|
score = result["distance"] |
|
verified = result["verified"] |
|
results.append( |
|
{"frame": frame_path, "distance": round(score, 4), "verified": verified} |
|
) |
|
except Exception as e: |
|
results.append({"frame": frame_path, "error": str(e)}) |
|
return results |
|
|