FaceRecognition-python/main.py
2018-12-12 21:59:05 +03:00

141 lines
3.2 KiB
Python

from time import sleep
from PIL import Image
import os
import serial
import sys
import json
import datetime
try:
import numpy as np
from picamera import PiCamera
from picamera.array import PiRGBArray
import cv2
except Exception as ex:
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
sys.exit()
def init():
go = True
print('Initilizing..')
try:
camera = PiCamera()
camera.resolution = (640, 480)
sleep(0.2)
print('[ OK ] Camera')
except:
print('[ Error ] Can not initialize PiCamera')
go = False
try:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sleep(1)
print('[ OK ] CascadeClassifier')
except:
print('[ Error ] Can not load cascade File')
go = False
try:
os.mkdir('unknown')
except OSError as ex:
print('Found (unknown) folder')
if (go):
print('Starting Photo loop..')
print('Known people are '+str(loadNames()))
start(camera, face_cascade)
else:
print('closing')
def start( camera, face_cascade):
j = 0
rawCapture = PiRGBArray(camera)
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load('faces/h')
names = loadNames()
while 1:
try:
camera.capture(rawCapture, format="bgr")
except:
print('[ Error ] Can not capture image. Restarting..')
init()
img = rawCapture.array
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 += 1
j += 1
try:
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
person = recognize(roi_gray, face_recognizer, names)
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),1)
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (0,255,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('unknown/'+str(datetime.datetime.now())+'.png')
except Exception as ex :
print('[ Error ] Saving unkown fail')
print(ex)
else:
print('Found '+person)
OpenShifter()
cv2.imshow('image',img)
cv2.waitKey(1)
rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
def recognize(image,face_recognizer, names):
try:
img = image.copy()
label= face_recognizer.predict(img)
except:
print('[ Error ] Error in Recognize() function')
if label[1] > 70:
print(str(label))
return 'unknown'
else:
print(str(label) + ' >>'+names[label[0]])
return names[label[0]]
#def DrawSquares(image,gray,faces,color):
def OpenShifter():
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 ..')
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():
try:
with open("faces/names.json", "r") as read_file:
data = json.load(read_file)
return list(data)
except:
print('[ Error ] Error in loadNames() function')
init()