-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacedetection.py
63 lines (48 loc) · 2.24 KB
/
facedetection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import cv2
import os
import tempfile
def seconds_to_timestamp(seconds):
# Convert seconds to hours:minutes:seconds
hours = int(seconds // 3600)
minutes = int((seconds % 3600) // 60)
seconds = int(seconds % 60)
return f"{hours:02}:{minutes:02}:{seconds:02}"
def process_video(input_video_path):
# Load the video
cap = cv2.VideoCapture(input_video_path)
# Get the frame rate
frame_rate = cap.get(cv2.CAP_PROP_FPS) # Frame rate
# Get video properties for the output file
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
# Prepare the output path for the annotated video
temp_dir = tempfile.gettempdir()
output_video_path = os.path.join(temp_dir, 'annotated_video.mp4')
# Create VideoWriter object to write the video at original frame rate
out = cv2.VideoWriter(output_video_path, fourcc, frame_rate, (frame_width, frame_height))
# Initialize the face cascade
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
detections = [] # List to store detection times
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 0, 255), 2)
detection_time = cap.get(cv2.CAP_PROP_POS_MSEC) / 1000 # Current position of the video file in milliseconds
timestamp = seconds_to_timestamp(detection_time)
# Check if the timestamp already exists in the detections list before appending it
if timestamp not in detections:
detections.append(timestamp)
print(f"Face visible at timestamp: {timestamp}")
# Write the frame into the file
out.write(frame)
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
# Return the path to the annotated video and the detection times in timestamp format
return output_video_path, detections