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-12 10:46:29 +01:00
|
|
|
|
2018-11-18 10:47:06 +01:00
|
|
|
try:
|
|
|
|
import numpy as np
|
|
|
|
from picamera import PiCamera
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
except Exception as ex:
|
|
|
|
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
|
|
|
|
|
2018-11-19 14:12:30 +01:00
|
|
|
|
2018-11-12 10:46:29 +01:00
|
|
|
def init():
|
2018-11-12 16:16:58 +01:00
|
|
|
go = True
|
2018-11-18 14:22:08 +01:00
|
|
|
#OpenShifter()
|
|
|
|
#if Sensors():
|
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)
|
|
|
|
sleep(2)
|
|
|
|
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-12 10:46:29 +01:00
|
|
|
while True:
|
2018-11-18 14:22:08 +01:00
|
|
|
#try:
|
|
|
|
camera.capture('img.jpg')
|
|
|
|
img = cv2.imread('img.jpg',1)
|
|
|
|
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
|
|
|
|
roi_gray = gray[y:y+h, x:x+w]
|
|
|
|
roi_color = img[y:y+h, x:x+w]
|
|
|
|
#try:
|
|
|
|
#cv2.imwrite('train/'+str(j)+'.jpg',img)
|
|
|
|
#print(j)
|
|
|
|
#except Exception as ex:
|
|
|
|
#print(ex)
|
|
|
|
#print('Drawing on face ', i)
|
2018-11-19 14:12:30 +01:00
|
|
|
person = recognize(roi_gray)
|
|
|
|
if person == 'Hesham' or person == 'Saeed':
|
|
|
|
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)
|
|
|
|
cv2.waitKey(100)
|
|
|
|
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 train(number):
|
|
|
|
|
|
|
|
train_path = 'train/'
|
|
|
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
|
|
|
images = []
|
|
|
|
labels = []
|
|
|
|
image_names = os.listdir(train_path)
|
|
|
|
for image_name in image_names:
|
|
|
|
if image_name.startswith("."):
|
|
|
|
continue;
|
|
|
|
image_path = train_path + image_name
|
|
|
|
image = cv2.imread(image_path)
|
|
|
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
|
|
|
images.append(gray)
|
|
|
|
labels.append(number)
|
|
|
|
print('training')
|
|
|
|
face_recognizer.train(images, np.array(labels))
|
|
|
|
face_recognizer.save('faces/hesham')
|
2018-11-12 10:46:29 +01:00
|
|
|
|
2018-11-14 23:00:35 +01:00
|
|
|
|
|
|
|
def recognize(image):
|
|
|
|
|
2018-11-18 14:22:08 +01:00
|
|
|
subjects = ['','Hesham','Saeed']
|
2018-11-14 23:00:35 +01:00
|
|
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
2018-11-19 14:12:30 +01:00
|
|
|
face_recognizer.load('faces/Hesham-Saeed2')
|
2018-11-14 23:00:35 +01:00
|
|
|
img = image.copy()
|
|
|
|
label= face_recognizer.predict(img)
|
2018-11-18 14:22:08 +01:00
|
|
|
print(label)
|
2018-11-19 14:12:30 +01:00
|
|
|
if label[0] == 1 and int(label[1]) < 120:
|
2018-11-14 23:00:35 +01:00
|
|
|
return 'Hesham'
|
2018-11-19 14:12:30 +01:00
|
|
|
elif label[0] == 2 and int(label[1]) < 90:
|
2018-11-18 14:22:08 +01:00
|
|
|
return 'Saeed'
|
|
|
|
else:
|
2018-11-14 23:00:35 +01:00
|
|
|
return 'Unknown'
|
2018-11-18 14:22:08 +01:00
|
|
|
|
|
|
|
def Sensors():
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
try:
|
|
|
|
line = ser.readline()
|
|
|
|
print(line)
|
|
|
|
if 'A' in line:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
print('Sensors not OK..')
|
2018-11-12 10:46:29 +01:00
|
|
|
|
2018-11-18 14:22:08 +01:00
|
|
|
except:
|
|
|
|
print('[ Error ] Can not connect with arduino. Retrying..')
|
|
|
|
|
|
|
|
def OpenShifter():
|
2018-11-19 14:12:30 +01:00
|
|
|
ser = serial.Serial('/dev/ttyUSB1',9600)
|
|
|
|
ser.open()
|
2018-11-18 14:22:08 +01:00
|
|
|
ser.write('O')
|
2018-11-19 14:12:30 +01:00
|
|
|
ser.close()
|
2018-11-14 23:00:35 +01:00
|
|
|
|
2018-11-18 14:22:08 +01:00
|
|
|
init()
|