-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera.py
More file actions
119 lines (92 loc) · 3.17 KB
/
camera.py
File metadata and controls
119 lines (92 loc) · 3.17 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
import cv2
from pathlib import Path
from collections import Counter
import threading
import pickle
import face_recognition
import cv2
import asyncio
DEFAULT_ENCODINGS_PATH = Path("output/encodings.pkl")
Path("training").mkdir(exist_ok=True)
Path("output").mkdir(exist_ok=True)
Path("validation").mkdir(exist_ok=True)
def encode_known_faces(
model: str = "hog", encodings_location: Path = DEFAULT_ENCODINGS_PATH
) -> None:
names = []
encodings = []
for filepath in Path("training").glob("*/*"):
name = filepath.parent.name
image = face_recognition.load_image_file(filepath)
face_locations = face_recognition.face_locations(image, model=model)
face_encodings = face_recognition.face_encodings(image, face_locations)
for encoding in face_encodings:
names.append(name)
encodings.append(encoding)
name_encodings = {"names": names, "encodings": encodings}
with encodings_location.open(mode="wb") as f:
pickle.dump(name_encodings, f)
def recognize_faces(
image_location: str,
model: str = "hog",
encodings_location: Path = DEFAULT_ENCODINGS_PATH,
) -> None:
with encodings_location.open(mode="rb") as f:
loaded_encodings = pickle.load(f)
input_image = face_recognition.load_image_file(image_location)
input_face_locations = face_recognition.face_locations(
input_image, model=model
)
input_face_encodings = face_recognition.face_encodings(
input_image, input_face_locations
)
for bounding_box, unknown_encoding in zip(
input_face_locations, input_face_encodings
):
name = _recognize_face(unknown_encoding, loaded_encodings)
if not name:
name = "Unknown"
print(name)
def _recognize_face(unknown_encoding, loaded_encodings):
boolean_matches = face_recognition.compare_faces(
loaded_encodings["encodings"], unknown_encoding
)
votes = Counter(
name
for match, name in zip(boolean_matches, loaded_encodings["names"])
if match
)
if votes:
return str(votes.most_common(1)[0][0])
cnt = 0
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
video_capture = cv2.VideoCapture(0)
def detect_bounding_box(vid):
gray_image = cv2.cvtColor(vid, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray_image, 1.1, 5, minSize=(40, 40))
for (x, y, w, h) in faces:
cv2.rectangle(vid, (x, y), (x + w, y + h), (255, 255, 255), 4)
return faces
def recognize_faces_thread(img_name):
amon = recognize_faces(img_name)
print(amon)
while True:
result, frame = video_capture.read()
if result is False:
break
faces = detect_bounding_box(frame)
#if cnt % 40 == 0:
img_name = "frame.jpg"
ima = cv2.pyrDown(frame)
cv2.imwrite(img_name, ima)
if cnt % 40 == 0 :
thread = threading.Thread(target=recognize_faces_thread, args=(img_name,))
thread.start()
cv2.imshow("My Face Detection Project", frame)
cnt += 1
if cv2.waitKey(1) & 0xFF == ord("q"):
break
video_capture.release()
cv2.destroyAllWindows()