-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcvtest5.py
More file actions
92 lines (67 loc) · 2.9 KB
/
cvtest5.py
File metadata and controls
92 lines (67 loc) · 2.9 KB
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
# experimental face recognition from http://hanzratech.in/2015/02/03/face-recognition-using-opencv.html
import numpy as np
import cv2
import os
from PIL import Image
# get the path to a general face recognition
cascadePath = '/usr/local/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml'
# get the face cascade from the general
faceCascade = cv2.CascadeClassifier(cascadePath)
# define the recognizer to use
recognizer = cv2.face.createLBPHFaceRecognizer()
# define function to prepare training sets
def get_images_and_labels(path):
# append all the absolute image paths in a list image_paths
# we will not read the image with the .sad extension in the training set
# rather, we will use them to test the accuracy of the training
image_paths = [os.path.join(path,f) for f in os.listdir(path) if not f.endswith('.sad')]
# images will contain face images
images = []
# labels will contain the label that is assigned to the image
labels = []
# loop through each image
for image_path in image_paths:
# read the image and convert to greyscale
image_pil = Image.open(image_path).convert('L')
# convert the image format into numpy
image = np.array(image_pil, 'uint8')
# get the label of the image
nbr = int(os.path.split(image_path)[1].split(".")[0].replace("subject",""))
# detect the face in the image
faces = faceCascade.detectMultiScale(image)
# if face is detected, append the face to images and the label to labels
for (x,y,w,h) in faces:
images.append(image[y:y+h, x:x+w])
labels.append(nbr)
cv2.imshow("Adding faces to training set...", image[y:y+h, x:x+w])
cv2.waitKey(50)
# return the images list and labels list
return images, labels
## teach the face reconizer
# get path to yale dataset
path = '/home/kris/Pictures/yalefaces/yalefaces'
# call the get_images_and_labels function
images,labels = get_images_and_labels(path)
cv2.destroyAllWindows()
# perform the training
recognizer.train(images,np.array(labels))
## test the face recognizer
# append the images with the .sad extension into image_paths
image_paths = [os.path.join(path,f) for f in os.listdir(path) if f.endswith('sad')]
# go through each image
for image_path in image_paths:
# open the image and conert
predict_image_pil = Image.open(image_path).convert('L')
predict_image = np.array(predict_image_pil,'uint8')
# find the face in the image to get its ROI
faces = faceCascade.detectMultiScale(predict_image)
for (x,y,w,h) in faces:
# see if you can regognize the face
nbr_predicted,conf = recognizer.predict(predict_image[y:y+h, x:x+w])
nbr_actual = int(os.path.split(image_path)[1].split(".")[0].replace("subject",""))
if nbr_actual == nbr_predicted:
print "{} is correctly recognized with confidence {}".format(nbr_actual,conf)
else:
print "{} is incorrectly recognized as {}".format(nbr_actual,nbr_predicted)
cv2.imshow("Recognizing Face",predict_image[y:y+h,x:x+w])
cv2.waitKey(1000)