Working with single face
This commit is contained in:
parent
e004dc434f
commit
83f3765bf1
66
main.py
66
main.py
@ -4,6 +4,7 @@ from picamera import PiCamera
|
||||
from io import BytesIO
|
||||
import cv2
|
||||
import numpy as np
|
||||
import os
|
||||
|
||||
def init():
|
||||
go = True
|
||||
@ -31,35 +32,72 @@ def init():
|
||||
print('closing')
|
||||
|
||||
def start( camera, face_cascade):
|
||||
j = 0
|
||||
while True:
|
||||
try:
|
||||
print('Taking photo')
|
||||
camera.capture('img.jpg')
|
||||
print('Photo Captured')
|
||||
#try:
|
||||
camera.capture('img.jpg')
|
||||
img = cv2.imread('img.jpg',1)
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
print('Photo Converted')
|
||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
||||
print('Photo Processed')
|
||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
||||
i = 0;
|
||||
for (x,y,w,h) in faces:
|
||||
i = i + 1
|
||||
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
|
||||
#cv2.imwrite('result.jpg', gray)
|
||||
j = j + 1
|
||||
|
||||
roi_gray = gray[y:y+h, x:x+w]
|
||||
roi_color = img[y:y+h, x:x+w]
|
||||
try:
|
||||
cv2.imwrite('train/'+str(j)+'.jpg',roi_color)
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
print('Drawing on face ', i)
|
||||
|
||||
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
|
||||
cv2.putText(img, recognize(roi_gray), (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
|
||||
cv2.imshow('image',img)
|
||||
cv2.waitKey(100)
|
||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||
cv2.destroyAllWindows()
|
||||
break
|
||||
|
||||
except:
|
||||
print('[ Error ] Unexpected exception')
|
||||
print('Closing')
|
||||
break
|
||||
#except:
|
||||
#print('[ Error ] Unexpected exception')
|
||||
#print('Closing')
|
||||
#break
|
||||
|
||||
def train(number):
|
||||
|
||||
train_path = 'train/'
|
||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||
images = []
|
||||
labels = []
|
||||
image_names = os.listdir(train_path)
|
||||
for image_name in image_names:
|
||||
if image_name.startswith("."):
|
||||
continue;
|
||||
image_path = train_path + image_name
|
||||
image = cv2.imread(image_path)
|
||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||
images.append(gray)
|
||||
labels.append(number)
|
||||
print('training')
|
||||
face_recognizer.train(images, np.array(labels))
|
||||
face_recognizer.save('faces/hesham')
|
||||
|
||||
|
||||
def recognize(image):
|
||||
|
||||
subjects = ['','Hesham','Unknown']
|
||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||
face_recognizer.load('faces/hesham')
|
||||
img = image.copy()
|
||||
label= face_recognizer.predict(img)
|
||||
if label[0] == 1:
|
||||
return 'Hesham'
|
||||
else:
|
||||
return 'Unknown'
|
||||
|
||||
|
||||
init()
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user