-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfacerecon-system.py
73 lines (65 loc) · 2.35 KB
/
facerecon-system.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
import cv2
import scipy.io as sio
import os
from centerface import CenterFace
import uuid
import sys
import pandas as pd
class FaceRecognition():
def __init__(self, video: str, start, end, fullpic=False):
self.vidcap = cv2.VideoCapture(video)
self.fullpic = fullpic
sec = start
frameRate = 1/2 #//it will capture image in each 0.5 second
count=1
d = {
'timestamp': [],
'filename': []
}
self.track = pd.DataFrame(data = d)
success = self._getFrame(sec, count)
while success:
count = count + 1
sec = sec + frameRate
sec = round(sec, 2)
success = self._getFrame(sec, count)
if sec >= end:
self.track.to_csv('track_{}_s{}_e{}.csv'.format(video.split('.')[0], start, end), index=False)
break
def _getFrame(self, sec, count):
self.vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = self.vidcap.read()
names = []
if hasFrames:
h, w = image.shape[:2]
landmarks = True
centerface = CenterFace(landmarks=landmarks)
if landmarks:
dets, lms = centerface(image, h, w, threshold=0.35)
else:
dets = centerface(image, threshold=0.35)
for det in dets:
boxes, score = det[:4], det[4]
x, y, dx, dy = int(boxes[0]), int(boxes[1]), int(boxes[2]), int(boxes[3])
cropped = image[y:dy, x:dx]
curruuid = uuid.uuid1()
if cropped.size != 0:
cv2.imwrite("crop/face" + str(count)+ '_' + str(curruuid)+".jpg", cropped)
names.append("face" + str(count)+ '_' + str(curruuid)+".jpg")
if self.fullpic:
cv2.imwrite("images/image"+str(count)+".jpg", image) # save frame as JPG file
self.track = self.track.append({
'timestamp': float(sec),
'filename': names
}, ignore_index=True)
return hasFrames
from time import time
start_time = float(sys.argv[1])
end_time = float(sys.argv[2])
videoName = str(sys.argv[3])
# start_time = 0
# end_time = 30
print (start_time, end_time)
start = time()
fr = FaceRecognition(videoName, start_time, end_time)
print (time()-start)