Merge branch 'screen-shot-mod' of Hesham/FaceRecognition-python into master

This commit is contained in:
Hesham 2018-12-04 20:55:02 +03:00 committed by HeshamTB
commit 29fa3a3335
4 changed files with 131203 additions and 43 deletions

View File

@ -41,7 +41,6 @@ void loop() {
if (distance < 70) UltraOK = true; if (distance < 70) UltraOK = true;
else UltraOK = false; else UltraOK = false;
if(digitalRead(ProxSensor) == LOW ) IROK = true; if(digitalRead(ProxSensor) == LOW ) IROK = true;
else IROK = false; else IROK = false;
char pi = 'N'; char pi = 'N';

73686
faces/h Normal file

File diff suppressed because it is too large Load Diff

57467
faces/m

File diff suppressed because it is too large Load Diff

92
main.py
View File

@ -1,6 +1,7 @@
from time import sleep from time import sleep
from io import BytesIO from io import BytesIO
from PIL import Image
import os import os
import serial import serial
import sys import sys
@ -42,20 +43,19 @@ def init():
else: else:
print('closing') print('closing')
#else:
#print('Sensors not Satisfied')
def start( camera, face_cascade): def start( camera, face_cascade):
j = 0 j = 0
rawCapture = PiRGBArray(camera) rawCapture = PiRGBArray(camera)
#camera.framerate = 32 face_recognizer = cv2.createLBPHFaceRecognizer()
#rawCapture = PiRGBArray(camera, size=(640, 480)) face_recognizer.load('faces/h')
#for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True): names = loadNames()
while 1: while 1:
#try: try:
#camera.capture('img.jpg') camera.capture(rawCapture, format="bgr")
#img = cv2.imread('img.jpg',1) except:
camera.capture(rawCapture, format="bgr") print('[ Error ] Can not capture image. Restarting..')
init()
img = rawCapture.array img = rawCapture.array
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.3,5)
@ -63,37 +63,40 @@ def start( camera, face_cascade):
for (x,y,w,h) in faces: for (x,y,w,h) in faces:
i += 1 i += 1
j += 1 j += 1
roi_gray = gray[y:y+h, x:x+w] try:
roi_color = img[y:y+h, x:x+w] roi_gray = gray[y:y+h, x:x+w]
person = recognize(roi_gray) roi_color = img[y:y+h, x:x+w]
if not person == 'unknown': person = recognize(roi_gray, face_recognizer, names)
OpenShifter() cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
print('Sending to Arduino!') cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
except:
print('[ Error ] Recognition fail')
if person == 'unknown':
print('Found unknown person')
try:
RGBImage = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = Image.fromarray(RGBImage)
image.save('lastUnknown.png')
except:
print('[ Error ] Saving unkown fail')
else: else:
print('Found unknown face') print('Found '+person)
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2) OpenShifter()
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
cv2.imshow('image',img) cv2.imshow('image',img)
cv2.waitKey(100) cv2.waitKey(1)
rawCapture.truncate(0) rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows() cv2.destroyAllWindows()
break break
#except:
#print('[ Error ] Unexpected exception')
#print('Closing')
#break
def recognize(image): def recognize(image,face_recognizer, names):
names = loadNames()
face_recognizer = cv2.createLBPHFaceRecognizer() try:
face_recognizer.load('faces/m') img = image.copy()
img = image.copy() label= face_recognizer.predict(img)
label= face_recognizer.predict(img) except:
if label[1] > 100: print('[ Error ] Error in Recognize() function')
print(str(label)) if label[1] > 70:
return 'unknown' return 'unknown'
else: else:
print(str(label) + ' >>'+names[label[0]]) print(str(label) + ' >>'+names[label[0]])
@ -106,13 +109,22 @@ def OpenShifter():
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 ..') print('[ Error ] Can not connect to Arduino at /dev/ttyUSB0 ..')
try:
ser = serial.Serial('/dev/ttyUSB1',9600)
ser.open()
ser.write('o')
ser.close()
except:
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
def loadNames(): def loadNames():
try:
with open("faces/names.json", "r") as read_file: with open("faces/names.json", "r") as read_file:
data = json.load(read_file) data = json.load(read_file)
return list(data) return list(data)
except:
print('[ Error ] Error in loadNames() function')
init() init()