This repository was archived by the owner on Dec 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebcamdetect.py
70 lines (63 loc) · 2.4 KB
/
webcamdetect.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
# reading webcam input and recognizing faces using the service.
import myImageLibrary
import callService
import os
import json
import time
import cv2
import base64
import argparse
import sys
FACE_CASCADE = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
SQUARE_SIZE = 96
if os.path.exists("number_to_text_label.json"):
with open("number_to_text_label.json","r") as file:
index_to_name = json.loads(file.read())
else:
print("[WARNING] number_to_text_label.json not found.")
index_to_name = None
def main(args):
parser = argparse.ArgumentParser(description='Call the face recognition service')
parser.add_argument('-u', '--url', default='http://localhost:32773/score', type=str, help='Service URL')
parser.add_argument('-k','--key',type=str,help='service key',default=None)
#key is only needed when deploying to AKS cluster. Local service does not require key. Default = None.
args = parser.parse_args()
key = args.key
url = args.url
monitor_faces(url,key=key,plot=True)
def monitor_faces(url,key=None,plot=True):
cam = cv2.VideoCapture(0)
while True:
time.sleep(500/1000)
ret_val, img = cam.read()
faces, rectangles = myImageLibrary.extract_face(img,FACE_CASCADE, return_rectangles = True)
if plot:
for (x,y,w,h) in rectangles:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),3)
cv2.imshow('Hit escape to quit.',img)
if cv2.waitKey(1) == 27:
break # esc to quit
if len(faces) == 0:
print("No faces detected")
else:
people = []
for face in faces:
face = myImageLibrary.resize_crop(face,SQUARE_SIZE)
face_base64 = myImageLibrary.preprocess(face,normalize=True)
person = callService.callFaceService(face_base64,url=url,key=key)
# variable 'person' is an integer index (can be mapped to a name)
if index_to_name is None:
people.append(person) #adding index number to list
else:
people.append(index_to_name[person]) #adding name to list
print("Detected: ",people)
if plot:
cv2.destroyAllWindows()
cam.release()
if __name__ == '__main__':
from sys import argv
try:
main(argv)
except KeyboardInterrupt:
print("Keyboardinterrupt")
sys.exit()