to be tested

This commit is contained in:
HeshamTB 2018-11-22 03:56:09 +03:00
parent 20270cc73a
commit 9f75eda8a5
3 changed files with 26 additions and 28 deletions

View File

@ -20,6 +20,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Compile Include="main.py" /> <Compile Include="main.py" />
<Compile Include="Trainer.py" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" /> <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
</Project> </Project>

View File

@ -3,26 +3,29 @@ from io import BytesIO
import cv2 import cv2
import numpy as np import numpy as np
def train(label, path): def addPerson(label, ImagesPath, newFileName, oldFileName):
try: try:
#faceFilePath = '/faces/hesham-saeed2'
faceFile = '/faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer() face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load('/faces/hesham-saeed2') 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(path) 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 = train_path + 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)) face_recognizer.train(images, np.array(labels))
face_recognizer.save('/faces/'+newFileName)

36
main.py
View File

@ -12,11 +12,8 @@ try:
except Exception as ex: except Exception as ex:
print('[ Error ] some depandincies are missing\n'+ str(ex.args)) print('[ Error ] some depandincies are missing\n'+ str(ex.args))
def init(): def init():
go = True go = True
#OpenShifter()
#if Sensors():
print('Initilizing Camera and cascade components..') print('Initilizing Camera and cascade components..')
try: try:
camera = PiCamera() camera = PiCamera()
@ -115,25 +112,22 @@ def recognize(image):
return 'Saeed' return 'Saeed'
else: else:
return 'Unknown' return 'Unknown'
def Sensors():
while 1:
try:
line = ser.readline()
print(line)
if 'A' in line:
return True
else:
print('Sensors not OK..')
except:
print('[ Error ] Can not connect with arduino. Retrying..')
def OpenShifter(): def OpenShifter():
ser = serial.Serial('/dev/ttyUSB1',9600) try:
ser.open() ser = serial.Serial('/dev/ttyUSB1',9600)
ser.write('O') ser.open()
ser.close() ser.write('O')
ser.close()
except Exception as ex:
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 .. \n Trying /dev/ttyUSB0')
try:
ser = serial.Serial('/dev/ttyUSB0',9600)
ser.open()
ser.write('O')
ser.close()
except Exception as ex:
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB0')
init() init()