WebCam capture (Not tested)

This commit is contained in:
hesham 2018-12-07 12:21:18 +03:00
parent 48dbd127a2
commit 83de5c2894

40
main.py
View File

@ -7,18 +7,28 @@ 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
WebCam = False
print('Initilizing..') print('Initilizing..')
try: try:
camera = PiCamera() camera = PiCamera()
@ -26,9 +36,16 @@ def init():
sleep(0.2) sleep(0.2)
print('[ OK ] Camera') print('[ OK ] Camera')
except: except:
print('[ Error ] Can not initialize PiCamera') print('[ Error ] Can not initialize PiCamera\Trying webcam..')
go = False go = False
try:
camera = cv2.VideoCapture()
print('[ OK ] WebCamera')
go = True
WebCam = True
except Exception as ex:
print(ex.args)
go = False
try: try:
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
sleep(1) sleep(1)
@ -44,24 +61,29 @@ def init():
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 = cv2.face.LBPHFaceRecognizer_create() #cv2.face.createLBPHFaceRecognizer()
face_recognizer.load('faces/h') face_recognizer.load('faces/h')
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;