WIP: add multi-version support
Beter Init function
This commit is contained in:
		
							parent
							
								
									83de5c2894
								
							
						
					
					
						commit
						617bb779c0
					
				
							
								
								
									
										65
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										65
									
								
								main.py
									
									
									
									
									
								
							@ -14,38 +14,44 @@ try:
 | 
			
		||||
    from picamera import PiCamera
 | 
			
		||||
    from picamera.array import PiRGBArray
 | 
			
		||||
    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: 
 | 
			
		||||
 | 
			
		||||
except Exception as ex:
 | 
			
		||||
    print('Could not load OpenCV\Closing')
 | 
			
		||||
    sys.exit()
 | 
			
		||||
 | 
			
		||||
def init():
 | 
			
		||||
	go = True
 | 
			
		||||
	piCam = False
 | 
			
		||||
	WebCam = False
 | 
			
		||||
	print('Initilizing..')
 | 
			
		||||
	try:
 | 
			
		||||
		camera = PiCamera()
 | 
			
		||||
		camera.resolution = (640, 480)
 | 
			
		||||
		sleep(0.2)
 | 
			
		||||
		print('[ OK ] Camera')
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Can not initialize PiCamera\Trying webcam..')
 | 
			
		||||
		go = False
 | 
			
		||||
	print('Initilizing Camera ..')
 | 
			
		||||
	if piCamAvailable:
 | 
			
		||||
		try:
 | 
			
		||||
			camera = PiCamera()
 | 
			
		||||
			camera.resolution = (640, 480)
 | 
			
		||||
			go = True
 | 
			
		||||
			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(ex.args)
 | 
			
		||||
			print('[ Error ] Could not start Webcam.. \nExiting')
 | 
			
		||||
			go = False
 | 
			
		||||
	sleep(0.2)
 | 
			
		||||
	try:
 | 
			
		||||
		face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 | 
			
		||||
		sleep(1)
 | 
			
		||||
@ -53,25 +59,32 @@ def init():
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Can not load cascade File')
 | 
			
		||||
		go = False
 | 
			
		||||
	try: 
 | 
			
		||||
	try:
 | 
			
		||||
		os.mkdir('unknown')
 | 
			
		||||
	except OSError as ex:
 | 
			
		||||
		print('Found (unknown) folder')
 | 
			
		||||
		
 | 
			
		||||
	if (go):
 | 
			
		||||
 | 
			
		||||
	if go:
 | 
			
		||||
		print('Starting Photo loop..')
 | 
			
		||||
		print('Known people are '+str(loadNames()))
 | 
			
		||||
		start(camera, face_cascade, WebCam)
 | 
			
		||||
		
 | 
			
		||||
	else: 
 | 
			
		||||
 | 
			
		||||
	else:
 | 
			
		||||
		print('closing')
 | 
			
		||||
 | 
			
		||||
def start( camera, face_cascade, WebCam):
 | 
			
		||||
	j = 0
 | 
			
		||||
	if not WebCam:
 | 
			
		||||
		rawCapture = PiRGBArray(camera)
 | 
			
		||||
	face_recognizer = cv2.face.LBPHFaceRecognizer_create() #cv2.face.createLBPHFaceRecognizer()
 | 
			
		||||
	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()
 | 
			
		||||
	while 1:
 | 
			
		||||
		try:
 | 
			
		||||
@ -110,16 +123,16 @@ def start( camera, face_cascade, WebCam):
 | 
			
		||||
			else:
 | 
			
		||||
				print('Found '+person)
 | 
			
		||||
				OpenShifter()
 | 
			
		||||
		cv2.imshow('image',img) 
 | 
			
		||||
		cv2.imshow('image',img)
 | 
			
		||||
		cv2.waitKey(1)
 | 
			
		||||
		rawCapture.truncate(0)
 | 
			
		||||
		if cv2.waitKey(1) & 0xFF == ord('q'):
 | 
			
		||||
			cv2.destroyAllWindows()
 | 
			
		||||
			break
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def recognize(image,face_recognizer, names):
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	try:
 | 
			
		||||
		img = image.copy()
 | 
			
		||||
		label= face_recognizer.predict(img)
 | 
			
		||||
@ -131,7 +144,7 @@ def recognize(image,face_recognizer, names):
 | 
			
		||||
	else:
 | 
			
		||||
		print(str(label) + ' >>'+names[label[0]])
 | 
			
		||||
		return names[label[0]]
 | 
			
		||||
		
 | 
			
		||||
 | 
			
		||||
def OpenShifter():
 | 
			
		||||
	try:
 | 
			
		||||
		ser = serial.Serial('/dev/ttyUSB0',9600)
 | 
			
		||||
@ -147,7 +160,7 @@ def OpenShifter():
 | 
			
		||||
			ser.close()
 | 
			
		||||
		except:
 | 
			
		||||
			print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
 | 
			
		||||
			
 | 
			
		||||
 | 
			
		||||
def loadNames():
 | 
			
		||||
	try:
 | 
			
		||||
		with open("faces/names.json", "r") as read_file:
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user