-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathidentify.py
112 lines (84 loc) · 3.2 KB
/
identify.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
104
105
106
107
108
109
110
111
112
import cv2
import numpy as np
import sys
import requests
import os
import alerts
import datetime
import time
def captures(names):
file1=open("admin_files/logs.txt","a+")
file2=open("admin_files/mobile_no.txt","r")
data=file2.read()
file2.close()
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer.yml')
cascadePath = 'haarcascade_frontalface_default.xml'
faceCascade = cv2.CascadeClassifier(cascadePath)
font = cv2.FONT_HERSHEY_SIMPLEX
id = 0
cam = cv2.VideoCapture(0)
#Variable to counter valid and invalid
valid=0
invalid=0
flag=0
while (flag==0):
ret, img =cam.read()
img = cv2.flip(img, 1)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor = 1.2,
minNeighbors = 3,
minSize=(10,10)
)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y), (x+w,y+h), (0,0,255), 2)
id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
text=""
if(confidence<53):
valid+=1
text=names[id]
if(valid>=60):
cv2.putText(img, str("Logged to system"), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.putText(img, str("Paused for few minutes.."), (x+5,y+5+270), font, 1, (255,255,255), 2)
cv2.imshow('camera',img)
if cv2.waitKey(1) &0xFF == ord('q'):
flag=1
break
x=datetime.datetime.now()
x=x.strftime("%m/%d/%Y, %H:%M:%S")
msg="\n "+text+" logged at "+x
file1.write(msg)
valid=0
invalid=0
time.sleep(3)
else:
cv2.putText(img, str("Detected "+text), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.imshow('camera',img)
if cv2.waitKey(1) &0xFF == ord('q'):
flag=1
break
else:
invalid+=1
if(invalid>=150):
cv2.putText(img, str("Cannot detect the face system will be alerted.."), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.imshow('camera',img)
if cv2.waitKey(1) &0xFF == ord('q'):
flag=1
break
alerts.alert(data)
invalid=0
valid=0
else:
cv2.putText(img, str("Detecting.."), (x+5,y-5), font, 1, (255,255,255), 2)
cv2.imshow('camera',img)
if cv2.waitKey(1) &0xFF == ord('q'):
flag=1
break
cv2.imshow('camera',img)
if cv2.waitKey(1) &0xFF == ord('q'):
break
cam.release()
cv2.destroyAllWindows()
file1.close()