video_path = os.path.join(UPLOAD_FOLDER, video.filename)
photo_path = os.path.join(UPLOAD_FOLDER, photo.filename)
with open(video_path, "wb") as buffer:
shutil.copyfileobj(video.file, buffer)
with open(photo_path, "wb") as buffer:
shutil.copyfileobj(photo.file, buffer)
known_image = face_recognition.load_image_file(photo_path)
known_encodings = face_recognition.face_encodings(known_image)
if len(known_encodings) == 0:
return {
"error": "No face detected in uploaded image."
}
known_encoding = known_encodings[0]
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
frame_number = 0
matches = []
while True:
success, frame = cap.read()
if not success:
break
if frame_number % int(fps) == 0:
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
face_locations = face_recognition.face_locations(rgb)
face_encodings = face_recognition.face_encodings(
rgb,
face_locations
)
for encoding in face_encodings:
result = face_recognition.compare_faces(
[known_encoding],
encoding,
tolerance=0.5
)
if result[0]:
timestamp = frame_number / fps
matches.append(round(timestamp,2))
frame_number += 1
cap.release()
return {
"matches_found": len(matches),
"timestamps": matches
}