-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfacer.py
executable file
·59 lines (49 loc) · 1.55 KB
/
facer.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
#!/usr/bin/env python3
import cv2
import sys
import os
import glob
import json
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
def lf(path):
if os.path.exists(path):
with open(path) as fp:
try:
return json.load(fp)
except:
return {}
for userPath in sorted(glob.glob('data/*')):
user = os.path.basename(userPath)
if not os.path.isdir(userPath):
continue
faceMap = lf('data/{}/faces.json'.format(user)) or {}
print(user, end='', flush=True)
for imagePath in glob.glob("data/{}/*[jp][np]g".format(user)):
print('.', end='', flush=True)
filename = os.path.basename(imagePath)
if filename in faceMap:
continue
try:
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=7,
minSize=(300, 300),
flags = cv2.CASCADE_SCALE_IMAGE
)
faceMap[filename] = 1 if len(faces) else 0
except:
continue
print('done')
with open("data/{}/faces.json".format(user), 'w') as fp:
json.dump(faceMap, fp)
try:
faceMaster = lf('facemaster.json') or {}
faceMaster[user] = sum(faceMap.values()) / len(faceMap.values())
with open("facemaster.json", 'w') as fp:
json.dump(faceMaster, fp)
except:
pass