-
Notifications
You must be signed in to change notification settings - Fork 2
/
face_detector.py
194 lines (146 loc) · 5.59 KB
/
face_detector.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import os
import re
import uuid
import argparse
import statistics
import cv2
import face_recognition
#
#
# Functions
#
#
def makeFolder(path):
try:
if not os.path.exists(path):
os.makedirs(path)
except OSError as e:
if e.errno != errno.EEXIST:
raise
def checkFaceMatchesPerson(face, skips = []):
result = []
for person in persons:
if person in skips:
continue
matches = face_recognition.compare_faces(person['faces'], face, args.tolerance)
matchAccuracy = (sum(matches) / len(matches))
result.append({
'person': person,
'matchAccuracy': matchAccuracy
})
return result
def getClosestMatch(matches):
bestMatch = {'matchAccuracy': -1}
for match in matches:
if match['matchAccuracy'] > bestMatch['matchAccuracy']:
bestMatch = match
return bestMatch
def determinePerson(skips = []):
matchData = checkFaceMatchesPerson(face, skips)
matched = False
if len(matchData):
data = getClosestMatch(matchData)
matchAccuracy = data['matchAccuracy']
if matchAccuracy > args.matchaccuracy:
return {
'person': data['person'],
'accuracy': matchAccuracy
}
return False
def createNewPerson(face):
newPerson = {
'name': f'E{len(persons)+1}',
'faces': [face]
}
persons.append(newPerson)
return newPerson
#
#
# Logic Start
#
#
parser = argparse.ArgumentParser()
parser.add_argument('--video')
parser.add_argument('--frames', nargs='?', const=1, type=float, default=.01)
parser.add_argument('--matchaccuracy', nargs='?', const=1, type=float, default=.15)
parser.add_argument('--tolerance', nargs='?', const=1, type=float, default=.6)
parser.add_argument('--blur', nargs='?', const=1, type=int, default=0)
parser.add_argument('--framesize', nargs='?', const=1, type=int, default=1)
args = parser.parse_args()
# Open video file
video = args.video
video_capture = cv2.VideoCapture(video)
video_capture_frame_count = int(video_capture.get(cv2.CAP_PROP_FRAME_COUNT));
print(f'Total Frames: {video_capture_frame_count}')
# Create a folder for this video
alphanumeric_video_name = re.sub('[^0-9a-zA-Z]+', '_', video)
base_folder = f'faces/{alphanumeric_video_name}'
makeFolder(base_folder)
# Base definition for variables
persons = []
frame_count = 0
# Process Video
while video_capture.isOpened():
# Grab a single frame of video
retval, frame = video_capture.read()
# Break the loop after process whole video
if not retval:
break
# Resize frame of video. Smaller frame size can improve face recognition processing speed
frame = cv2.resize(frame, (0, 0), fx=args.framesize, fy=args.framesize)
# Convert the image from BGR color (which OpenCV uses) to RGB color (which face_recognition uses)
rgb_frame = frame[:, :, ::-1]
frame_count += 1
# Skip Frames to help speed up processing
if frame_count % round(video_capture_frame_count * args.frames) == 0:
# Pull facial Data
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
face_names = []
face_accuracy = []
for face in face_encodings:
person = determinePerson()
if person != False:
name = person['person']['name']
accuracy = person['accuracy']
person['person']['faces'].append(face)
else:
name = createNewPerson(face)['name']
accuracy = 1
print(f' - Found {name} ({accuracy})')
face_names.append(name)
face_accuracy.append(accuracy)
# Display the resulting image
output_frame = frame.copy()
if args.blur > 0:
output_frame = cv2.blur(output_frame, (args.blur, args.blur), cv2.BORDER_DEFAULT)
# Save faces, and highlight on output
for (top, right, bottom, left), name, matchP in zip(face_locations, face_names, face_accuracy):
# Write to disk
file_name = str(uuid.uuid4())
person_folder = f'{base_folder}/{name}'
makeFolder(person_folder)
cv2.imwrite(f'{person_folder}/{file_name}.jpg', frame.copy()[top:bottom, left:right])
# Draw a box around the face, and text displaying person and match certancy
cv2.rectangle(output_frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(output_frame, '{} ({})'.format(name, round(matchP, 2)), (left + 6, bottom - 6), cv2.FONT_HERSHEY_SIMPLEX, .5, (255, 255, 255), 1, cv2.LINE_AA)
# Render image
cv2.imshow('Video Facial Extraction Classification', output_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
averageFaces = statistics.mean(map(lambda p: len(p['faces']), persons))
print(f'Checking for potential false positive Threshold ({averageFaces})')
for person in persons:
# If the person has 25% or less face extractions that the average, we assume it may be a false positive
faceThreshold = averageFaces * .25
faceCount = len(person['faces'])
if faceCount < faceThreshold:
person_name = person['name']
print(f' - Potential false: {person_name} faces ({faceCount}/{faceThreshold})')
for face in person['faces']:
person = determinePerson(face, [person])
if person != False:
print('Face better matches ' + data['person']['name'])
# Cleanup
video_capture.release()
cv2.destroyAllWindows()