forked from jackuio440/finals
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuildface.py
62 lines (51 loc) · 1.74 KB
/
buildface.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
import cv2,os,glob
import numpy as np
from time import sleep
def bface():
def saveImg(image,index):
filename="images/"+"/face{:03d}.jpg".format(index)
cv2.imwrite(filename,image)
index=1
total = 100
os.mkdir("images/")
face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades+"haarcascade_frontalface_alt2.xml")
cap = cv2.VideoCapture(0)
cv2.namedWindow('video',cv2.WINDOW_NORMAL)
while index>0:
ret,frame = cap.read()
frame = cv2.flip(frame,1)
gray = cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray,1.1,3)
for(x,y,w,h) in faces:
frame = cv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),3)
image = cv2.resize(gray[y:y + h,x: x+w],(400,400))
saveImg(image,index)
sleep(0.1)
index+=1
if index > total:
print("done!")
index = -1
break
cv2.imshow('video',frame)
cv2.waitKey(1)
cap.release()
cv2.destroyAllWindows()
images=[]
labels=[]
labelstr=[]
count=0
dirs = os.listdir('images')
for d in dirs:
if os.path.isdir('images/'):
files = glob.glob("images/"+"/*.jpg")
for filename in files:
img = cv2.imread(filename,cv2.COLOR_BGR2GRAY)
images.append(img)
labels.append(count)
labelstr.append(d)
count += 1
print("start create...")
model = cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(images),np.asarray(labels))
model.save('faces_LBPH.yml')
print("create done!")