forked from 1Crazymoney/Face-Detection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
127 lines (90 loc) · 3.28 KB
/
app.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import time
import datetime
from threading import Thread
import cv2 as cv
from deepface import DeepFace
global faceMatch
global FaceData
global CheckFlag
CheckFlag = True
faceMatch = False
FaceData = None
sourceImage = cv.imread("source.png")
sourceName = "Will Smith"
cap = cv.VideoCapture(0)
def checkFace(frame):
"""
This function takes an image frame and
check for the face and compare the face with the source image
"""
global faceMatch
global CheckFlag
global FaceData
try:
FaceData = DeepFace.verify(frame, sourceImage)
faceMatch = FaceData['verified']
FaceData = FaceData["facial_areas"]
except Exception as e:
faceMatch = False
CheckFlag = True
# def AnalizyeFace(frame):
# Analizye's face motion and race and gender
# global Counter
# Counter += 1
# if Counter == 60:
# Counter = 0
# result = DeepFace.analyze(frame)[0]
# print(json.dumps(result, indent=1))
timer = time.time()
imageCounter = 0
FPS = 0
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
if CheckFlag:
counter = 0
print(f"Thread Started !{imageCounter}")
Thread(target=checkFace, args=(frame.copy(), )).start()
CheckFlag = False
if faceMatch:
frame = cv.putText(img=frame,
org=(0, 690) ,text="Face Match", color=(0, 0, 255),
fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=3, fontScale=1.5)
frame = cv.rectangle(frame, (0,698), (300,698), color=(0 , 0, 255), thickness=2)
x = "img1"
startPoint = (FaceData[x]['x'], FaceData[x]['y'],)
endPoint = (FaceData[x]['x']+FaceData[x]['w'], FaceData[x]['y']+FaceData[x]['h'],)
# draw a rectangle around the face in the frame
frame = cv.rectangle(frame, startPoint, endPoint, color=(0 , 255, 0), thickness=3)
# add person name to frame
frame = cv.putText(img=frame,
org=(endPoint[0]-100, endPoint[1]+20), text=sourceName,
color=(0, 0, 255), fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=2, fontScale=0.6)
frame = cv.putText(img=frame,
org=(endPoint[0]-100, endPoint[1]+40), text=f"x:{startPoint[0]},y:{startPoint[1]}",
color=(0, 0, 255), fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=1, fontScale=0.6)
else:
frame = cv.putText(img=frame,
org =(0, 690) , text="No Match", color=(0, 255, 0),
fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=3, fontScale=1.5)
frame = cv.rectangle(frame, (0,698), (225, 698), color=(0 , 255, 0), thickness=2)
frame = cv.putText(img=frame,
org=(0, 715), text=f"{datetime.datetime.utcnow()}", color=(0, 0, 255),
fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=1, fontScale=0.6)
# FPS
imageCounter += 1
now = time.time()
if now - timer > 1:
FPS = imageCounter / 1
imageCounter = 0
timer = time.time()
frame = cv.putText(img=frame,
org=(0, 20), text=f"FPS:{FPS}",
color=(0, 0, 255), fontFace=cv.FONT_HERSHEY_DUPLEX, thickness=1, fontScale=0.6)
frame = cv.resize(frame, (920, 640))
cv.imshow("video", frame)
if cv.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv.destroyAllWindows()