-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLive Video Testing.py
149 lines (109 loc) · 4.49 KB
/
Live Video Testing.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
import cv2
import cv2.face
import numpy as np
import matplotlib.pyplot as plt
import os
def collect_dataset():
images = []
labels = []
labels_dic = {}
people = [person for person in os.listdir('people/')]
for i,person in enumerate(people):
labels_dic[i] = person
for image in os.listdir('people/' +person):
images.append(cv2.imread('people/' + person + '/' + image, 0))
labels.append(i)
return (images,np.array(labels), labels_dic)
images, labels, labels_dic = collect_dataset()
rec_eig = cv2.face.EigenFaceRecognizer_create()
rec_eig.train(images, labels)
#need at least two people
rec_fisher = cv2.face.FisherFaceRecognizer_create()
rec_fisher.train(images, labels)
rec_lbph = cv2.face.LBPHFaceRecognizer_create()
rec_lbph.train(images, labels)
print("Models Trained Succesfully")
#defining the classes
class FaceDetector(object):
def __init__(self, xml_path):
self.classifier = cv2.CascadeClassifier(xml_path)
def detect(self, image, biggest_only = True) :
scale_factor = 1.2
min_neighbors = 5
min_size = (30,30)
biggest_only = True
flags = cv2.CASCADE_FIND_BIGGEST_OBJECT | \
cv2.CASCADE_DO_ROUGH_SEARCH if biggest_only else \
cv2.CASCADE_SCALE_IMAGE
faces_coord = self.classifier.detectMultiScale(frame, scaleFactor= scale_factor, minNeighbors = min_neighbors,
minSize = min_size, flags = flags)
return faces_coord
def cut_faces(image, faces_coord):
faces = []
for (x,y,w,h) in faces_coord:
w_rm = int(0.2 * w/2)
faces.append( image[y: y+h, x+w_rm: x+w - w_rm])
return faces
def normalize_intensity(images):
images_norm = []
for image in images:
is_color = len(image.shape)==3
if is_color:
image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
images_norm.append(cv2.equalizeHist(image))
return images_norm
def resize(images, size=(100,100)):
images_norm = []
for image in images:
if image.shape < size:
image_norm = cv2.resize(image, size, interpolation = cv2.INTER_AREA)
else:
image_norm = cv2.resize(image, size, interpolation = cv2.INTER_CUBIC)
images_norm.append(image_norm)
return images_norm
def normalize_faces(frame, faces_coord):
faces = cut_faces(frame, faces_coord)
faces = normalize_intensity(faces)
faces = resize(faces)
return faces
def draw_rectangle(image, coords):
for(x, y, w, h) in coords:
w_rm = int(0.2 *w/2)
cv2.rectangle(image, (x+w_rm, y), (x + w - w_rm, y + h), (150, 150, 0), 8)
#initializing camera and detector
detector = FaceDetector("haarcascade_frontalface_default.xml")
webcam = cv2.VideoCapture(0)
print(webcam.isOpened())
#live
cv2.namedWindow('Live Detection', cv2.WINDOW_AUTOSIZE)
while True:
_, frame = webcam.read()
frame = cv2.flip(frame,1)
faces_coord = detector.detect(frame, True) #detect more than one face
if len(faces_coord):
faces = normalize_faces(frame, faces_coord) #norm pipeline
for i, face in enumerate(faces): # for each detected face
collector = cv2.face.StandardCollector_create()
rec_lbph.predict_collect(face, collector)
conf = collector.getMinDist()
pred = collector.getMinLabel()
threshold = 140
if conf < 100:
print("prediction: " + labels_dic[pred].capitalize() + "\nConfidence: " + str(conf))
cv2.putText(frame, labels_dic[pred].capitalize(),
(faces_coord[i][0], faces_coord[i][1] - 10),
cv2.FONT_HERSHEY_PLAIN, 3, (66,53,243), 2)
else:
print("prediction: unknown" )
cv2.putText(frame, 'Unknown',
(faces_coord[i][0], faces_coord[i][1] - 10),
cv2.FONT_HERSHEY_PLAIN, 3, (66,53,243), 2)
draw_rectangle(frame, faces_coord) #rectangle around face
cv2.putText(frame, "ESC to exit", (5, frame.shape[0] - 5),
cv2.FONT_HERSHEY_PLAIN, 1.3, (66,53,243), 2, cv2.LINE_AA)
cv2.imshow("live detection", frame)
if cv2.waitKey(40) & 0xFF ==27:
cv2.destroyAllWindows()
break
#release webcam
webcam.release()