-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebcam2.py
More file actions
151 lines (110 loc) · 4.1 KB
/
webcam2.py
File metadata and controls
151 lines (110 loc) · 4.1 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
import dlib
import cv2 as cv
import numpy as np
# addition
import face_recognition
import pickle
encoding_file = './ai_cv/encodings3.pickle'
unknown_name = 'Unknown'
model_method = 'hog'
output_name = './ai_cv/video/output_' + model_method + '.avi'
# ----------------------
detector = dlib.get_frontal_face_detector()
predictor = dlib.shape_predictor('./ai_cv/model/shape_predictor_68_face_landmarks.dat')
cap = cv.VideoCapture(0)
if not cap.isOpened:
print('### Error opening video ###')
exit(0)
# range는 끝값이 포함안됨
ALL = list(range(0, 68))
RIGHT_EYEBROW = list(range(17, 22))
LEFT_EYEBROW = list(range(22, 27))
RIGHT_EYE = list(range(36, 42))
LEFT_EYE = list(range(42, 48))
NOSE = list(range(27, 36))
MOUTH_OUTLINE = list(range(48, 61))
MOUTH_INNER = list(range(61, 68))
JAWLINE = list(range(0, 17))
index = ALL
def detectFaceAndDisplay(frame):
img_gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY)
dets = detector(img_gray, 1)
rgb = cv.cvtColor(frame, cv.COLOR_BGR2RGB)
boxes = face_recognition.face_locations(rgb, model=model_method)
encodings = face_recognition.face_encodings(rgb, boxes)
names = []
count = 0
# loop over the facial embeddings
for encoding in encodings:
# attempt to match each face in the input image to our known
# encodings
matches = face_recognition.compare_faces(data["encodings"],encoding)
name = 'Unknown'
# check to see if we have found a match
if True in matches:
# find the indexes of all matched faces then initialize a
# dictionary to count the total number of times each face
# was matched
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
# loop over the matched indexes and maintain a count for
# each recognized face face
count = len(matchedIdxs)
print(matchedIdxs)
for i in matchedIdxs:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
# determine the recognized face with the largest number of
# votes (note: in the event of an unlikely tie Python will
# select first entry in the dictionary)
name = max(counts, key=counts.get)
if(counts.get(name) < 5):
name = unknown_name
names.append(name)
for name, face in zip(names, dets):
shape = predictor(frame, face) #얼굴에서 68개 점 찾기
list_points = []
for p in shape.parts():
list_points.append([p.x, p.y])
list_points = np.array(list_points)
for i,pt in enumerate(list_points[index]):
pt_pos = (pt[0], pt[1])
cv.circle(frame, pt_pos, 2, (0, 255, 0), -1)
color = (0, 255, 0)
line = 2
if(name == unknown_name):
color = (0, 0, 255)
line = 2
name = unknown_name
cv.rectangle(frame, (face.left(), face.top()), (face.right(), face.bottom()), color, line)
y = face.top() - 15 if face.top() - 15 > 15 else face.top() + 15
cv.putText(frame, name, (face.left(), y), cv.FONT_HERSHEY_SIMPLEX, 0.75, color, line)
cv.imshow('result', frame)
# load the known faces and embeddings
data = pickle.loads(open(encoding_file, "rb").read())
while True:
ret, frame = cap.read()
if frame is None:
print('--(!) No captured frame -- Break!')
# close the video file pointers
cap.release()
# close the writer point
writer.release()
break
detectFaceAndDisplay(frame)
key = cv.waitKey(1)
if key == 27:
break
elif key == ord('1'):
index = ALL
elif key == ord('2'):
index = LEFT_EYEBROW + RIGHT_EYEBROW
elif key == ord('3'):
index = LEFT_EYE + RIGHT_EYE
elif key == ord('4'):
index = NOSE
elif key == ord('5'):
index = MOUTH_OUTLINE+MOUTH_INNER
elif key == ord('6'):
index = JAWLINE
cv.destroyAllWindows()