-
Notifications
You must be signed in to change notification settings - Fork 0
/
securitycam.py
80 lines (75 loc) · 3.06 KB
/
securitycam.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
from time import sleep
import camm
import cv2
from cv2 import face
import time
import gmail
######
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath)
######
cam = cv2.VideoCapture(0)
def activityDetection(second):
key = 1
while True:
print("detection is workin")
ret, im = cam.read()
if key == 1:
control = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
cv2.imwrite("motion/control.jpg",control)
cv2.imwrite("motion/now.jpg", im)
key = 0
cv2.imwrite("motion/now.jpg", im)
now = cv2.imread("motion/now.jpg", 0)
similarity = cv2.matchTemplate(control, now, cv2.TM_CCOEFF_NORMED)
print("Similarity: " + str(similarity[0][0]))
if (similarity[0][0] >= 0.93):
print("Safe")
else:
print("unSafe")
cv2.imwrite("motion/UFO.jpg",now)
########
confirmation = 0
unknownCounter = 0
w = time.time()
q = w
print("Face Detection is working")
while True:
ret, im = cam.read()
gray = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.2, 5)
for (x, y, w, h) in faces:
cv2.rectangle(im, (x, y), (x + w, y + h), (225, 0, 0), 2)
Id, conf = recognizer.predict(gray[y:y + h, x:x + w])
if (conf < 50):
print(camm.user(Id))
name = camm.user(Id)
if camm.user(Id) == camm.user(Id):
confirmation = confirmation + 1
else:
Id = 0
name = "Unknown"
unknownCounter = unknownCounter + 1
print("Unknown")
cv2.putText(im, str(name), (x, y + h), cv2.FONT_HERSHEY_COMPLEX, 2, (255, 0, 0), 2)
##cv2.imshow('SecurityCam',im)
if confirmation >= 10 and str(camm.user(Id)) == camm.user(Id):
print("You are " + camm.user(Id))
print(time.time() - q)
print("cam stop")
break
elif unknownCounter >= 50:
print("I don't know you!")
calendar = str(time.localtime()[0]) + "." + str(time.localtime()[1]) + "." + str(time.localtime()[2])
cv2.imwrite("unknown/" + calendar + ".jpg",im)
gmail.send_mail(calendar)
print("Face Detection stop")
break
if cv2.waitKey(10) & 0xFF == ord('q'):
break
########
print("Motion Detection sleep for "+ str(second) + " seconds")
sleep(second)
activityDetection(ENTER SLEEP TIME HERE)