-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathInference_on_video.py
executable file
·84 lines (65 loc) · 2.85 KB
/
Inference_on_video.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
72
73
74
75
76
77
78
79
80
81
82
83
84
from detect_face_video import detect_face
import cv2
import pickle
import numpy as np
import argparse
class inference():
def load_model(self):
with open('./outputs/EVM_model.pkl','rb') as b:
self.mevm = pickle.load(b)
with open('./outputs/class_names.pkl','rb') as cl:
self.cls_names = pickle.load(cl)
def read_video(self,path):
self.cap = cv2.VideoCapture(path)
width, height = self.cap.get(cv2.CAP_PROP_FRAME_WIDTH), self.cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
fourcc=cv2.VideoWriter_fourcc('M','J','P','G')
#fourcc = cv2.cv.CV_FOURCC(*'XVID')
self.videowriter = cv2.VideoWriter('./outputs/output_video.avi', fourcc, 20, (int(width),int(height),))
#Facetection class to be loaded
self.df = detect_face()
self.df.load_facenet()
def run_inference(self):
while(self.cap.isOpened()):
# Capture frame-by-frame
ret, frame0 = self.cap.read()
if ret == True:
try:
frame = cv2.cvtColor(frame0,cv2.COLOR_BGR2RGB)
#frame = cv2.rotate(frame1, cv2.ROTATE_90_COUNTERCLOCKWISE)
res = self.df.vid_detect_face(frame)
bb_img = self.df.draw_detect(frame)
face = self.df.vid_crop_resize()
#print(len(res))
#print(np.shape(face))
for i in range(len(res)):
embds = self.df.get_embedding(np.array(face[i]))
print(np.shape(embds))
x = self.df.normalize([embds])
probs,index = self.mevm.max_probabilities(x)
#bb_img = bb_img[:,:,::-1]
print(probs,self.cls_names[index[0][0]])
if probs[0] > 0.9:
cv2.putText(bb_img,self.cls_names[index[0][0]],(res[i]['box'][0],res[i]['box'][1]-10),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,0,255),2)
self.videowriter.write(bb_img[:,:,::-1])
if probs[0] < 0.2:
cv2.putText(bb_img,"unknown",(res[i]['box'][0],res[i]['box'][1]-10),cv2.FONT_HERSHEY_COMPLEX_SMALL,1,(0,0,255),2)
self.videowriter.write(bb_img[:,:,::-1])
except Exception as e:
print(e)
self.videowriter.write(frame)
continue
else:
break
self.videowriter.release()
self.cap.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
print("args.video_path")
parser = argparse.ArgumentParser()
parser.add_argument('--video_path',required=True,
help = 'Please input the path of the input video')
args = parser.parse_args()
inf = inference()
inf.load_model()
inf.read_video(args.video_path)
inf.run_inference()