Compare commits
	
		
			2 Commits
		
	
	
		
			master
			...
			webcam-sup
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| 617bb779c0 | |||
| 83de5c2894 | 
@ -1 +1 @@
 | 
			
		||||
### Python code to run in Raspberry Pi with PiCamera to recognize certin faces.
 | 
			
		||||
### Python code to run in Raspberry Pi with PiCamera to recognize certin faces and send a Serial signal 'o' to be received by arduino. 
 | 
			
		||||
							
								
								
									
										95
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										95
									
								
								main.py
									
									
									
									
									
								
							@ -7,28 +7,51 @@ 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
 | 
			
		||||
	print('Initilizing..')
 | 
			
		||||
	try:
 | 
			
		||||
		camera = PiCamera()
 | 
			
		||||
		camera.resolution = (640, 480)
 | 
			
		||||
		sleep(0.2)
 | 
			
		||||
		print('[ OK ] Camera')
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Can not initialize PiCamera')
 | 
			
		||||
		go = False
 | 
			
		||||
 | 
			
		||||
	piCam = False
 | 
			
		||||
	WebCam = 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('[ Error ] Could not start Webcam.. \nExiting')
 | 
			
		||||
			go = False
 | 
			
		||||
	sleep(0.2)
 | 
			
		||||
	try:
 | 
			
		||||
		face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 | 
			
		||||
		sleep(1)
 | 
			
		||||
@ -36,35 +59,47 @@ 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)
 | 
			
		||||
		
 | 
			
		||||
	else: 
 | 
			
		||||
		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()
 | 
			
		||||
	face_recognizer.load('faces/h')
 | 
			
		||||
	if not WebCam:
 | 
			
		||||
		rawCapture = PiRGBArray(camera)
 | 
			
		||||
	# 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:
 | 
			
		||||
			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
 | 
			
		||||
		i = 0;
 | 
			
		||||
		for (x,y,w,h) in faces:
 | 
			
		||||
			i += 1
 | 
			
		||||
			j += 1
 | 
			
		||||
@ -88,16 +123,16 @@ def start( camera, face_cascade):
 | 
			
		||||
			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)
 | 
			
		||||
@ -110,8 +145,6 @@ def recognize(image,face_recognizer, names):
 | 
			
		||||
		print(str(label) + ' >>'+names[label[0]])
 | 
			
		||||
		return names[label[0]]
 | 
			
		||||
 | 
			
		||||
#def DrawSquares(image,gray,faces,color):
 | 
			
		||||
 | 
			
		||||
def OpenShifter():
 | 
			
		||||
	try:
 | 
			
		||||
		ser = serial.Serial('/dev/ttyUSB0',9600)
 | 
			
		||||
@ -127,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:
 | 
			
		||||
 | 
			
		||||
							
								
								
									
										211
									
								
								trainer.py
									
									
									
									
									
								
							
							
						
						
									
										211
									
								
								trainer.py
									
									
									
									
									
								
							@ -7,127 +7,122 @@ import os
 | 
			
		||||
import sys
 | 
			
		||||
import json
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
picCount = 0
 | 
			
		||||
new = False
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def start():
 | 
			
		||||
    print('Starting training..')
 | 
			
		||||
    label = int(input('Label (Integer): '))
 | 
			
		||||
    name = str(raw_input('Person name: '))
 | 
			
		||||
    picCount = int(input('Number of Photos: '))
 | 
			
		||||
    global new
 | 
			
		||||
    new = promptNew()
 | 
			
		||||
    if new:
 | 
			
		||||
        newFileName = str(raw_input('New File name: '))
 | 
			
		||||
        oldFileName = newFileName
 | 
			
		||||
    else:
 | 
			
		||||
        print('Found in faces:')
 | 
			
		||||
        print(os.listdir('faces/'))
 | 
			
		||||
        oldFileName = str(raw_input('Old File name: '))
 | 
			
		||||
        newFileName = str(raw_input('New File name: '))
 | 
			
		||||
    clearTrainFolder()
 | 
			
		||||
    dum = input('Press any key to start capture..')
 | 
			
		||||
    capture(picCount)
 | 
			
		||||
    addPerson(label, newFileName, oldFileName)
 | 
			
		||||
    addName(name)
 | 
			
		||||
 | 
			
		||||
	print('Starting training..')
 | 
			
		||||
	label = int(input('Label (Integer): '))
 | 
			
		||||
	name = str(raw_input('Person name: '))
 | 
			
		||||
	picCount = int(input('Number of Photos: '))
 | 
			
		||||
	global new 
 | 
			
		||||
	new = promptNew()
 | 
			
		||||
	if new:
 | 
			
		||||
		newFileName = str(raw_input('New File name: '))
 | 
			
		||||
		oldFileName = newFileName
 | 
			
		||||
	else:
 | 
			
		||||
		print('Found in faces:')
 | 
			
		||||
		print(os.listdir('faces/'))
 | 
			
		||||
		oldFileName = str(raw_input('Old File name: '))
 | 
			
		||||
		newFileName = str(raw_input('New File name: '))
 | 
			
		||||
	clearTrainFolder()
 | 
			
		||||
	dum = input('Press any key to start capture..')
 | 
			
		||||
	capture(picCount)
 | 
			
		||||
	addPerson(label, newFileName, oldFileName)
 | 
			
		||||
	addName(name)
 | 
			
		||||
 | 
			
		||||
def clearTrainFolder():
 | 
			
		||||
    print('clearing train folder')
 | 
			
		||||
    filelist = [f for f in os.listdir('train/') if f.endswith(".jpg")]
 | 
			
		||||
    for f in filelist:
 | 
			
		||||
        os.remove(os.path.join('train/', f))
 | 
			
		||||
 | 
			
		||||
	print('clearing train folder')
 | 
			
		||||
	filelist = [ f for f in os.listdir('train/') if f.endswith(".jpg") ]
 | 
			
		||||
	for f in filelist:
 | 
			
		||||
		os.remove(os.path.join('train/', f))
 | 
			
		||||
 | 
			
		||||
def addPerson(label, newFileName, oldFileName):
 | 
			
		||||
    try:
 | 
			
		||||
        # faceFilePath = '/faces/hesham-saeed2'
 | 
			
		||||
        faceFile = 'faces/' + oldFileName
 | 
			
		||||
        face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
			
		||||
        global new
 | 
			
		||||
        if new:
 | 
			
		||||
            print('new file. skipping load function')
 | 
			
		||||
        else:
 | 
			
		||||
            print('loading old file')
 | 
			
		||||
            face_recognizer.load(faceFile)
 | 
			
		||||
    except:
 | 
			
		||||
        print('[ Error ] Problem in init/loading LBPHfacerecognizer')
 | 
			
		||||
        sys.exit()
 | 
			
		||||
    images = []
 | 
			
		||||
    labels = []
 | 
			
		||||
    image_names = os.listdir('train/')
 | 
			
		||||
    for image_name in image_names:
 | 
			
		||||
        if image_name.startswith('.'):
 | 
			
		||||
            continue;
 | 
			
		||||
        image_path = 'train/' + image_name
 | 
			
		||||
        image = cv2.imread(image_path)
 | 
			
		||||
        gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 | 
			
		||||
        face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 | 
			
		||||
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
 | 
			
		||||
        for (x, y, w, h) in faces:
 | 
			
		||||
            images.append(gray[y:y + h, x:x + w])
 | 
			
		||||
            labels.append(label)
 | 
			
		||||
            cv2.imshow('face', gray[y:y + h, x:x + w])
 | 
			
		||||
            cv2.waitKey(100)
 | 
			
		||||
            print('Learning ' + image_name)
 | 
			
		||||
    if new:
 | 
			
		||||
        face_recognizer.train(images, np.array(labels))
 | 
			
		||||
    else:
 | 
			
		||||
        face_recognizer.update(images, np.array(labels))
 | 
			
		||||
    face_recognizer.save('faces/' + newFileName)
 | 
			
		||||
    print('Updated and saved file in faces/' + newFileName)
 | 
			
		||||
 | 
			
		||||
	try:
 | 
			
		||||
		#faceFilePath = '/faces/hesham-saeed2'
 | 
			
		||||
		faceFile = 'faces/'+ oldFileName
 | 
			
		||||
		face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
			
		||||
		global new
 | 
			
		||||
		if new:
 | 
			
		||||
			print 'new file. skipping load function'
 | 
			
		||||
		else:
 | 
			
		||||
			print 'loading old file'
 | 
			
		||||
			face_recognizer.load(faceFile)
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Problem in init/loading LBPHfacerecognizer')
 | 
			
		||||
		sys.exit()
 | 
			
		||||
	images = []
 | 
			
		||||
	labels = []
 | 
			
		||||
	image_names = os.listdir('train/')
 | 
			
		||||
	for image_name in image_names:
 | 
			
		||||
		if image_name.startswith('.'):
 | 
			
		||||
			continue;
 | 
			
		||||
		image_path = 'train/' + image_name
 | 
			
		||||
		image = cv2.imread(image_path)
 | 
			
		||||
		gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 | 
			
		||||
		face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 | 
			
		||||
		faces = face_cascade.detectMultiScale(gray, 1.3, 5)
 | 
			
		||||
		for (x,y,w,h) in faces:
 | 
			
		||||
			images.append(gray[y:y+h, x:x+w])
 | 
			
		||||
			labels.append(label)
 | 
			
		||||
			cv2.imshow('face',gray[y:y+h, x:x+w])
 | 
			
		||||
			cv2.waitKey(100)
 | 
			
		||||
			print('Learning '+ image_name)
 | 
			
		||||
	if new:
 | 
			
		||||
		face_recognizer.train(images, np.array(labels))
 | 
			
		||||
	else:
 | 
			
		||||
		face_recognizer.update(images, np.array(labels))
 | 
			
		||||
	face_recognizer.save('faces/'+newFileName)
 | 
			
		||||
	print('Updated and saved file in faces/'+newFileName) 
 | 
			
		||||
 | 
			
		||||
def capture(count):
 | 
			
		||||
    try:
 | 
			
		||||
        print('Initializing camera')
 | 
			
		||||
        camera = PiCamera()
 | 
			
		||||
        camera.resolution = (640, 480)
 | 
			
		||||
        print('[ OK ] Camera')
 | 
			
		||||
        sleep(2)
 | 
			
		||||
    except:
 | 
			
		||||
        print('[ Error ] Can not initialize PiCamera')
 | 
			
		||||
        sys.exit()
 | 
			
		||||
    for i in range(1, count + 1):
 | 
			
		||||
        pic = 'train/' + str(i) + '.jpg'
 | 
			
		||||
        camera.capture(pic)
 | 
			
		||||
        photo = cv2.imread(pic, 1)
 | 
			
		||||
        cv2.imshow('Photo', photo)
 | 
			
		||||
        print('Captured ' + str(i) + '\nPath ' + pic)
 | 
			
		||||
        cv2.waitKey(100)
 | 
			
		||||
        sleep(1)
 | 
			
		||||
    cv2.destroyAllWindows()
 | 
			
		||||
    print('Done!')
 | 
			
		||||
 | 
			
		||||
	try:
 | 
			
		||||
		print('Initializing camera')
 | 
			
		||||
		camera = PiCamera()
 | 
			
		||||
		camera.resolution = (640, 480)
 | 
			
		||||
		print('[ OK ] Camera')
 | 
			
		||||
		sleep(2)
 | 
			
		||||
	except:
 | 
			
		||||
		print('[ Error ] Can not initialize PiCamera')
 | 
			
		||||
		sys.exit()
 | 
			
		||||
	for i in range(1, count+1):
 | 
			
		||||
		pic = 'train/'+str(i)+'.jpg'
 | 
			
		||||
		camera.capture(pic)
 | 
			
		||||
		photo = cv2.imread(pic,1)
 | 
			
		||||
		cv2.imshow('Photo',photo)
 | 
			
		||||
		print('Captured '+str(i)+'\nPath '+ pic)
 | 
			
		||||
		cv2.waitKey(100)
 | 
			
		||||
		sleep(1)
 | 
			
		||||
	cv2.destroyAllWindows()
 | 
			
		||||
	print('Done!')
 | 
			
		||||
 | 
			
		||||
def promptNew():
 | 
			
		||||
    filemode = raw_input('Make new File? (y/n): ')
 | 
			
		||||
    if filemode == 'y':
 | 
			
		||||
        return True
 | 
			
		||||
    elif filemode == 'n':
 | 
			
		||||
        return False
 | 
			
		||||
    else:
 | 
			
		||||
        print('incorrect input')
 | 
			
		||||
        promptNew()
 | 
			
		||||
 | 
			
		||||
	filemode = raw_input('Make new File? (y/n): ')
 | 
			
		||||
	if filemode == 'y':
 | 
			
		||||
		return True
 | 
			
		||||
	elif filemode == 'n':
 | 
			
		||||
		return False
 | 
			
		||||
	else:
 | 
			
		||||
		print('incorrect input')
 | 
			
		||||
		promptNew()
 | 
			
		||||
 | 
			
		||||
def addName(newName):
 | 
			
		||||
    with open("faces/names.json", "r") as read_file:
 | 
			
		||||
        exist = False
 | 
			
		||||
        namesJson = json.load(read_file)
 | 
			
		||||
        names = list(namesJson)
 | 
			
		||||
        i = 0
 | 
			
		||||
        for name in names:
 | 
			
		||||
            if newName == name:
 | 
			
		||||
                exist = True
 | 
			
		||||
                print('name already in names list with index ' + str(i))
 | 
			
		||||
            i += 1
 | 
			
		||||
        if not exist:
 | 
			
		||||
            names.append(newName)
 | 
			
		||||
            print('Added ' + newName + 'to names.json file')
 | 
			
		||||
    with open("faces/names.json", "w") as write_file:
 | 
			
		||||
        json.dump(names, write_file)
 | 
			
		||||
 | 
			
		||||
	
 | 
			
		||||
	with open("faces/names.json", "r") as read_file:
 | 
			
		||||
		exist = False
 | 
			
		||||
		namesJson = json.load(read_file)
 | 
			
		||||
		names = list(namesJson)
 | 
			
		||||
		i = 0
 | 
			
		||||
		for name in names:
 | 
			
		||||
			if newName == name:
 | 
			
		||||
				exist = True
 | 
			
		||||
				print('name already in names list with index '+str(i))
 | 
			
		||||
			i += 1
 | 
			
		||||
		if not exist:
 | 
			
		||||
			names.append(newName)
 | 
			
		||||
			print('Added '+newName+'to names.json file')
 | 
			
		||||
	with open("faces/names.json","w") as write_file:
 | 
			
		||||
		json.dump(names,write_file)
 | 
			
		||||
 | 
			
		||||
start()
 | 
			
		||||
 | 
			
		||||
		Loading…
	
		Reference in New Issue
	
	Block a user