Updated-Teted Trainer
This commit is contained in:
parent
50bb02ab67
commit
ecd7ad2c48
47
Trainer.py
47
Trainer.py
@ -2,30 +2,33 @@ from time import sleep
|
|||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
import cv2
|
import cv2
|
||||||
import numpy as np
|
import numpy as np
|
||||||
|
import os
|
||||||
|
|
||||||
def addPerson(label, ImagesPath, newFileName, oldFileName):
|
def addPerson(label, ImagesPath, newFileName, oldFileName):
|
||||||
try:
|
try:
|
||||||
#faceFilePath = '/faces/hesham-saeed2'
|
#faceFilePath = '/faces/hesham-saeed2'
|
||||||
faceFile = '/faces/'+ oldFileName
|
faceFile = 'faces/'+ oldFileName
|
||||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||||
face_recognizer.load(faceFile)
|
face_recognizer.load(faceFile)
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
|
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
|
||||||
images = []
|
images = []
|
||||||
labels = []
|
labels = []
|
||||||
image_names = os.listdir(ImagesPath)
|
image_names = os.listdir(ImagesPath)
|
||||||
for image_name in image_names:
|
for image_name in image_names:
|
||||||
if image_name.startswith('.'):
|
if image_name.startswith('.'):
|
||||||
continue;
|
continue;
|
||||||
image_path = train_path + image_name
|
image_path = ImagesPath + image_name
|
||||||
image = cv2.imread(image_path)
|
image = cv2.imread(image_path)
|
||||||
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
||||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
||||||
for (x,y,w,h) in faces:
|
for (x,y,w,h) in faces:
|
||||||
images.append(gray[y:y+h, x:x+w])
|
images.append(gray[y:y+h, x:x+w])
|
||||||
labels.append(label)
|
labels.append(label)
|
||||||
face_recognizer.train(images, np.array(labels))
|
print('Added '+ image_name)
|
||||||
face_recognizer.save('/faces/'+newFileName)
|
face_recognizer.update(images, np.array(labels))
|
||||||
|
face_recognizer.save('faces/'+newFileName)
|
||||||
|
|
||||||
|
|
||||||
|
addPerson(3,'train/','test1','Hesham-Saeed2')
|
||||||
|
30
main.py
30
main.py
@ -47,7 +47,7 @@ def start( camera, face_cascade):
|
|||||||
camera.capture('img.jpg')
|
camera.capture('img.jpg')
|
||||||
img = cv2.imread('img.jpg',1)
|
img = cv2.imread('img.jpg',1)
|
||||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
|
||||||
i = 0;
|
i = 0;
|
||||||
for (x,y,w,h) in faces:
|
for (x,y,w,h) in faces:
|
||||||
i += 1
|
i += 1
|
||||||
@ -102,7 +102,7 @@ def recognize(image):
|
|||||||
|
|
||||||
subjects = ['','Hesham','Saeed']
|
subjects = ['','Hesham','Saeed']
|
||||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||||
face_recognizer.load('faces/Hesham-Saeed2')
|
face_recognizer.load('faces/test1')
|
||||||
img = image.copy()
|
img = image.copy()
|
||||||
label= face_recognizer.predict(img)
|
label= face_recognizer.predict(img)
|
||||||
print(label)
|
print(label)
|
||||||
@ -110,24 +110,26 @@ def recognize(image):
|
|||||||
return 'Hesham'
|
return 'Hesham'
|
||||||
elif label[0] == 2 and int(label[1]) < 90:
|
elif label[0] == 2 and int(label[1]) < 90:
|
||||||
return 'Saeed'
|
return 'Saeed'
|
||||||
|
elif label[0] == 3 and int(label[1]) < 120:
|
||||||
|
return 'Hesham2'
|
||||||
else:
|
else:
|
||||||
return 'Unknown'
|
return 'Unknown'
|
||||||
|
|
||||||
def OpenShifter():
|
def OpenShifter():
|
||||||
try:
|
try:
|
||||||
ser = serial.Serial('/dev/ttyUSB1',9600)
|
ser = serial.Serial('/dev/ttyUSB1',9600)
|
||||||
ser.open()
|
#ser.open()
|
||||||
ser.write('O')
|
ser.write('O')
|
||||||
ser.close()
|
ser.close()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 .. \n Trying /dev/ttyUSB0')
|
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 .. \n Trying /dev/ttyUSB0')
|
||||||
try:
|
try:
|
||||||
ser = serial.Serial('/dev/ttyUSB0',9600)
|
ser = serial.Serial('/dev/ttyUSB0',9600)
|
||||||
ser.open()
|
ser.open()
|
||||||
ser.write('O')
|
ser.write('O')
|
||||||
ser.close()
|
ser.close()
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB0')
|
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB0')
|
||||||
|
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
from picamera import PiCamera
|
from picamera import PiCamera
|
||||||
from time import sleep
|
from time import sleep
|
||||||
|
import cv2
|
||||||
|
|
||||||
def capture(path):
|
def capture(path):
|
||||||
try:
|
try:
|
||||||
@ -14,10 +15,14 @@ def capture(path):
|
|||||||
sleep(2)
|
sleep(2)
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Can not initialize PiCamera')
|
print('[ Error ] Can not initialize PiCamera')
|
||||||
for i in range(0, choice):
|
for i in range(1, choice):
|
||||||
camera.capture(path+str(i)+'.jpg')
|
camera.capture(path+str(i)+'.jpg')
|
||||||
|
photo = cv2.imread(path+str(i)+'.jpg',1)
|
||||||
|
cv2.imshow('Photo',photo)
|
||||||
print('Captured '+str(i)+'\nPath ('+path+str(i)+'.jpg)')
|
print('Captured '+str(i)+'\nPath ('+path+str(i)+'.jpg)')
|
||||||
|
cv2.waitKey(100)
|
||||||
sleep(1)
|
sleep(1)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
capture('test/')
|
print('Done!')
|
||||||
|
capture('train/')
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user