-
Notifications
You must be signed in to change notification settings - Fork 0
/
eigenface.py
40 lines (32 loc) · 1.53 KB
/
eigenface.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
import cv2
import os
classifierFace = cv2.CascadeClassifier("classifiers\\haar\\haarcascade_frontalface_default.xml")
eigenface = cv2.face.EigenFaceRecognizer_create(num_components=50, threshold=1)
eigenface.read("classifiers\\classifierEigenface.yml")
height, width = 220, 220
webcam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
while True:
isConnected, image = webcam.read()
if isConnected:
imageConverted = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
faceDetected = classifierFace.detectMultiScale(imageConverted, scaleFactor=1.5, minSize=(30, 30))
for (x, y, w, h) in faceDetected:
faceImage = cv2.resize(imageConverted[y:y + h, x:x + w], (width, height))
faceId, accuracy = eigenface.predict(faceImage)
faceName = ''
if faceId == 1:
color = (0, 255, 51)
faceName = 'THIAGO DE BONIS'
elif faceId == 2:
color = (0, 0, 255)
faceName = 'JANE RICHA'
else:
color = (0, 0, 255)
faceName = 'UNKNOWN'
cv2.rectangle(image, (x, y), (x + w, y + h), color, 2)
cv2.putText(image, f'ID: {faceId}', (x, y + (h + 30)), cv2.FONT_HERSHEY_PLAIN, 1, color)
cv2.putText(image, f'NAME: {faceName}', (x, y + (h + 50)), cv2.FONT_HERSHEY_PLAIN, 1, color)
cv2.putText(image, f'ACCURACY: {accuracy}', (x, y + (h + 70)), cv2.FONT_HERSHEY_PLAIN, 1, color)
cv2.imshow("FACE", image)
if cv2.waitKey(1) == ord('q'):
break