-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfetchdata.py
More file actions
67 lines (51 loc) · 2.04 KB
/
Copy pathfetchdata.py
File metadata and controls
67 lines (51 loc) · 2.04 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
import cv2
import time
import pymongo
import numpy as np
import face_recognition
from keys import sendgridKey, mongoKey, mailID
def fetch():
client = pymongo.MongoClient(mongoKey)
db = client['test']
collection = db['userdata']
known_face_encodings = []
known_face_names = []
for data in collection.find():
username = data["username"]
image = np.asarray(bytearray(data["image"]), dtype="uint8")
image = cv2.imdecode(image, cv2.IMREAD_COLOR)
face_encoding = face_recognition.face_encodings(image)[0]
known_face_encodings.append(face_encoding)
known_face_names.append(username)
cam = cv2.VideoCapture(0)
cv2.namedWindow("Camera", cv2.WINDOW_NORMAL)
cv2.resizeWindow("Camera", 960, 720)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, frame = cam.read()
if not ret:
print("Failed to capture image from camera.")
break
face_locations = face_recognition.face_locations(frame)
face_encodings = face_recognition.face_encodings(frame, face_locations)
match_found = False
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
username = "Unknown"
face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
best_match_index = np.argmin(face_distances)
if matches[best_match_index]:
username = known_face_names[best_match_index]
match_found = True
break
cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(frame, username, (left + 6, bottom - 6), font, 1.0, (0, 255, 0), 1)
cv2.imshow("Camera", frame)
if match_found:
cam.release()
cv2.destroyAllWindows()
return username
if cv2.waitKey(1) == ord("q"):
break
cam.release()
cv2.destroyAllWindows()