This repository has been archived by the owner on Aug 17, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recognitionBACKUP.py
74 lines (67 loc) · 2.35 KB
/
recognitionBACKUP.py
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
from imutils.video import VideoStream
from imutils.video import FPS
import face_recognition
import imutils
import pickle
import base64, numpy, io, binascii
import time
import requests
from PIL import Image
import eventLogger
import lockCtrl
from cv2 import COLOR_BGR2RGB, COLOR_BGR2GRAY, CascadeClassifier, cvtColor, CASCADE_SCALE_IMAGE
import cv2
embeddedDB = None
postURL = None
def fetchEmbedded():
global embeddedDB
#if(embeddedDB == None):
#Force fetch DB
# r = requests.get(FORCE_ENDPOINT_HERE)
# embeddedDB = {"encodings": r.json().get("encodings"), "names": r.json().get("names")}
#else:
# #Update DB if new images were added
# r = requests.get('https://boiling-reef-89836.herokuapp.com/lock_owners/api/recognition/get_embedded_data/')
# if(r.json().get("status") == 200):
# embeddedDB = {"encodings": r.json().get("encodings"), "names": r.json().get("names")}
print("[INFO] Fetching embeddings...")
embeddedDB = pickle.loads(open("encodings.pickle", "rb").read())
def start(q, postURLe):
global embeddedDB
global postURL
postURL = postURLe
embedUpdateTimeout = 0
fetchEmbedded()
detector = CascadeClassifier("haarcascade_frontalface_default.xml")
while True:
time.sleep(0.1)
if(not(q.empty())):
if(embedUpdateTimeout == 6):
fetchEmbedded()
embedUpdateTimeout = 0
frame = imutils.resize(q.get(),width=500)
frame = cvtColor(frame, COLOR_BGR2RGB)
frameGray = cvtColor(frame, COLOR_BGR2GRAY)
rects = detector.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),flags=CASCADE_SCALE_IMAGE)
boxes = [(y, x + w, y + h, x) for (x, y, w, h) in rects]
sendBoxes = [(x, y, x + w, y + h) for (x, y, w, h) in rects]
encodings = face_recognition.face_encodings(frame, boxes)
names = []
for encoding in encodings:
matches = face_recognition.compare_faces(embeddedDB["encodings"],encoding)
name = "Unknown"
if True in matches:
matchedIdxs = [i for (i, b) in enumerate(matches) if b]
counts = {}
for i in matchedIdxs:
name = embeddedDB["names"][i]
counts[name] = counts.get(name, 0) + 1
name = max(counts, key=counts.get)
names.append(name)
if(len(names) > 0):
eventLogger.eventLogger(frame, sendBoxes[0], names[0],postURL)
if(names[0] != "Unknown"):
lockCtrl.unlock()
else:
eventLogger.eventLogger(frame, None, None,postURL)
embedUpdateTimeout+=1