-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
213 lines (182 loc) · 8.25 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#!/usr/bin/python3
import numpy as np
import cv2
import math
from statistics import variance
import itertools
import time
import argparse
import sys
face_cascade = cv2.CascadeClassifier('models/haarcascade_frontalface_alt.xml')
eye_cascade = cv2.CascadeClassifier('models/haarcascade_eye.xml')
#eye_cascade = cv2.CascadeClassifier('models/haarcascade_eye_tree_eyeglasses.xml')
mouth_cascade = cv2.CascadeClassifier('models/haarcascade_mcs_mouth.xml')
#eye_cascade = cv2.CascadeClassifier('haarcascade_mcs_lefteye.xml')
scale_factor = 1.2
font = cv2.FONT_ITALIC
font_color = (255, 255, 255)
face_min_neighbors = 3
face_min_size = (100, 100)
face_max_size = (2000, 2000)
eye_min_neighbors = 3
eye_min_size = (50, 50)
eye_max_size = (100, 100)
mouth_min_neighbors = 2
mouth_min_size = (50, 50)
mouth_max_size = (200, 200)
angleslist = []
goodtriangleslist = []
angles = 0
goodblurlist = []
blurlist = []
# Plot
from matplotlib import pyplot as plt
#webcam=True #if working with video file then make it 'False'
#webcam=False
def is_blurred(image, threshold=100):
# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Calculate the variance of Laplacian
variance = cv2.Laplacian(gray, cv2.CV_64F).var()
# Check if the variance is below the threshold
return variance < threshold
def calculate_distance(point1, point2):
x1, y1 = point1
x2, y2 = point2
distance = math.sqrt((x2 - x1)**2 + (y2 - y1)**2)
return distance
def calculate_angles(points):
angles = []
for i in range(len(points)):
p1, p2, p3 = points[i], points[(i + 1) % len(points)], points[(i + 2) % len(points)]
# Calculate vectors
v1 = (p1[0] - p2[0], p1[1] - p2[1])
v2 = (p3[0] - p2[0], p3[1] - p2[1])
# Calculate dot product and magnitudes
dot_product = v1[0] * v2[0] + v1[1] * v2[1]
magnitude_v1 = math.sqrt(v1[0]**2 + v1[1]**2)
magnitude_v2 = math.sqrt(v2[0]**2 + v2[1]**2)
angle_radians = math.acos(min(max(dot_product / (magnitude_v1 * magnitude_v2), -1.0), 1.0))
angle_degrees = math.degrees(angle_radians)
angles.append(angle_degrees)
return angles
def detect(input_file_name, webcam):
if webcam:
video_cap = cv2.VideoCapture(0) # use 0,1,2..depanding on your webcam
else:
video_cap = cv2.VideoCapture(input_file_name)
while True:
# Capture frame-by-frame
ret, img = video_cap.read()
if not ret:
video_cap.release()
break
#converting to gray image for faster video processing
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = face_cascade.detectMultiScale(gray, scaleFactor=scale_factor, minNeighbors=face_min_neighbors, minSize=face_min_size, maxSize=face_max_size)
# if at least 1 face detected
if len(rects) >= 0:
artifactlist=[]
artifactlist_mouth = []
# Draw a rectangle around the faces
for (x, y, w, h) in rects:
# Crop image for blur check
crop_img = img[y:y+h, x:x+w]
#cv2.namedWindow("Display1", cv2.WINDOW_AUTOSIZE)
#cv2.imshow('Display1', crop_img)
if is_blurred(crop_img) == True:
goodblurlist.append(is_blurred(crop_img))
else:
blurlist.append(is_blurred(crop_img))
time.sleep(0.1)
color = (0, 0, 255)
fcenter = (int(x+(w/2)), int(y+(h/2)))
rectangle_text = cv2.putText(img, str(fcenter), fcenter, font, 0.5, font_color)
rectangle_circle = cv2.circle(rectangle_text, fcenter , 5, color, -1)
rectangle_img = cv2.rectangle(rectangle_circle, (x, y), (x + w, y + h), (0, 255, 255), 2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = rectangle_img[y:y+h, x:x+w]
artifactlist.append(fcenter)
# TODO: compare histogram
#histogram = cv2.calcHist([roi_color], [0], None, [256], [0, 256])
#print(histogram)
#plt.plot(histogram)
#plt.show()
eyes = eye_cascade.detectMultiScale(roi_gray, scaleFactor=scale_factor, minNeighbors=eye_min_neighbors, minSize=eye_min_size, maxSize=eye_max_size)
for (ex,ey,ew,eh) in eyes:
color = (0, 0, 255)
ecenter = (int(ex+(ew/2)), int(ey+(eh/2)))
if ecenter[1] > int(h/2):
pass
#print("Not valid eye coord: ", ecenter)
else:
#print("Eye center coordinate : ", ecenter)
roi_color = cv2.circle(roi_color, ecenter , 5, color, -1)
roi_color = cv2.putText(roi_color,str(ecenter), ecenter, font, 0.5, font_color)
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(213,255,0),2)
artifactlist.append(ecenter)
mouth = mouth_cascade.detectMultiScale(roi_gray, scaleFactor=scale_factor, minNeighbors=mouth_min_neighbors, minSize=mouth_min_size, maxSize=mouth_max_size)
for (mx, my, mw, mh) in mouth:
color = (0, 100, 244)
mcenter = (int(mx+(mw/2)), int(my+(mh/2)))
if mcenter[1] < int(h/2):
pass
else:
roi_color = cv2.rectangle(roi_color, (mx,my), (mx+mw, my+mh), color, 1)
cv2.circle(roi_color, mcenter, 5, color, -1)
roi_color = cv2.putText(roi_color,str(mcenter), mcenter, font, 0.5, font_color)
cv2.line(roi_color, (int(mx+(mw/2)), int(my+(mh/2))), (int(ex+(ew/2)), int(ey+(eh/2))) ,(59, 0, 225), 1)
artifactlist_mouth.append(mcenter)
# Display the resulting frame
distances = {}
for i, j in itertools.combinations(range(len(artifactlist)), 2):
distance = calculate_distance(artifactlist[i], artifactlist[j])
distances[(i, j)] = distance
try:
if len(artifactlist)>0:
angles = calculate_angles(artifactlist)
'''for i, angle in enumerate(angles, 1):
print(f"Angle {i}: {angle:.2f} degrees")'''
if int(sum(angles)) == 180:
goodtriangleslist.append(angles)
angleslist.append(int(sum(angles)))
else:
pass
#print("Artifactlist is empty!")
except Exception as e:
pass
#print(f"An error has occoured: {e}")
cv2.imshow('Face Detection on Video', img)
#wait for 'c' to close the application
if cv2.waitKey(1) & 0xFF == ord('c'):
break
video_cap.release()
def main():
parser = argparse.ArgumentParser(description='Process some data from an input file or webcam')
parser.add_argument('--webcam', default=False, type=bool, help='Whether to use webcam input (True or False)')
parser.add_argument('input_file', nargs='?', default=None, help='Path to the input file (optional if webcam=True)')
args = parser.parse_args()
if args.webcam is False and args.input_file is None:
print("Error: Please provide either --webcam option or input file.")
parser.print_help()
sys.exit(1)
# Access the input file path using args.input_file
detect(args.input_file, args.webcam)
cv2.destroyAllWindows()
for sublist in goodtriangleslist:
sublist.sort()
transposed_data = list(zip(*goodtriangleslist))
if len(blurlist)==0:
blurresult=0
else:
blurresult = len(goodblurlist)/len(blurlist)
print("Blurred rate: ", "{:.3f}".format(blurresult))
v = []
for i, numbers in enumerate(transposed_data, 1):
var = variance(numbers)
v.append(var)
#print(f"Variance of element {i}: {var:.2f}")
print("Variance median: ", "{:.3f}".format(np.median(v)))
print("Success rate: ", "{:.3f}".format(len(goodtriangleslist)/len(angleslist)))
if __name__ == "__main__":
main()