-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathccv_facedetector.py
312 lines (263 loc) · 13.6 KB
/
ccv_facedetector.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import print_function
import commands
import logging
import os
import os.path
import random
import string
import cv2
import numpy as np
from trendi.constants import project_dir
path_to_ccvface = '/' + project_dir + '/classifier_stuff/ccvface'
path_to_ccvface_db = '/' + project_dir + '/classifier_stuff/ccvface.sqlite3'
def ccv_facedetect(filename=None, image_array=None):
delete_when_done = False
if not filename or not os.path.isfile(filename):
if image_array is not None:
filename = '/var/tmp/' + rand_string() + '.jpg'
if not cv2.imwrite(filename, image_array):
raise IOError("Could not save temp image")
delete_when_done = True
else:
raise IOError("Bad parameters passed -- no file and no array.")
detect_command = "{path_to_ccvface} {filename} {path_to_ccvface_db}" \
.format(path_to_ccvface=path_to_ccvface, filename=filename, path_to_ccvface_db=path_to_ccvface_db)
retvals = commands.getstatusoutput(detect_command)
# logging.debug('return from command ' + detect_command + ':' + str(retvals), end="\n")
if delete_when_done:
try:
os.remove(filename)
except Exception as e:
logging.warning("ccv_facedetect could not delete file {0} because of exception: \n{1}".format(filename, e))
rects = []
if isinstance(retvals[1], basestring) and retvals[1] != '':
rectangle_strings = retvals[1].split('\n')
logging.debug('rectangle_strings:' + str(rectangle_strings))
for rectangle_string in rectangle_strings:
new_rect = [int(s) for s in rectangle_string.split() if s.isdigit()]
if len(new_rect) == 4:
rects.append(new_rect)
else:
logging.warning('Got weird string from ccv:' + rectangle_string)
arr = np.asarray(rects, dtype='uint16')
logging.debug('rects: ' + str(arr))
return rects
else:
logging.debug('No answer string recd from ccv')
return []
def rand_string(length=32):
return ''.join((random.choice(string.ascii_letters + string.digits) for i in xrange(length)))
def check_lfw(use_visual_output=False):
BASE_PATH = os.getcwd()
BASE_PATH2 = os.path.join(BASE_PATH, 'many_faces')
print('basepath:' + BASE_PATH2)
n_images = 0
n_extra = 0
n_single_detections = 0
for dirname, dirnames, filenames in os.walk(BASE_PATH2):
dirnames.sort()
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
abs_path = "%s/%s" % (subject_path, filename)
faces = ccv_facedetect(abs_path)
# faces = background_removal.find_face()
print('path:' + abs_path + ' faces:' + str(faces), end="\r")
n_images = n_images + 1
if len(faces) > 1:
n_extra = n_extra + 1
if len(faces) == 1:
n_single_detections = n_single_detections + 1
if use_visual_output:
show_rects(abs_path, faces)
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra) + ' n_detections:' + str(
n_single_detections), end="\r")
true_positives = n_single_detections + n_extra
true_pos_rate = float(true_positives) / n_images
false_neg_rate = float(n_images - true_positives) / n_images
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra) + ' n_detections:' + str(n_single_detections))
print('true pos:' + str(true_pos_rate) + ' false_neg:' + str(false_neg_rate))
BASE_PATH = os.getcwd()
BASE_PATH2 = os.path.join(BASE_PATH, 'male-female/male')
print('basepath:' + BASE_PATH2)
n_images = 0
n_extra = 0
n_single_detections = 0
for dirname, dirnames, filenames in os.walk(BASE_PATH2):
dirnames.sort()
for subdirname in dirnames:
subject_path = os.path.join(dirname, subdirname)
for filename in os.listdir(subject_path):
abs_path = "%s/%s" % (subject_path, filename)
faces = ccv_facedetect(abs_path)
print('path:' + abs_path + ' faces:' + str(faces), end="\r")
n_images = n_images + 1
if len(faces) > 1:
n_extra = n_extra + 1
if len(faces) == 1:
n_single_detections = n_single_detections + 1
if use_visual_output:
show_rects(abs_path, faces)
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra) + ' n_detections:' + str(
n_single_detections), end="\n")
true_positives = n_single_detections + n_extra
true_pos_rate = float(true_positives) / n_images
false_neg_rate = float(n_images - true_positives) / n_images
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra) + ' n_detections:' + str(n_single_detections))
print('true pos:' + str(true_pos_rate) + ' false_neg:' + str(false_neg_rate))
def run_classifier_recursively(path=None, use_visual_output=False, classifier=ccv_facedetect, n_images=0,
n_single_detections=0, n_extra_detections=0, classifier_arg=None):
if path is None:
path = os.getcwd()
print('basepath:' + path)
raw_input('enter to continue')
donePaths = []
for paths, dirs, files in os.walk(path):
if paths not in donePaths:
count = paths.count('/')
if files:
for ele1 in files:
raw_input('enter to continue')
# print('---------' * (count), ele1)
full_name = os.path.join(path, ele1)
print('arg to classifier:' + str(classifier_arg))
img_arr = cv2.imread(full_name)
faces = classifier(img_arr, method=classifier_arg)
n_images = n_images + 1
print('faces:' + str(faces) + ' images:' + str(n_images) + ' file:' + str(ele1), end="\n")
if len(faces) > 1:
n_extra_detections = n_extra_detections + len(faces) - 1
if len(faces) == 1:
n_single_detections = n_single_detections + 1
# write_rects(full_name,faces,version=classifier_arg)
if use_visual_output:
show_rects(full_name, faces)
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra_detections) + ' n_detections:' + str(
n_single_detections) + ' file:' + str(ele1), end="\n")
print('')
if dirs:
for ele2 in dirs:
print('---------' * (count), ele2)
absPath = os.path.join(paths, ele2)
# recursively calling the direct function on each directory
n_images, n_single_detections, n_extra_detections = run_classifier_recursively(path=absPath,
use_visual_output=use_visual_output,
classifier=classifier,
n_images=n_images,
n_single_detections=n_single_detections,
n_extra_detections=n_extra_detections,
classifier_arg=classifier_arg)
# adding the paths to the list that got traversed
donePaths.append(absPath)
if n_images:
positives = n_single_detections + n_extra_detections
pos_rate = float(positives) / n_images
neg_rate = float(n_images - positives) / n_images
print('n_images:' + str(n_images) + ' n_extra:' + str(n_extra_detections) + ' n_detections:' + str(
n_single_detections))
print('pos rate:' + str(pos_rate) + ' neg rate:' + str(neg_rate))
return n_images, n_single_detections, n_extra_detections
else:
return 0, 0, 0
# !/usr/bin/python
# Creating an empty list that will contain the already traversed paths
def direct(path):
donePaths = []
for paths, dirs, files in os.walk(path):
if paths not in donePaths:
count = paths.count('/')
if files:
for ele1 in files:
print('---------' * (count), ele1)
if dirs:
for ele2 in dirs:
print('---------' * (count), ele2)
absPath = os.path.join(paths, ele2)
# recursively calling the direct function on each directory
direct(absPath)
# adding the paths to the list that got traversed
donePaths.append(absPath)
def show_rects(abs_path, faces, save_figs=True):
img_arr = cv2.imread(abs_path)
if len(faces):
for rect in faces:
print('rect:' + str(rect))
cv2.rectangle(img_arr, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), [0, 0, 250], 2)
cv2.imshow('candidate', img_arr)
newname = abs_path.replace('.jpg', '.cascaderects.jpg')
cv2.imwrite(newname, img_arr)
cv2.waitKey(10)
def write_rects(abs_path, faces, version=None):
img_arr = cv2.imread(abs_path)
if len(faces):
for rect in faces:
print('rect:' + str(rect))
cv2.rectangle(img_arr, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), [0, 0, 250], 2)
# cv2.imshow('candidate',img_arr)
if version is not None:
abs_path = abs_path.replace('.jpg', version + '.jpg')
newname = abs_path.replace('.jpg', '.rects.jpg')
print('newname:' + str(newname))
cv2.imwrite(newname, img_arr)
def depth_of_subdir_of_calling_function():
'''
this finds the depth of subdirectory in which the caller resides
:return:
'''
path = os.getcwd()
# print('path:'+str(path))
p2 = path.split('trendi_guru_modules')
# print('path split on trendigurumodules:'+str(p2))
if len(p2) < 2:
print('not in trendi_guru_modules')
secondhalf = p2[1]
# print('secondhalf:'+str(secondhalf))
cur = secondhalf.split('/')
# print('cur:'+str(cur))
if len(cur) > 1:
in_subdir_of_trendi_guru_modules = True
return len(cur) - 1
if __name__ == "__main__":
# direct('.')
# pos,neg = run_classifier_on_dir_of_dirs('/home/jeremy/jeremy.rutman@gmail.com/TrendiGuru/techdev/trendi_guru_modules/classifier_stuff/images/llamas')
# n,singles,multiples = run_classifier_recursively('images/many_faces',use_visual_output=True,classifier=background_removal.find_face_cascade)
# n,singles,multiples = run_classifier_recursively('images/many_faces',use_visual_output=True)
# print('n:{0} single:{1} multiple:{2}'.format(n,singles,multiples))
# raw_input('enter to continue')
# filenames = ["images/female1.jpg","images/male1.jpg","images/female2.jpg","images/male2.jpg","images/female3.jpg","images/male3.jpg"]
filenames = ["images/female_korean1.jpg","images/male_korean1.jpg","images/female_korean2.jpg","images/male_korean2.jpg","images/female_korean3.jpg","images/male_korean3.jpg"]
for filename in filenames:
faces = ccv_facedetect(filename)
print('faces:' + str(faces)+ " in file "+filename)
img_arr = cv2.imread(filename)
if len(faces):
for rect in faces:
print('rect:' + str(rect))
cv2.rectangle(img_arr, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), [255, 255, 255], 5)
cv2.rectangle(img_arr, (rect[0], rect[1]), (rect[0] + rect[2], rect[1] + rect[3]), [255, 255, 255], 5)
cv2.imshow('candidate', img_arr)
cv2.waitKey(0)
if(0):
filename = "images/female1.jpg"
img_arr = cv2.imread(filename)
faces = ccv_facedetect(image_array=img_arr)
print('faces:' + str(faces))
n, singles, multiples = run_classifier_recursively('images/many_faces', use_visual_output=True)
print('n:{0} single:{1} multiple:{2}'.format(n, singles, multiples))
raw_input('enter to continue')
n, singles, multiples = run_classifier_recursively(
'/home/developer/python-packages/trendi_guru_modules/images/female_faces')
print('n:{0} single:{1} multiple:{2}'.format(n, singles, multiples))
raw_input('enter to continue')
n, singles, multiples = run_classifier_recursively(
'/home/developer/python-packages/trendi_guru_modules/classifier_stuff/images/llamas')
print('n:{0} single:{1} multiple:{2}'.format(n, singles, multiples))
raw_input('enter to continue')
n, singles, multiples = run_classifier_recursively(
'/home/developer/python-packages/trendi_guru_modules/classifier_stuff/images/monkeys')
print('n:{0} single:{1} multiple:{2}'.format(n, singles, multiples))
raw_input('enter to continue')
n, singles, multiples = run_classifier_recursively(
'/home/developer/python-packages/trendi_guru_modules/classifier_stuff/images/male_faces')
print('n:{0} single:{1} multiple:{2}'.format(n, singles, multiples))
raw_input('enter to continue')