-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceVideo.py
More file actions
180 lines (165 loc) · 5.95 KB
/
FaceVideo.py
File metadata and controls
180 lines (165 loc) · 5.95 KB
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
import cv2
import os
import numpy as np
from PIL import Image
def photoTest(path):
name = ['NOpreson','Ryanair', 'wanglei', 'fanbingbin']
imgTest = cv2.imread(path)
facecascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recoginer = cv2.face.createLBPHFaceRecognizer()
recoginer.load("recognizer\\trainningData.yml")
gray = cv2.cvtColor(imgTest, cv2.COLOR_BGR2GRAY)
# Detect faces in the image
faces = facecascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CV_FEATURE_PARAMS_HAAR
)
id = 0
num = 0
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
id, conf = recoginer.predict(gray[y:y + h, x:x + w])
print(conf)
if conf <= 50:
print(name[id])
num = id
m = (int)(x + w / 2)
n = (int)(y + h / 2)
cv2.rectangle(imgTest, (x, y), (x + w, y + h), (255, 255, 255), 2)
cv2.rectangle(imgTest, (m, n - 10), (m, n + 10), (255, 255, 255), 2)
cv2.rectangle(imgTest, (m - 10, n), (m + 10, n), (255, 255, 255), 2)
cv2.imshow(name[id], imgTest)
cv2.waitKey(0)
def getImageface(path, id):
# Create the haar
facecascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Read the image
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
number = len(imagePaths)
num=0
for imagepath in imagePaths:
num = num + 1
gray = cv2.imread(imagepath, cv2.IMREAD_GRAYSCALE)
# Detect faces in the image
faces = facecascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=5,
minSize=(30, 30),
flags=cv2.CV_FEATURE_PARAMS_HAAR
)
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
cv2.imwrite("img/User." + str(id) + "." + str(num) + ".jpg", gray[y:y + h, x:x + w])
def videoTest():
cap = cv2.VideoCapture(0)
facecascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
recoginer = cv2.face.createLBPHFaceRecognizer()
recoginer.load("recognizer\\trainningData.yml")
name = ['Ryanair', 'wanglei']
id = 0
while (True):
# Capture frame-by-frame
ret, img = cap.read()
# Our operations on the frame come hereq
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = facecascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=7,
minSize=(30, 30),
flags=cv2.CV_FEATURE_PARAMS_HAAR
)
# print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
id, conf = recoginer.predict(gray[y:y + h, x:x + w])
print(conf)
if conf <= 40:
cv2.putText(img, name[id - 1], (x, y + h), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
m = (int)(x + w / 2)
n = (int)(y + h / 2)
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
cv2.rectangle(img, (m, n - 10), (m, n + 10), (255, 255, 255), 2)
cv2.rectangle(img, (m - 10, n), (m + 10, n), (255, 255, 255), 2)
# print((int)(x + w / 2), y + h / 2)
# Display the resulting frame
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
def video(id):
cap = cv2.VideoCapture(0)
facecascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sampleNum=0
while(True):
# Capture frame-by-frame
ret, img = cap.read()
# Our operations on the frame come hereq
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = facecascade.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=7,
minSize=(30, 30),
flags=cv2.CV_FEATURE_PARAMS_HAAR
)
print("Found {0} faces!".format(len(faces)))
# Draw a rectangle around the faces
for (x, y, w, h) in faces:
sampleNum = sampleNum + 1
m = (int)(x + w / 2)
n = (int)(y + h / 2)
cv2.imwrite("img/User."+str(id)+"."+str(sampleNum)+".jpg" ,gray[y:y+h ,x:x+w])
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 255, 255), 2)
cv2.rectangle(img, (m, n - 10), (m, n + 10), (255, 255, 255), 2)
cv2.rectangle(img, (m - 10, n), (m + 10, n), (255, 255, 255), 2)
print((int)(x + w / 2), y + h / 2)
# Display the resulting frame
cv2.imshow('frame', img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if(sampleNum>70):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
def getImagesWithID(path):
imagePaths = [os.path.join(path ,f) for f in os.listdir(path)]
faces = []
IDs = []
for imagePath in imagePaths:
faceImg = Image.open(imagePath).convert('L')
faceNp = np.array(faceImg, 'uint8')
ID = int(imagePath.split('.')[1])
faces.append(faceNp)
IDs.append(ID)
cv2.imshow("tranining" ,faceNp)
cv2.waitKey(10)
return IDs ,faces
def trainDatas():
recoginer = cv2.face.createLBPHFaceRecognizer()
path = 'img'
getImagesWithID(path)
IDs, faces = getImagesWithID(path)
recoginer.train(faces, np.array(IDs))
recoginer.save('recognizer/trainningData.yml')
cv2.destroyAllWindows()
if __name__ == '__main__':
while(1):
num = input('1: take photos 2: train 3:Test import: ')
if num == '1':
id = input('enter your id : ')
video(id)
elif num == '2':
trainDatas()
elif num == '3':
videoTest()
else:
break