-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (89 loc) · 2.54 KB
/
main.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
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
import time
import numpy as np
import cv2
import asyncio
import firebase_admin
from firebase_admin import credentials,firestore,storage
from os import listdir
from os.path import isfile, join
from random import seed
from random import random
from playsound import playsound
seed(1)
cred = credentials.Certificate("./servicekey.json")
firebase_admin.initialize_app(cred)
# {
# 'storageBucket':'gs://sentryhome-c84fb.appspot.com'
# })
db=firestore.client()
ref=db.collection("Options").document("isArmed")
doc=None
face_no=0
# from pykeyboard import PyKeyboard
faceCascade = cv2.CascadeClassifier("haarcascade_default.xml")
def fire_and_forget(f):
def wrapped(*args, **kwargs):
return asyncio.get_event_loop().run_in_executor(None, f, *args, *kwargs)
return wrapped
@fire_and_forget
def get_data():
global doc
try:
doc=ref.get()
# print(doc.to_dict())
# return await True
except:
print("not found")
@fire_and_forget
def play_scream(number):
onlyfiles = [f for f in listdir("./Scream") if isfile(join("./Scream", f))]
# print(onlyfiles)
screams=[]
for i in range(number):
val=1+int(random()*number)
# print(val)
screams.append(onlyfiles[val])
print("./Scream/"+onlyfiles[val])
playsound("./Scream/"+onlyfiles[val])
def face_detect(orig):
global face_no
normalized=cv2.normalize(orig,normalized,1,255,cv2.NORM_MINMAX)
gray=cv2.cvtColor(normalized,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(20, 20),
)
if len(faces)>0:
if not len(faces) == face_no:
face_no=len(faces)
asyncio.ensure_future(play_scream(len(faces)))
for (x, y, w, h) in faces:
cv2.rectangle(orig, (x, y), (x+w, y+h), (0, 255, 0), 2)
# cv2.imshow("face "+str(i),orig[y:y+h,x:x+h])
# cv2.destroyWindow("face "+str(i+1))
return orig
def show_fb():
cam=cv2.VideoCapture(0)
# doc=None
global doc
try:
doc=ref.get()
print(doc.to_dict())
except:
print("not found")
while True:
ret, frame=cam.read()
if(doc.to_dict()["check"]):
cv2.imshow("FrameBuffer2", face_detect(frame))
else:
cv2.imshow("FrameBuffer2",frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# if("ALL COMPLETED"==asyncio.ALL_COMPLETED):
asyncio.ensure_
def main():
show_fb()
if __name__ == '__main__':
main()