FaceRecognition-python/main.py

66 lines
1.4 KiB
Python
Raw Normal View History

2018-11-12 10:46:29 +01:00
from time import sleep
from picamera import PiCamera
from io import BytesIO
import cv2
import numpy as np
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)
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-12 11:26:55 +01:00
else:
print('closing')
2018-11-12 10:46:29 +01:00
2018-11-12 16:16:58 +01:00
def start( camera, face_cascade):
2018-11-12 10:46:29 +01:00
while True:
2018-11-12 11:26:55 +01:00
try:
print('Taking photo')
camera.capture('img.jpg')
print('Photo Captured')
img = cv2.imread('img.jpg',1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
print('Photo Converted')
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
print('Photo Processed')
i = 0;
for (x,y,w,h) in faces:
i = i + 1
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
#cv2.imwrite('result.jpg', gray)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
print('Drawing on face ', i)
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-12 11:26:55 +01:00
except:
print('[ Error ] Unexpected exception')
print('Closing')
break
2018-11-12 10:46:29 +01:00
init()