This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
setFaceByHand.pyw
412 lines (325 loc) · 11.9 KB
/
setFaceByHand.pyw
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
#!python3.4
# https://github.com/cvzi/py_save_face_xmp
import time
import os
import cv2
import re
import numpy as np
try:
import tkinter
except:
import Tkinter
tkinter = Tkinter
from picaseXMPFaceReader import XMPFace
"""
Reads images JPG from current directory
Left mouse: draw rectangle around face
Right mouse: reset rectangle
Middle mouse: select detected face / save faces on second click
Enter: save face to image metadata
Left arrow: previous image
Right arrow: skip image
"""
def point_in_rect(px, py, rect, rect_y0=None, rect_x1=None, rect_y1=None):
if rect_y0 is None:
if len(rect) == 2:
(x0, y0), (x1, y1) = rect
else:
x0, y0, x1, y1 = rect
else:
x0, y0, x1, y1 = rect, rect_y0, rect_x1, rect_y0
return px >= x0 and px <= x1 and py >= y0 and py <= y1
def detectFace(image, grayscaleImage, faceCascade, scale=1.0):
# Detect faces with opencv
try:
detected_faces = faceCascade.detectMultiScale(
grayscaleImage,
scaleFactor=1.1,
minNeighbors=2,
minSize=(5, 5),
flags = cv2.CASCADE_SCALE_IMAGE
)
except cv2.error as e:
print("Error while trying to detect faces: detectMultiScale() throw error:")
print(e)
return []
# Draw a rectangle around the detectedfaces
result = []
for (x, y, w, h) in detected_faces:
cv2.rectangle(image, (int(scale*x), int(scale*y)), (int(scale*(x+w)), int(scale*(y+h))), (100, 255, 70), 1)
result.append((int(scale*x), int(scale*y), int(scale*(x+w)), int(scale*(y+h))))
return result
def selectFace(imagePath, noskip=False):
# Open image in window
global faces
global orgimage
global image
global image2
global resultRect
global detectedFaces
global screenscale
global ctrl_image
global ctrl_input_active
global ctrl_input_str
global ctrl_changed
global requestSaveRect
requestSaveRect = False
# Set window title
fileName = os.path.basename(imagePath)
cv2.setWindowTitle("image", "File: %s" % fileName)
cv2.setWindowTitle("control", "Wait... Skipping...")
# Read the image
try:
image = cv2.imread(imagePath)
orgimage = image.copy()
except:
print("Cannot load %s. Error 01" % imagePath)
return 1 # error, skip
# Read existing faces from tags
try:
facereader = XMPFace(imagePath)
faces = facereader.getFaces()
index = len(faces)
except gi.repository.GLib.Error as e:
print("Cannot load %s. Error 02" % imagePath)
return 1 # error, skip
# Skip files that already have enough faces compared to their filename
#if len(faces) >= len(fileName.split(",")) and not noskip:
# return 1 # a face exists, skip
# Try to detect faces with opencv
detectedFaces = []
print("Finding faces...")
scale = 400.0 / len(image)
grayscaleImage = cv2.resize(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY), (0,0), fx=scale, fy=scale)
for casc in cascades:
f = detectFace(image, grayscaleImage, casc, scale=1.0/scale)
if len(f):
for r in f:
detectedFaces.append(r)
f = detectFace(image, grayscaleImage, casc, scale=1.0/scale)
if len(f):
for r in f:
detectedFaces.append(r)
cv2.setWindowTitle("control", "Faces: %d, Detected: %d" % (len(faces), len(detectedFaces)))
# Draw rectangles around the existing faces
for (x, y, w, h, name) in faces:
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 120), 3)
cv2.putText(image, name, (x, y-3), 5, 0.7, (0, 255, 120), 1, 1);
# Get a name from the filename
try:
name = re.sub(r"\s*\(?\d+\)?\.jpe?g$", "", fileName.split(",")[index]).strip()
except:
name = re.sub(r"\s*\(?\d+\)?\.jpe?g$", "", fileName).strip()
ctrl_input_str = name
ctrl_changed = True
# Copy for mouse drawing
image2 = image.copy()
resultRect = None
# Scale image to screen size
screenscale = min(1.0, screenheight/float(len(image)), screenwidth/float(len(image[0])))
# Show image in window
while True:
screenimage = cv2.resize(image, (0,0), fx=screenscale, fy=screenscale)
cv2.imshow('image',screenimage)
ctrl_show_controls()
k = cv2.waitKey(10)
if k == -1 and not requestSaveRect:
continue
if not ctrl_input_active:
if k == ord('q'): # q -> Quit
cv2.destroyAllWindows()
raise RuntimeError("Ctrl-C")
elif k == 10 or k == 13 or requestSaveRect: # Enter -> Save faces
break
elif k == 65363 or k == 2555904: # Right arrow -> Skip image
return 1
elif k == 65361 or k == 2424832: # Left arrow -> Previous image
return -1
elif k == 114: # r -> Reset index, start with face 0 and overwrite it
print("Reset index=0")
index = 0
elif ctrl_input_active:
ctrl_changed = True
if k == 10 or k == 13: # Enter
ctrl_input_active = False
elif k == 8 or k == 65288: # Backspace
ctrl_input_str = ctrl_input_str[0:-1]
elif k > 27 and k < 127: # Ascii, opencv does not support other characters
ctrl_input_str += chr(k)
elif k-65504 > 27 and k-65504 < 127:
ctrl_input_str += chr(k-65504).upper()
else:
ctrl_changed = False
if resultRect is not None:
# Save face to metadata
cv2.setWindowTitle("control", "Saving changes...")
img_width = len(image[0])
img_height = len(image)
facereader.setDim(img_width, img_height)
(x0,y0),(x1,y1) = resultRect
w = x1 - x0
h = y1 - y0
facereader.setFace(x0, y0, w, h, ctrl_input_str, index)
print("Saving to file...")
facereader.save_file(imagePath)
return 1
# mouse callback function
def mouse_draw_rect(event,x,y,flags,param):
global image,image2,ix,iy,drawing
global resultRect
global detectedFaces
global ctrl_changed
global requestSaveRect
x = int(x*1.0/screenscale)
y = int(y*1.0/screenscale)
if event == cv2.EVENT_MBUTTONDOWN: # Middle mouse button
if resultRect and point_in_rect(x, y, resultRect):
# Save face if clicked into selected rect
requestSaveRect = True
else:
# Use detected face, if clicked into rectangle
for rect in detectedFaces:
if point_in_rect(x, y, rect):
cv2.rectangle(image,(rect[0],rect[1]),(rect[2],rect[3]),(255,255,0),2)
resultRect = [(rect[0],rect[1]),(rect[2],rect[3])]
ctrl_changed = True
break
elif event == cv2.EVENT_RBUTTONDOWN: # Right mouse button
# Reset image
drawing = False
image = image2.copy()
resultRect = None
ctrl_changed = True
elif event == cv2.EVENT_LBUTTONDOWN: # Left mouse button pressed
# Start rectangle
drawing = True
ix,iy = x,y
if event == cv2.EVENT_MOUSEMOVE: # Moving mouse
# Drag rectangle
if drawing == True:
image = image2.copy()
if ix < x and iy < y:
cv2.rectangle(image,(ix,iy),(x,y),(255,255,0),2)
if event == cv2.EVENT_LBUTTONUP: # Left mouse button released
# Stop rectangle
if ix < x and iy < y:
drawing = False
cv2.rectangle(image,(ix,iy),(x,y),(255,255,0),2)
resultRect = [(ix,iy),(x,y)]
ctrl_changed = True
# mouse callback function
def ctrl_mouse_draw_rect(event,x,y,flags,param):
global ctrl_input_rect
global ctrl_input_active
global ctrl_changed
x = x
y = y
if event == cv2.EVENT_LBUTTONDOWN: # Left mouse button pressed
if point_in_rect(x, y, ctrl_input_rect):
ctrl_input_active = True
ctrl_changed = True
ctrl_show_controls()
# Show controls as image
def ctrl_show_controls():
global ctrl_image
global ctrl_input_rect
global ctrl_image_empty
global ctrl_input_active
global ctrl_input_str
global ctrl_changed
global resultRect
global faces
global image2
if not ctrl_changed:
return
ctrl_changed = False
ctrl_image = ctrl_image_empty.copy()
# Input name field
ctrl_input_rect = ((10,10),(400,30))
color = (255, 50, 50) if ctrl_input_active else (50,50,50)
cv2.rectangle(ctrl_image, ctrl_input_rect[0], ctrl_input_rect[1], color, 2)
cv2.putText(ctrl_image, ctrl_input_str, (ctrl_input_rect[0][0], ctrl_input_rect[1][1]-3), 5, 1.0, (0, 0, 0), 1, 1);
# Faces preview
startx = 10
starty = 50
maxwidth = 70
maxheight = 70
for (x, y, w, h, name) in faces:
face = orgimage[y:y+h,x:x+w]
if h > maxheight or w > maxwidth:
scale = min(1.0, maxheight/float(h), maxwidth/float(w))
face = cv2.resize(face, (0,0), fx=scale, fy=scale)
h = len(face)
w = len(face[0])
ctrl_image[starty:starty+h,startx:startx+w] = face
cv2.putText(ctrl_image, name[0:12], (startx, starty+maxheight+5), 1, 0.7, (0, 0, 0), 1, 1);
startx += maxwidth + int(maxwidth*0.1)
# Result preview
if resultRect:
startx = 10
starty += maxheight + int(maxheight*0.1)
(x0,y0),(x1,y1) = resultRect
x, y, w, h = x0, y0, x1-x0, y1-y0
face = orgimage[y:y+h,x:x+w]
if h > maxheight or w > maxwidth:
scale = min(1.0, maxheight/float(h), maxwidth/float(w))
face = cv2.resize(face, (0,0), fx=scale, fy=scale)
h = len(face)
w = len(face[0])
ctrl_image[starty:starty+h,startx:startx+w] = face
cv2.putText(ctrl_image, ctrl_input_str, (startx, starty+maxheight+5), 1, 0.7, (0, 0, 0), 1, 1);
cv2.imshow('control', ctrl_image)
if __name__ == "__main__":
# Walk directory
fileList = []
rootdir = '.'
for root, subFolders, files in os.walk(rootdir):
for file in files:
if file.lower().endswith(".jpg") or file.lower().endswith(".jpeg"):
fileList.append(os.path.join(root,file))
# Detect screen resolution
root = tkinter.Tk()
screenwidth = root.winfo_screenwidth()
screenheight = root.winfo_screenheight()
root.destroy()
root = None
# Load patterns for face detection
cascPath0 = "haarcascade_frontalface_alt.xml"
cascPath1 = "haarcascade_profileface.xml"
cascades = [cv2.CascadeClassifier(cascPath0), cv2.CascadeClassifier(cascPath1)]
# Globals for mouse callback
drawing = False # true if mouse is pressed
ix,iy = -1,-1
screenscale = 1.0
faces = []
detectedFaces = []
orgimage = None
resultRect = None
requestSaveRect = False
# Create window
cv2.namedWindow('image')
cv2.setMouseCallback('image',mouse_draw_rect)
cv2.moveWindow('image', 0, 0)
# Control window
ctrl_input_active = False
ctrl_input_str = ""
ctrl_changed = True
cv2.namedWindow('control')
cv2.moveWindow('control', 800, 0)
cv2.setWindowTitle("control", "Loading...")
ctrl_image_empty = np.array([np.array([255, 255, 255], dtype=np.uint8) for i in range(420*220)]).reshape(220,420,3)
ctrl_show_controls()
cv2.setMouseCallback('control', ctrl_mouse_draw_rect)
# Open each file individually
i = 0
last = -1
while i < len(fileList):
filepath = fileList[i]
resultcode = selectFace(filepath, last >= i)
last = i
if not resultcode:
break
i += resultcode
if i < 0:
i = 0
cv2.destroyAllWindows()