-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_processor.py
75 lines (52 loc) · 2.18 KB
/
video_processor.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
64
65
66
67
68
69
70
71
from mtcnn import MTCNN
from deepface import DeepFace
import cv2
from tqdm import tqdm
import numpy as np
detector = MTCNN()
def process_video(path, prediction_model, step=24, end=None, pred_freq = .2):
cap = cv2.VideoCapture(path)
if not cap.isOpened():
raise Exception("Invalid Video: ", path)
total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
step = min(total_frames//4, step)
frame_count = 0
pred_idx = 0
all_embeddings = []
predictions = {}
history = {}
face_counts = []
with tqdm(total=total_frames) as pbar:
while cap.isOpened():
ret, frame = cap.read()
# if i<cap.get(cv2.CAP_PROP_FPS)*32:
# continue
if not ret or (end and frame_count>cap.get(cv2.CAP_PROP_FPS)*end):
break
# frame = imutils.resize(frame, width=1024)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
detections = detector.detect_faces(frame)
face_counts.append(0)
history[frame_count] = []
for detection in detections:
confidence = detection["confidence"]
if confidence > 0.9:
x, y, w, h = detection["box"]
if (h*w)<1200:
continue
detected_face = frame[int(y):int(y+h), int(x):int(x+w)]
embedding = np.array(DeepFace.represent(detected_face, model_name='Facenet512', enforce_detection=False)[0]['embedding'])
all_embeddings.append(embedding)
if np.random.random() < pred_freq or pred_idx == 0:
predictions[pred_idx] = prediction_model.predict(cv2.cvtColor(detected_face, cv2.COLOR_RGB2BGR))
face_counts[-1] += 1
history[frame_count].append((pred_idx, (x, y, w, h)))
pred_idx += 1
pbar.update(step)
frame_count += step
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_count)
# Release resources
cap.release()
cv2.destroyAllWindows()
all_embeddings = np.array(all_embeddings)
return history, all_embeddings, predictions, face_counts