-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.py
129 lines (67 loc) · 1.99 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
#!/usr/bin/env python
# coding: utf-8
# # Training
# In[1]:
import time
from os import listdir
import numpy as np
import cv2 as cv
# In[2]:
sample_path = 'Samples/'
sample_names = listdir(sample_path)
# In[3]:
training_data, labels = [], []
for (i, sample_name) in enumerate(sample_names):
image_path = sample_path + sample_name
gray = cv.imread(image_path, cv.IMREAD_GRAYSCALE)
training_data.append(np.array(gray, dtype = np.uint8))
labels.append(i)
training_data = np.array(training_data)
labels = np.array(labels, dtype = np.int32)
# In[4]:
labels[2]
# In[5]:
training_data[0]
# In[6]:
model = cv.face.LBPHFaceRecognizer_create()
# In[7]:
model.train(training_data, labels)
# # Detection
# In[8]:
face_classifier_path = "haarcascade_frontalface_default.xml"
face_classifier = cv.CascadeClassifier(face_classifier_path)
# In[9]:
def face_detector(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)
if faces is ():
return None
else:
for face in faces:
(x, y, w, h) = face
cv.rectangle(image, (x, y), (x + w, y + h), (255, 50, 50), 2)
roi = image[y : y + h, x : x + w]
resized = cv.resize(roi, (128, 128))
return resized
# In[10]:
device = 0
capture = cv.VideoCapture(device)
while capture.isOpened():
ret, frame = capture.read()
face = face_detector(frame)
try:
gray = cv.cvtColor(face, cv.COLOR_BGR2GRAY)
result = model.predict(gray)
confidence = int(result[1])
cv.putText(frame, str(confidence) + '%', (250,250), cv.FONT_HERSHEY_PLAIN, 1, (255, 50, 50), 2)
except:
cv.putText(frame, 'face not found', (250,250), cv.FONT_HERSHEY_PLAIN, 1, (255, 50, 50), 2)
pass
cv.imshow('result',frame)
if cv.waitKey(1) == 13:
break
capture.release()
cv.destroyAllWindows()
# In[11]:
capture.release()
# In[ ]: