Merge branch 'perf' of Hesham/FaceRecognition-python into master
This commit is contained in:
		
						commit
						03490f1a25
					
				@ -3,24 +3,28 @@ const int trigPin = 9;
 | 
				
			|||||||
const int echoPin = 10;
 | 
					const int echoPin = 10;
 | 
				
			||||||
const int ProxSensor = 2;
 | 
					const int ProxSensor = 2;
 | 
				
			||||||
const int relayPin = 12;
 | 
					const int relayPin = 12;
 | 
				
			||||||
 | 
					const int buzzerPin = 3;
 | 
				
			||||||
int inputVal = 0;
 | 
					int inputVal = 0;
 | 
				
			||||||
long duration;
 | 
					long duration;
 | 
				
			||||||
int distance;
 | 
					int distance;
 | 
				
			||||||
char piInput = 'N';
 | 
					char piInput = 'N';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void setup() {
 | 
					void setup() {
 | 
				
			||||||
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
 | 
					  pinMode(trigPin, OUTPUT);
 | 
				
			||||||
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
 | 
					  pinMode(echoPin, INPUT); 
 | 
				
			||||||
  pinMode(LED_BUILTIN, OUTPUT);
 | 
					  pinMode(LED_BUILTIN, OUTPUT);
 | 
				
			||||||
  pinMode(relayPin, OUTPUT);
 | 
					  pinMode(relayPin, OUTPUT);
 | 
				
			||||||
 | 
					  pinMode(buzzerPin, OUTPUT);
 | 
				
			||||||
 | 
					  pinMode(ProxSensor,INPUT);
 | 
				
			||||||
 | 
					  digitalWrite(buzzerPin, LOW);
 | 
				
			||||||
  digitalWrite(relayPin, LOW);
 | 
					  digitalWrite(relayPin, LOW);
 | 
				
			||||||
  Serial.begin(9600); // Starts the serial communication
 | 
					  Serial.begin(9600); 
 | 
				
			||||||
  pinMode(ProxSensor,INPUT);    //Pin 2 is connected to the output of proximity sensor
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
void loop() {
 | 
					void loop() {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  boolean IROK = false;
 | 
					  boolean IROK = false;
 | 
				
			||||||
  boolean UltraOK = false;
 | 
					  boolean UltraOK = false;
 | 
				
			||||||
  // Clears the trigPin
 | 
					  // Clears the trigPin
 | 
				
			||||||
@ -45,6 +49,7 @@ void loop() {
 | 
				
			|||||||
  if (IROK && UltraOK && Serial.available() > 0 ){
 | 
					  if (IROK && UltraOK && Serial.available() > 0 ){
 | 
				
			||||||
    if (Serial.read() == 'O') {
 | 
					    if (Serial.read() == 'O') {
 | 
				
			||||||
      digitalWrite(relayPin, HIGH);
 | 
					      digitalWrite(relayPin, HIGH);
 | 
				
			||||||
 | 
					      playBuzzer(2,false);
 | 
				
			||||||
      delay(5000);
 | 
					      delay(5000);
 | 
				
			||||||
      digitalWrite(relayPin, LOW);
 | 
					      digitalWrite(relayPin, LOW);
 | 
				
			||||||
      IROK = false;
 | 
					      IROK = false;
 | 
				
			||||||
@ -64,7 +69,26 @@ void serialFlush(){
 | 
				
			|||||||
  while(Serial.available() > 0) {
 | 
					  while(Serial.available() > 0) {
 | 
				
			||||||
    char t = Serial.read();
 | 
					    char t = Serial.read();
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
}   
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					void playBuzzer(int time, boolean ok){
 | 
				
			||||||
 | 
					  if (ok) {
 | 
				
			||||||
 | 
					    tone(buzzerPin, 1000);
 | 
				
			||||||
 | 
					    delay(time/3);
 | 
				
			||||||
 | 
					    noTone(buzzerPin);
 | 
				
			||||||
 | 
					    delay(time/5);
 | 
				
			||||||
 | 
					    tone(buzzerPin,1000);
 | 
				
			||||||
 | 
					    delay(time/3);
 | 
				
			||||||
 | 
					    noTone(buzzerPin);
 | 
				
			||||||
 | 
					  } 
 | 
				
			||||||
 | 
					  else {
 | 
				
			||||||
 | 
					    tone(buzzerPin, 1000);
 | 
				
			||||||
 | 
					    delay(time);
 | 
				
			||||||
 | 
					    noTone(buzzerPin);
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
							
								
								
									
										68
									
								
								main.py
									
									
									
									
									
								
							
							
						
						
									
										68
									
								
								main.py
									
									
									
									
									
								
							@ -4,10 +4,12 @@ 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
 | 
				
			||||||
    from picamera import PiCamera
 | 
					    from picamera import PiCamera
 | 
				
			||||||
 | 
					    from picamera.array import PiRGBArray
 | 
				
			||||||
    import cv2
 | 
					    import cv2
 | 
				
			||||||
    
 | 
					    
 | 
				
			||||||
except Exception as ex:
 | 
					except Exception as ex:
 | 
				
			||||||
@ -20,7 +22,7 @@ def init():
 | 
				
			|||||||
	try:
 | 
						try:
 | 
				
			||||||
		camera = PiCamera()
 | 
							camera = PiCamera()
 | 
				
			||||||
		camera.resolution = (640, 480)
 | 
							camera.resolution = (640, 480)
 | 
				
			||||||
		sleep(2)
 | 
							sleep(0.2)
 | 
				
			||||||
		print('[ OK ] Camera')
 | 
							print('[ OK ] Camera')
 | 
				
			||||||
	except:
 | 
						except:
 | 
				
			||||||
		print('[ Error ] Can not initialize PiCamera')
 | 
							print('[ Error ] Can not initialize PiCamera')
 | 
				
			||||||
@ -44,16 +46,22 @@ def init():
 | 
				
			|||||||
	
 | 
						
 | 
				
			||||||
def start( camera, face_cascade):
 | 
					def start( camera, face_cascade):
 | 
				
			||||||
	j = 0
 | 
						j = 0
 | 
				
			||||||
	while True:
 | 
						rawCapture = PiRGBArray(camera)
 | 
				
			||||||
 | 
						#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)
 | 
				
			||||||
		gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)		
 | 
							camera.capture(rawCapture, format="bgr")
 | 
				
			||||||
		faces = face_cascade.detectMultiScale(gray, 1.1, 4)			
 | 
							img = rawCapture.array
 | 
				
			||||||
 | 
							gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 | 
				
			||||||
 | 
							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)
 | 
				
			||||||
@ -65,6 +73,7 @@ def start( camera, face_cascade):
 | 
				
			|||||||
			
 | 
								
 | 
				
			||||||
		cv2.imshow('image',img) 
 | 
							cv2.imshow('image',img) 
 | 
				
			||||||
		cv2.waitKey(100)
 | 
							cv2.waitKey(100)
 | 
				
			||||||
 | 
							rawCapture.truncate(0)
 | 
				
			||||||
		if cv2.waitKey(1) & 0xFF == ord('q'):
 | 
							if cv2.waitKey(1) & 0xFF == ord('q'):
 | 
				
			||||||
			cv2.destroyAllWindows()
 | 
								cv2.destroyAllWindows()
 | 
				
			||||||
			break
 | 
								break
 | 
				
			||||||
@ -73,52 +82,33 @@ 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/m')
 | 
						face_recognizer.load('faces/m')
 | 
				
			||||||
	img = image.copy()	
 | 
						img = image.copy()
 | 
				
			||||||
	label= face_recognizer.predict(img)
 | 
						label= face_recognizer.predict(img)
 | 
				
			||||||
	print(label)
 | 
						print(label)
 | 
				
			||||||
	if label[0] == 1 and int(label[1]) < 120:
 | 
						if label[1] > 120:
 | 
				
			||||||
		return 'Hesham'
 | 
							return 'unknown'
 | 
				
			||||||
	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/ttyUSB0',9600)
 | 
				
			||||||
		#ser.open()
 | 
							#ser.open()
 | 
				
			||||||
		ser.write('O')
 | 
							ser.write('O')
 | 
				
			||||||
		ser.close()
 | 
							ser.close()
 | 
				
			||||||
	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