-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathvideocamera.py
More file actions
55 lines (41 loc) · 1.5 KB
/
videocamera.py
File metadata and controls
55 lines (41 loc) · 1.5 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
# imports
import cv2
import face_recognition
import pickle
face_cascade = cv2.CascadeClassifier('haarcascade/haarcascade_frontalface_default.xml')
data = pickle.loads(open("dataset_encoding.pickle", "rb").read())
names = []
class VideoCamera(object):
def __init__(self):
self.video = cv2.VideoCapture(0)
def __del__(self):
self.video.release()
def get_frame(self):
success, image = self.video.read()
faces = face_cascade.detectMultiScale(image, 1.3, 5)
rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
rgb = cv2.resize(image, (0, 0), fx = 0.25, fy = 0.25)
r = image.shape[1] / float(rgb.shape[1])
boxes = face_recognition.face_locations(rgb)
encodings = face_recognition.face_encodings(rgb, boxes)
for encoding in encodings:
matches = face_recognition.compare_faces(data["encodings"], encoding)
name = "unknown"
if True in matches:
matchesid = [i for (i, b) in enumerate(matches) if b]
counts = {}
for i in matchesid:
name = data["names"][i]
counts[name] = counts.get(name, 0) + 1
name = max(counts, key = counts.get)
names.append(name)
for ((top, right, bottom, left), name) in zip(boxes, names):
top = int(top * r)
right = int(right * r)
bottom = int(bottom * r)
left = int(left * r)
cv2.rectangle(image, (left, top), (right, bottom), (0, 0, 255), 2)
y = top - 15 if top - 15 > 15 else top + 15
cv2.putText(image, name, (left, y), cv2.FONT_HERSHEY_DUPLEX, 0.75, (0, 0, 255), 2)
ret, jpeg = cv2.imencode('.jpg', image)
return jpeg.tobytes()