merge #3
							
								
								
									
										59
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										59
									
								
								main.py
									
									
									
									
									
								
							@ -4,6 +4,7 @@ from io import BytesIO
 | 
				
			|||||||
import os
 | 
					import os
 | 
				
			||||||
import serial
 | 
					import serial
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
try:
 | 
					try:
 | 
				
			||||||
    import numpy as np
 | 
					    import numpy as np
 | 
				
			||||||
@ -46,18 +47,21 @@ def init():
 | 
				
			|||||||
def start( camera, face_cascade):
 | 
					def start( camera, face_cascade):
 | 
				
			||||||
	j = 0
 | 
						j = 0
 | 
				
			||||||
	rawCapture = PiRGBArray(camera)
 | 
						rawCapture = PiRGBArray(camera)
 | 
				
			||||||
	while True:
 | 
						#camera.framerate = 32
 | 
				
			||||||
 | 
						#rawCapture = PiRGBArray(camera, size=(640, 480))
 | 
				
			||||||
 | 
						#for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):	
 | 
				
			||||||
 | 
						while 1:
 | 
				
			||||||
	#try:
 | 
						#try:
 | 
				
			||||||
		#camera.capture('img.jpg')			
 | 
							#camera.capture('img.jpg')
 | 
				
			||||||
		#img = cv2.imread('img.jpg',1)
 | 
							#img = cv2.imread('img.jpg',1)
 | 
				
			||||||
		camera.capture(rawCapture, format="bgr")
 | 
							camera.capture(rawCapture, format="bgr")
 | 
				
			||||||
		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.2, 4)			
 | 
							faces = face_cascade.detectMultiScale(gray, 1.2,5)
 | 
				
			||||||
		i = 0;
 | 
							i = 0;
 | 
				
			||||||
		for (x,y,w,h) in faces:
 | 
							for (x,y,w,h) in faces:
 | 
				
			||||||
			i += 1
 | 
								i += 1
 | 
				
			||||||
			j += 1 				
 | 
								j += 1
 | 
				
			||||||
			roi_gray = gray[y:y+h, x:x+w]
 | 
								roi_gray = gray[y:y+h, x:x+w]
 | 
				
			||||||
			roi_color = img[y:y+h, x:x+w]
 | 
								roi_color = img[y:y+h, x:x+w]
 | 
				
			||||||
			person = recognize(roi_gray)
 | 
								person = recognize(roi_gray)
 | 
				
			||||||
@ -78,44 +82,19 @@ def start( camera, face_cascade):
 | 
				
			|||||||
			#print('[ Error ] Unexpected exception')
 | 
								#print('[ Error ] Unexpected exception')
 | 
				
			||||||
			#print('Closing')
 | 
								#print('Closing')
 | 
				
			||||||
			#break
 | 
								#break
 | 
				
			||||||
			
 | 
					 | 
				
			||||||
def train(number):
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	train_path = 'train/'
 | 
					 | 
				
			||||||
	face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
					 | 
				
			||||||
	images = []
 | 
					 | 
				
			||||||
	labels = []
 | 
					 | 
				
			||||||
	image_names = os.listdir(train_path)
 | 
					 | 
				
			||||||
	for image_name in image_names:
 | 
					 | 
				
			||||||
		if image_name.startswith("."):
 | 
					 | 
				
			||||||
			continue;
 | 
					 | 
				
			||||||
		image_path = train_path + image_name
 | 
					 | 
				
			||||||
		image = cv2.imread(image_path)
 | 
					 | 
				
			||||||
		gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
 | 
					 | 
				
			||||||
		images.append(gray)
 | 
					 | 
				
			||||||
		labels.append(number)
 | 
					 | 
				
			||||||
	print('training')
 | 
					 | 
				
			||||||
	face_recognizer.train(images, np.array(labels))
 | 
					 | 
				
			||||||
	face_recognizer.save('faces/hesham')
 | 
					 | 
				
			||||||
	
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
def recognize(image):
 | 
					def recognize(image):
 | 
				
			||||||
	
 | 
						names = loadNames()
 | 
				
			||||||
	subjects = ['','Hesham','Saeed']
 | 
					 | 
				
			||||||
	face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
						face_recognizer = cv2.createLBPHFaceRecognizer()
 | 
				
			||||||
	face_recognizer.load('faces/Hesham-Saeed')
 | 
						face_recognizer.load('faces/m')
 | 
				
			||||||
	img = image.copy()	
 | 
						img = image.copy()
 | 
				
			||||||
	label= face_recognizer.predict(img)
 | 
						label= face_recognizer.predict(img)
 | 
				
			||||||
	print(label)
 | 
						if label[1] > 120:
 | 
				
			||||||
	if label[0] == 1 and int(label[1]) < 120:
 | 
							return 'unknown'
 | 
				
			||||||
		return 'Hesham'
 | 
					 | 
				
			||||||
	elif label[0] == 2 and int(label[1]) < 120:
 | 
					 | 
				
			||||||
		return 'Safwan'
 | 
					 | 
				
			||||||
	elif label[0] == 4 and int(label[1]) < 120:
 | 
					 | 
				
			||||||
		return 'Hesham2'
 | 
					 | 
				
			||||||
	else:
 | 
						else:
 | 
				
			||||||
		return 'Unknown'
 | 
							return names[label[0]]
 | 
				
			||||||
			
 | 
							
 | 
				
			||||||
def OpenShifter():
 | 
					def OpenShifter():
 | 
				
			||||||
	try:
 | 
						try:
 | 
				
			||||||
		ser = serial.Serial('/dev/ttyUSB1',9600)
 | 
							ser = serial.Serial('/dev/ttyUSB1',9600)
 | 
				
			||||||
@ -125,5 +104,11 @@ def OpenShifter():
 | 
				
			|||||||
	except Exception as ex:
 | 
						except Exception as ex:
 | 
				
			||||||
		print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
 | 
							print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					def loadNames():
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						with open("faces/names.json", "r") as read_file:
 | 
				
			||||||
 | 
							data = json.load(read_file)
 | 
				
			||||||
 | 
							return list(data)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
init()
 | 
					init()
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										31
									
								
								trainer.py
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								trainer.py
									
									
									
									
									
								
							@ -5,6 +5,7 @@ import cv2
 | 
				
			|||||||
import numpy as np
 | 
					import numpy as np
 | 
				
			||||||
import os
 | 
					import os
 | 
				
			||||||
import sys
 | 
					import sys
 | 
				
			||||||
 | 
					import json
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
picCount = 0
 | 
					picCount = 0
 | 
				
			||||||
@ -13,6 +14,7 @@ new = False
 | 
				
			|||||||
def start():
 | 
					def start():
 | 
				
			||||||
	print('Starting training..')
 | 
						print('Starting training..')
 | 
				
			||||||
	label = int(input('Label (Integer): '))
 | 
						label = int(input('Label (Integer): '))
 | 
				
			||||||
 | 
						name = str(raw_input('Person name: '))
 | 
				
			||||||
	picCount = int(input('Number of Photos: '))
 | 
						picCount = int(input('Number of Photos: '))
 | 
				
			||||||
	global new 
 | 
						global new 
 | 
				
			||||||
	new = promptNew()
 | 
						new = promptNew()
 | 
				
			||||||
@ -25,11 +27,11 @@ def start():
 | 
				
			|||||||
		oldFileName = str(raw_input('Old File name: '))
 | 
							oldFileName = str(raw_input('Old File name: '))
 | 
				
			||||||
		newFileName = str(raw_input('New File name: '))
 | 
							newFileName = str(raw_input('New File name: '))
 | 
				
			||||||
	clearTrainFolder()
 | 
						clearTrainFolder()
 | 
				
			||||||
	print('Press any key to start capture..')
 | 
						dum = input('Press any key to start capture..')
 | 
				
			||||||
	dum = input('')
 | 
					 | 
				
			||||||
	capture(picCount)
 | 
						capture(picCount)
 | 
				
			||||||
	addPerson(label, newFileName, oldFileName)
 | 
						addPerson(label, newFileName, oldFileName)
 | 
				
			||||||
	
 | 
						addName(name)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
def clearTrainFolder():
 | 
					def clearTrainFolder():
 | 
				
			||||||
	print('clearing train folder')
 | 
						print('clearing train folder')
 | 
				
			||||||
	filelist = [ f for f in os.listdir('train/') if f.endswith(".jpg") ]
 | 
						filelist = [ f for f in os.listdir('train/') if f.endswith(".jpg") ]
 | 
				
			||||||
@ -64,6 +66,8 @@ def addPerson(label, newFileName, oldFileName):
 | 
				
			|||||||
		for (x,y,w,h) in faces:
 | 
							for (x,y,w,h) in faces:
 | 
				
			||||||
			images.append(gray[y:y+h, x:x+w])
 | 
								images.append(gray[y:y+h, x:x+w])
 | 
				
			||||||
			labels.append(label)
 | 
								labels.append(label)
 | 
				
			||||||
 | 
								cv2.imshow('face',gray[y:y+h, x:x+w])
 | 
				
			||||||
 | 
								cv2.waitKey(100)
 | 
				
			||||||
			print('Learning '+ image_name)
 | 
								print('Learning '+ image_name)
 | 
				
			||||||
	if new:
 | 
						if new:
 | 
				
			||||||
		face_recognizer.train(images, np.array(labels))
 | 
							face_recognizer.train(images, np.array(labels))
 | 
				
			||||||
@ -102,5 +106,22 @@ def promptNew():
 | 
				
			|||||||
	else:
 | 
						else:
 | 
				
			||||||
		print('incorrect input')
 | 
							print('incorrect input')
 | 
				
			||||||
		promptNew()
 | 
							promptNew()
 | 
				
			||||||
         
 | 
					
 | 
				
			||||||
start()
 | 
					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)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user