-
Notifications
You must be signed in to change notification settings - Fork 0
/
opencv.py
69 lines (58 loc) · 2.05 KB
/
opencv.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
import cv2
import os,numpy
from PIL import Image
labels=[]
images=[]
def rotateImage(image, angle):
# image_center = tuple(numpy.array(image.shape)/2)
(h, w) = image.shape[:2]
center = (w / 2, h / 2)
rot_mat = cv2.getRotationMatrix2D(center=center,angle=angle,scale=1.0)
result = cv2.warpAffine(image, rot_mat, (w, h))
return result
face_cascadePath = "haarcascade_frontalface_default.xml"
eyes_cascadePath= "haarcascade_eye.xml"
faceCascade = cv2.CascadeClassifier(face_cascadePath)
eyes_cascade= cv2.CascadeClassifier(eyes_cascadePath)
recognizer = cv2.createLBPHFaceRecognizer()
# image_pil = Image.open('test/test1.jpg').convert('L')
# img= numpy.array(image_pil, 'uint8')
image_paths = []
for f in os.listdir('subjects'):
if f.endswith('.jpg'):
image_paths.append('subjects/'+f+';'+'1')
print f
for path in image_paths:
path,label=path.split(';')
img = cv2.imread(path)
img =cv2.resize(img,(0,0),fx=0.2,fy=0.2)
img= rotateImage(img,-90)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
images.append(gray[y: y + h, x: x + w])
labels.append(int(label))
# cv2.imshow("Adding faces to traning set...", gray[y: y + h, x: x + w])
# cv2.imshow('img',img)
# cv2.waitKey(50)
# cv2.destroyAllWindows()
recognizer.train(images, numpy.array(labels))
# # paths=open('faces.csv')
# # paths.close()
# # paths=open('faces.csv')
# recognizer.save('train.yml')
# recognizer.load('train.yml')
# for path in paths:
# path,label=path.split(';')
# img = cv2.imread(path,0)
# faces = faceCascade.detectMultiScale(img)
# for (x, y, w, h) in faces:
# predicted,conf=recognizer.predict(img[y: y + h, x: x + w])
# print predicted,conf
# cv2.imshow("Predicting...", img[y: y + h, x: x + w])
# cv2.waitKey(5000)
# cv2.destroyAllWindows()
__author__ = 'gaurav'