WebCam capture (Not tested)
This commit is contained in:
		
							parent
							
								
									48dbd127a2
								
							
						
					
					
						commit
						83de5c2894
					
				
							
								
								
									
										40
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										40
									
								
								main.py
									
									
									
									
									
								
							@ -7,18 +7,28 @@ import sys
 | 
			
		||||
import json
 | 
			
		||||
import datetime
 | 
			
		||||
 | 
			
		||||
piCamAvailable = False
 | 
			
		||||
 | 
			
		||||
try:
 | 
			
		||||
    import numpy as np
 | 
			
		||||
    from picamera import PiCamera
 | 
			
		||||
    from picamera.array import PiRGBArray
 | 
			
		||||
    import cv2
 | 
			
		||||
    piCamAvailable = True
 | 
			
		||||
    
 | 
			
		||||
except Exception as ex:
 | 
			
		||||
    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()
 | 
			
		||||
 | 
			
		||||
def init():
 | 
			
		||||
	go = True
 | 
			
		||||
	WebCam = False
 | 
			
		||||
	print('Initilizing..')
 | 
			
		||||
	try:
 | 
			
		||||
		camera = PiCamera()
 | 
			
		||||
@ -26,9 +36,16 @@ def init():
 | 
			
		||||
		sleep(0.2)
 | 
			
		||||
		print('[ OK ] Camera')
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Can not initialize PiCamera')
 | 
			
		||||
		print('[ Error ] Can not initialize PiCamera\Trying webcam..')
 | 
			
		||||
		go = False
 | 
			
		||||
 | 
			
		||||
		try:
 | 
			
		||||
			camera = cv2.VideoCapture()
 | 
			
		||||
			print('[ OK ] WebCamera')
 | 
			
		||||
			go = True
 | 
			
		||||
			WebCam = True
 | 
			
		||||
		except Exception as ex:
 | 
			
		||||
			print(ex.args)
 | 
			
		||||
			go = False
 | 
			
		||||
	try:
 | 
			
		||||
		face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 | 
			
		||||
		sleep(1)
 | 
			
		||||
@ -44,24 +61,29 @@ def init():
 | 
			
		||||
	if (go):
 | 
			
		||||
		print('Starting Photo loop..')
 | 
			
		||||
		print('Known people are '+str(loadNames()))
 | 
			
		||||
		start(camera, face_cascade)
 | 
			
		||||
		start(camera, face_cascade, WebCam)
 | 
			
		||||
		
 | 
			
		||||
	else: 
 | 
			
		||||
		print('closing')
 | 
			
		||||
 | 
			
		||||
def start( camera, face_cascade):
 | 
			
		||||
def start( camera, face_cascade, WebCam):
 | 
			
		||||
	j = 0
 | 
			
		||||
	rawCapture = PiRGBArray(camera)
 | 
			
		||||
	face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
			
		||||
	if not WebCam:
 | 
			
		||||
		rawCapture = PiRGBArray(camera)
 | 
			
		||||
	face_recognizer = cv2.face.LBPHFaceRecognizer_create() #cv2.face.createLBPHFaceRecognizer()
 | 
			
		||||
	face_recognizer.load('faces/h')
 | 
			
		||||
	names = loadNames()
 | 
			
		||||
	while 1:
 | 
			
		||||
		try:
 | 
			
		||||
			camera.capture(rawCapture, format="bgr")
 | 
			
		||||
			if not WebCam:
 | 
			
		||||
				camera.capture(rawCapture, format="bgr")
 | 
			
		||||
				img = rawCapture.array
 | 
			
		||||
			else:
 | 
			
		||||
				ret, img = camera.read()
 | 
			
		||||
		except:
 | 
			
		||||
			print('[ Error ] Can not capture image. Restarting..')
 | 
			
		||||
			init()
 | 
			
		||||
		img = rawCapture.array
 | 
			
		||||
		#img = rawCapture.array
 | 
			
		||||
		gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 | 
			
		||||
		faces = face_cascade.detectMultiScale(gray, 1.3,5)
 | 
			
		||||
		i = 0;
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user