2018-11-12 10:46:29 +01:00
|
|
|
|
|
|
|
from time import sleep
|
2018-12-04 18:21:15 +01:00
|
|
|
from PIL import Image
|
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-12-04 20:03:00 +01:00
|
|
|
import datetime
|
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-12-04 21:31:23 +01:00
|
|
|
print('Initilizing..')
|
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-12-04 21:31:23 +01:00
|
|
|
try:
|
|
|
|
os.mkdir('unknown')
|
|
|
|
except OSError as ex:
|
|
|
|
print('Found (unknown) folder')
|
|
|
|
|
2018-11-12 11:26:55 +01:00
|
|
|
if (go):
|
|
|
|
print('Starting Photo loop..')
|
2018-11-30 11:13:21 +01:00
|
|
|
print('Known people are '+str(loadNames()))
|
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-30 11:45:40 +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-30 11:48:06 +01:00
|
|
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
2018-11-30 22:05:38 +01:00
|
|
|
face_recognizer.load('faces/h')
|
2018-11-30 11:54:17 +01:00
|
|
|
names = loadNames()
|
2018-11-23 20:28:15 +01:00
|
|
|
while 1:
|
2018-11-30 11:45:40 +01:00
|
|
|
try:
|
|
|
|
camera.capture(rawCapture, format="bgr")
|
|
|
|
except:
|
|
|
|
print('[ Error ] Can not capture image. Restarting..')
|
|
|
|
init()
|
2018-11-23 14:23:21 +01:00
|
|
|
img = rawCapture.array
|
2018-11-23 20:28:15 +01:00
|
|
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
2018-11-30 11:13:21 +01:00
|
|
|
faces = face_cascade.detectMultiScale(gray, 1.3,5)
|
2018-12-12 19:56:33 +01:00
|
|
|
i = 0
|
2018-11-18 14:22:08 +01:00
|
|
|
for (x,y,w,h) in faces:
|
|
|
|
i += 1
|
2018-11-23 20:28:15 +01:00
|
|
|
j += 1
|
2018-12-04 18:21:15 +01:00
|
|
|
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)
|
2018-12-04 20:03:00 +01:00
|
|
|
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)
|
2018-12-04 18:21:15 +01:00
|
|
|
except:
|
|
|
|
print('[ Error ] Recognition fail')
|
2018-11-30 11:37:53 +01:00
|
|
|
if person == 'unknown':
|
|
|
|
print('Found unknown person')
|
2018-12-04 18:21:15 +01:00
|
|
|
try:
|
|
|
|
RGBImage = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
|
|
|
image = Image.fromarray(RGBImage)
|
2018-12-04 20:03:00 +01:00
|
|
|
image.save('unknown/'+str(datetime.datetime.now())+'.png')
|
|
|
|
except Exception as ex :
|
2018-12-04 18:21:15 +01:00
|
|
|
print('[ Error ] Saving unkown fail')
|
2018-12-04 20:03:00 +01:00
|
|
|
print(ex)
|
2018-11-30 11:37:53 +01:00
|
|
|
else:
|
|
|
|
print('Found '+person)
|
2018-11-19 14:12:30 +01:00
|
|
|
OpenShifter()
|
2018-11-18 14:22:08 +01:00
|
|
|
cv2.imshow('image',img)
|
2018-12-04 18:21:15 +01:00
|
|
|
cv2.waitKey(1)
|
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-12-04 21:31:23 +01:00
|
|
|
|
2018-11-14 23:00:35 +01:00
|
|
|
|
2018-11-30 11:54:17 +01:00
|
|
|
def recognize(image,face_recognizer, names):
|
|
|
|
|
2018-11-30 11:45:40 +01:00
|
|
|
try:
|
|
|
|
img = image.copy()
|
|
|
|
label= face_recognizer.predict(img)
|
|
|
|
except:
|
|
|
|
print('[ Error ] Error in Recognize() function')
|
2018-11-30 22:05:38 +01:00
|
|
|
if label[1] > 70:
|
2018-12-04 21:31:23 +01:00
|
|
|
print(str(label))
|
2018-11-23 20:28:15 +01:00
|
|
|
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-12-12 19:59:05 +01:00
|
|
|
|
|
|
|
#def DrawSquares(image,gray,faces,color):
|
|
|
|
|
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-12-04 18:38:13 +01:00
|
|
|
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 ..')
|
|
|
|
|
2018-11-23 20:28:15 +01:00
|
|
|
def loadNames():
|
2018-11-30 11:45:40 +01:00
|
|
|
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')
|
2018-11-23 20:28:15 +01:00
|
|
|
|
2018-11-14 23:00:35 +01:00
|
|
|
|
2018-11-18 14:22:08 +01:00
|
|
|
init()
|