FaceRecognition-python/main.py

115 lines
2.6 KiB
Python
Raw Normal View History

2018-11-12 10:46:29 +01:00
from time import sleep
from io import BytesIO
2018-11-14 23:00:35 +01:00
import os
2018-11-18 14:22:08 +01:00
import serial
2018-11-23 09:21:11 +01:00
import sys
2018-11-23 20:28:15 +01:00
import json
2018-11-12 10:46:29 +01:00
2018-11-18 10:47:06 +01:00
try:
import numpy as np
from picamera import PiCamera
2018-11-23 14:23:21 +01:00
from picamera.array import PiRGBArray
2018-11-18 10:47:06 +01:00
import cv2
except Exception as ex:
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
2018-11-23 09:21:11 +01:00
sys.exit()
2018-11-18 10:47:06 +01:00
2018-11-12 10:46:29 +01:00
def init():
2018-11-12 16:16:58 +01:00
go = True
2018-11-12 10:46:29 +01:00
print('Initilizing Camera and cascade components..')
2018-11-12 11:26:55 +01:00
try:
camera = PiCamera()
camera.resolution = (640, 480)
2018-11-23 14:23:21 +01:00
sleep(0.2)
2018-11-12 11:26:55 +01:00
print('[ OK ] Camera')
except:
print('[ Error ] Can not initialize PiCamera')
2018-11-12 16:16:58 +01:00
go = False
2018-11-12 11:26:55 +01:00
try:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sleep(1)
print('[ OK ] CascadeClassifier')
except:
print('[ Error ] Can not load cascade File')
2018-11-12 16:16:58 +01:00
go = False
2018-11-12 11:26:55 +01:00
if (go):
print('Starting Photo loop..')
2018-11-12 16:16:58 +01:00
start(camera, face_cascade)
2018-11-18 14:22:08 +01:00
2018-11-12 11:26:55 +01:00
else:
print('closing')
2018-11-18 14:22:08 +01:00
#else:
#print('Sensors not Satisfied')
2018-11-12 10:46:29 +01:00
2018-11-12 16:16:58 +01:00
def start( camera, face_cascade):
2018-11-14 23:00:35 +01:00
j = 0
2018-11-23 14:23:21 +01:00
rawCapture = PiRGBArray(camera)
2018-11-23 20:28:15 +01:00
#camera.framerate = 32
#rawCapture = PiRGBArray(camera, size=(640, 480))
#for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
while 1:
2018-11-18 14:22:08 +01:00
#try:
2018-11-23 20:28:15 +01:00
#camera.capture('img.jpg')
2018-11-23 14:23:21 +01:00
#img = cv2.imread('img.jpg',1)
camera.capture(rawCapture, format="bgr")
img = rawCapture.array
2018-11-23 20:28:15 +01:00
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2,5)
2018-11-18 14:22:08 +01:00
i = 0;
for (x,y,w,h) in faces:
i += 1
2018-11-23 20:28:15 +01:00
j += 1
2018-11-18 14:22:08 +01:00
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
2018-11-19 14:12:30 +01:00
person = recognize(roi_gray)
2018-11-24 17:58:35 +01:00
if person == 'Hesham' or person == 'Saeed':#Change to if 'unknown' elif openShifter
2018-11-19 14:12:30 +01:00
OpenShifter()
print('Sending to Arduino!')
2018-11-18 14:22:08 +01:00
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
2018-11-19 14:12:30 +01:00
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
2018-11-18 14:22:08 +01:00
cv2.imshow('image',img)
2018-11-23 09:21:11 +01:00
cv2.waitKey(100)
2018-11-23 14:23:21 +01:00
rawCapture.truncate(0)
2018-11-18 14:22:08 +01:00
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
2018-11-12 10:46:29 +01:00
2018-11-14 23:00:35 +01:00
#except:
#print('[ Error ] Unexpected exception')
#print('Closing')
#break
def recognize(image):
2018-11-23 20:28:15 +01:00
names = loadNames()
2018-11-14 23:00:35 +01:00
face_recognizer = cv2.createLBPHFaceRecognizer()
2018-11-23 20:28:15 +01:00
face_recognizer.load('faces/m')
img = image.copy()
2018-11-14 23:00:35 +01:00
label= face_recognizer.predict(img)
2018-11-23 20:28:15 +01:00
if label[1] > 120:
return 'unknown'
2018-11-18 14:22:08 +01:00
else:
2018-11-24 17:58:35 +01:00
print(str(label) + ' >>'+names[label[0]])
2018-11-23 20:28:15 +01:00
return names[label[0]]
2018-11-18 14:22:08 +01:00
def OpenShifter():
2018-11-22 01:56:09 +01:00
try:
2018-11-24 15:39:49 +01:00
ser = serial.Serial('/dev/ttyUSB0',9600)
2018-11-24 17:58:35 +01:00
ser.open()
ser.write('o')
2018-11-22 06:41:03 +01:00
ser.close()
2018-11-22 01:56:09 +01:00
except Exception as ex:
2018-11-22 11:14:28 +01:00
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
2018-11-23 20:28:15 +01:00
def loadNames():
with open("faces/names.json", "r") as read_file:
data = json.load(read_file)
return list(data)
2018-11-14 23:00:35 +01:00
2018-11-18 14:22:08 +01:00
init()