-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdetect.py
36 lines (29 loc) · 1.18 KB
/
detect.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
import cv2
import sys
import os.path
def detect(filepath, cascade_file = "./lbpcascade_animeface.xml"):
if not os.path.isfile(cascade_file):
raise RuntimeError("%s: not found" % cascade_file)
cascade = cv2.CascadeClassifier(cascade_file)
i = 0
for f in os.listdir(filepath):
image = cv2.imread(filepath + '/' + f, cv2.IMREAD_COLOR)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.equalizeHist(gray)
faces = cascade.detectMultiScale(gray,
# detector options
scaleFactor = 1.1,
minNeighbors = 5,
minSize = (24, 24))
for x, y, w, h in faces:
if w >= 64 and h >= 64:
i += 1
cv2.imwrite("data/face%06d.jpg" %i, image[y:y+h, x:x+w])
print("Cut success. Total sample %d data" %i)
print("Total sample %d data" %i)
if len(sys.argv) != 2:
sys.stderr.write("usage: detect.py <filename>\n")
sys.exit(-1)
if not os.path.isdir('data'):
os.makedirs('data')
detect(sys.argv[1])