Compare commits

...

2 Commits

Author SHA1 Message Date
617bb779c0 WIP: add multi-version support
Beter Init function
2019-01-22 20:25:08 +03:00
83de5c2894 WebCam capture (Not tested) 2018-12-07 12:21:18 +03:00

73
main.py
View File

@ -7,28 +7,51 @@ import sys
import json import json
import datetime import datetime
piCamAvailable = False
try: try:
import numpy as np import numpy as np
from picamera import PiCamera from picamera import PiCamera
from picamera.array import PiRGBArray from picamera.array import PiRGBArray
import cv2 piCamAvailable = True
except Exception as ex: except Exception as ex:
print('[ Error ] some depandincies are missing\n'+ str(ex.args)) print('[ Error ] some depandincies are missing\n'+ str(ex.args))
piCamAvailable = False;
try:
import cv2
except Exception as ex:
print('Could not load OpenCV\Closing')
sys.exit() sys.exit()
def init(): def init():
go = True go = True
print('Initilizing..') piCam = False
try: WebCam = False
camera = PiCamera() print('Initilizing Camera ..')
camera.resolution = (640, 480) if piCamAvailable:
sleep(0.2) try:
print('[ OK ] Camera') camera = PiCamera()
except: camera.resolution = (640, 480)
print('[ Error ] Can not initialize PiCamera') go = True
go = False piCam = True
print('[ OK ] Started PiCamera')
except:
print('[ Warning ] Could not start PiCamera. Trying Webcam.. ')
piCam = False
go = False
else:
try:
camera = cv2.VideoCapture()
print('[ OK ] WebCamera')
go = True
WebCam = True
except Exception as ex:
print('[ Error ] Could not start Webcam.. \nExiting')
go = False
sleep(0.2)
try: try:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sleep(1) sleep(1)
@ -41,27 +64,39 @@ def init():
except OSError as ex: except OSError as ex:
print('Found (unknown) folder') print('Found (unknown) folder')
if (go): if go:
print('Starting Photo loop..') print('Starting Photo loop..')
print('Known people are '+str(loadNames())) print('Known people are '+str(loadNames()))
start(camera, face_cascade) start(camera, face_cascade, WebCam)
else: else:
print('closing') print('closing')
def start( camera, face_cascade): def start( camera, face_cascade, WebCam):
j = 0 j = 0
rawCapture = PiRGBArray(camera) if not WebCam:
face_recognizer = cv2.createLBPHFaceRecognizer() rawCapture = PiRGBArray(camera)
face_recognizer.load('faces/h') # face_recognizer = cv2.face.LBPHFaceRecognizer_create() #cv2.face.createLBPHFaceRecognizer()
face_recognizer = cv2.face_LBPHFaceRecognizer('faces/h')
# face_recognizer.read('faces/h') # Try on Pi
##WIP
try:
face_recognizer.load('faces/h')
except:
pass
names = loadNames() names = loadNames()
while 1: while 1:
try: try:
camera.capture(rawCapture, format="bgr") if not WebCam:
camera.capture(rawCapture, format="bgr")
img = rawCapture.array
else:
ret, img = camera.read()
except: except:
print('[ Error ] Can not capture image. Restarting..') print('[ Error ] Can not capture image. Restarting..')
init() init()
img = rawCapture.array #img = rawCapture.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3,5) faces = face_cascade.detectMultiScale(gray, 1.3,5)
i = 0; i = 0;