FaceRecognition-python/main.py

116 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-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()
face_recognizer.load('faces/m')
2018-11-30 11:54:17 +01:00
names = loadNames()
print('Known people '+ str(names))
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-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-30 11:54:17 +01:00
person = recognize(roi_gray, face_recognizer, names)
2018-11-30 11:37:53 +01:00
if person == 'unknown':
print('Found unknown person')
else:
print('Found '+person)
2018-11-19 14:12:30 +01:00
OpenShifter()
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-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 11:13:21 +01:00
if label[1] > 100:
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-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():
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()