Compare commits

..

No commits in common. "master" and "0.3" have entirely different histories.
master ... 0.3

10 changed files with 209 additions and 270572 deletions

3
.gitignore vendored
View File

@ -1,4 +1,3 @@
*.[Jj][Pp][Gg] *.[Jj][Pp][Gg]
*.pyc *.pyc
*.suo *.suo
*.png

View File

@ -1,17 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>FaceRecognition-python</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.python.pydev.PyDevBuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.python.pydev.pythonNature</nature>
</natures>
</projectDescription>

View File

@ -1,5 +0,0 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<?eclipse-pydev version="1.0"?><pydev_project>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
</pydev_project>

View File

@ -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.

View File

@ -7,84 +7,28 @@ const int buzzerPin = 3;
int inputVal = 0; int inputVal = 0;
long duration; long duration;
int distance; int distance;
boolean IROK = false; char piInput = 'N';
boolean UltraOK = false;
char pi = 'N';
void setup() { void setup() {
pinMode(trigPin, OUTPUT); pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT); pinMode(echoPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(relayPin, OUTPUT); pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT); pinMode(buzzerPin, OUTPUT);
pinMode(ProxSensor, INPUT); pinMode(ProxSensor,INPUT);
digitalWrite(buzzerPin, LOW); digitalWrite(buzzerPin, LOW);
digitalWrite(relayPin, LOW); digitalWrite(relayPin, LOW);
Serial.begin(9600); Serial.begin(9600);
} }
void loop() { void loop() {
IROK = false;
UltraOK = false;
pi = 'N';
distance = readDistance(); boolean IROK = false;
if (distance < 70) UltraOK = true; boolean UltraOK = false;
else UltraOK = false;
if (digitalRead(ProxSensor) == LOW ) IROK = true;
else IROK = false;
if (Serial.available() > 0) {
pi = Serial.read();
}
delay(500);
Serial.println(pi);
Serial.println(IROK);
Serial.println(UltraOK);
if (IROK && UltraOK && pi == 'o') {
openShifter();
}
IROK = false;
UltraOK = false;
}
void openShifter() {
digitalWrite(relayPin, HIGH);
playBuzzer(300, true);
delay(7000);
digitalWrite(relayPin, LOW);
IROK = false;
UltraOK = false;
pi = 'N';
serialFlush();
}
void serialFlush() {
while (Serial.available() > 0) {
char t = Serial.read();
}
}
void playBuzzer(int time, boolean ok) {
tone(buzzerPin, 3000);
delay(time / 2);
noTone(buzzerPin);
delay(100);
tone(buzzerPin, 3000);
delay(time / 2);
noTone(buzzerPin);
}
int readDistance() {
// Clears the trigPin // Clears the trigPin
digitalWrite(trigPin, LOW); digitalWrite(trigPin, LOW);
delayMicroseconds(2); delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds // Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH); digitalWrite(trigPin, HIGH);
@ -93,6 +37,57 @@ int readDistance() {
// Reads the echoPin, returns the sound wave travel time in microseconds // Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH); duration = pulseIn(echoPin, HIGH);
// Calculating the distance // Calculating the distance
return duration * 0.034 / 2; distance= duration*0.034/2;
if (distance < 70) UltraOK = true;
else UltraOK = false;
if(digitalRead(ProxSensor) == LOW ) IROK = true;
else IROK = false;
char pi = 'N';
if (Serial.available() > 0) { pi = Serial.read(); }
delay(500);
Serial.println(pi);
Serial.println(IROK);
Serial.println(UltraOK);
//if (IROK && UltraOK) Serial.println("A");
if (IROK && UltraOK && pi == 'o'){
//if (Serial.read() == 'o') {
digitalWrite(relayPin, HIGH);
playBuzzer(300,true);
delay(7000);
digitalWrite(relayPin, LOW);
IROK = false;
UltraOK = false;
pi = 'N';
serialFlush();
//}
}
IROK = false;
UltraOK = false;
} }
void serialFlush(){
while(Serial.available() > 0) {
char t = Serial.read();
}
}
void playBuzzer(int time, boolean ok){
tone(buzzerPin, 3000);
delay(time/2);
noTone(buzzerPin);
delay(100);
tone(buzzerPin, 3000);
delay(time/2);
noTone(buzzerPin);
}

73686
faces/h

File diff suppressed because it is too large Load Diff

196621
faces/m

File diff suppressed because it is too large Load Diff

View File

@ -1 +1 @@
["", "Hesham", "Aidarous"] ["", "Hesham", "Saeed"]

110
main.py
View File

@ -1,11 +1,10 @@
from time import sleep from time import sleep
from PIL import Image from io import BytesIO
import os import os
import serial import serial
import sys import sys
import json import json
import datetime
try: try:
import numpy as np import numpy as np
@ -19,7 +18,7 @@ except Exception as ex:
def init(): def init():
go = True go = True
print('Initilizing..') print('Initilizing Camera and cascade components..')
try: try:
camera = PiCamera() camera = PiCamera()
camera.resolution = (640, 480) camera.resolution = (640, 480)
@ -36,82 +35,66 @@ def init():
except: except:
print('[ Error ] Can not load cascade File') print('[ Error ] Can not load cascade File')
go = False go = False
try:
os.mkdir('unknown')
except OSError as ex:
print('Found (unknown) folder')
if (go): if (go):
print('Starting Photo loop..') print('Starting Photo loop..')
print('Known people are '+str(loadNames()))
start(camera, face_cascade) start(camera, face_cascade)
else: else:
print('closing') print('closing')
#else:
#print('Sensors not Satisfied')
def start( camera, face_cascade): def start( camera, face_cascade):
j = 0 j = 0
rawCapture = PiRGBArray(camera) rawCapture = PiRGBArray(camera)
face_recognizer = cv2.createLBPHFaceRecognizer() #camera.framerate = 32
face_recognizer.load('faces/h') #rawCapture = PiRGBArray(camera, size=(640, 480))
names = loadNames() #for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
while 1: while 1:
try: #try:
camera.capture(rawCapture, format="bgr") #camera.capture('img.jpg')
except: #img = cv2.imread('img.jpg',1)
print('[ Error ] Can not capture image. Restarting..') camera.capture(rawCapture, format="bgr")
init()
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.3,5) 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
try: 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, face_recognizer, names) if person == 'Hesham' or person == 'Saeed':#Change to if 'unknown' elif openShifter
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),1)
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (0,255,0), 2)
except:
print('[ Error ] Recognition fail')
if person == 'unknown':
print('Found unknown person')
try:
RGBImage = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = Image.fromarray(RGBImage)
image.save('unknown/'+str(datetime.datetime.now())+'.png')
except Exception as ex :
print('[ Error ] Saving unkown fail')
print(ex)
else:
print('Found '+person)
OpenShifter() OpenShifter()
print('Sending to Arduino!')
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
cv2.imshow('image',img) cv2.imshow('image',img)
cv2.waitKey(1) cv2.waitKey(100)
rawCapture.truncate(0) rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'): if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows() cv2.destroyAllWindows()
break break
#except:
#print('[ Error ] Unexpected exception')
#print('Closing')
#break
def recognize(image,face_recognizer, names): def recognize(image):
names = loadNames()
try: face_recognizer = cv2.createLBPHFaceRecognizer()
img = image.copy() face_recognizer.load('faces/m')
label= face_recognizer.predict(img) img = image.copy()
except: label= face_recognizer.predict(img)
print('[ Error ] Error in Recognize() function') if label[1] > 120:
if label[1] > 70:
print(str(label))
return 'unknown' return 'unknown'
else: else:
print(str(label) + ' >>'+names[label[0]]) print(str(label) + ' >>'+names[label[0]])
return names[label[0]] return names[label[0]]
#def DrawSquares(image,gray,faces,color):
def OpenShifter(): def OpenShifter():
try: try:
ser = serial.Serial('/dev/ttyUSB0',9600) ser = serial.Serial('/dev/ttyUSB0',9600)
@ -119,22 +102,13 @@ def OpenShifter():
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/ttyUSB0 ..') print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
try:
ser = serial.Serial('/dev/ttyUSB1',9600)
ser.open()
ser.write('o')
ser.close()
except:
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
def loadNames(): def loadNames():
try:
with open("faces/names.json", "r") as read_file: with open("faces/names.json", "r") as read_file:
data = json.load(read_file) data = json.load(read_file)
return list(data) return list(data)
except:
print('[ Error ] Error in loadNames() function')
init() init()

View File

@ -7,127 +7,121 @@ import os
import sys import sys
import json import json
picCount = 0 picCount = 0
new = False 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: ')) 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()
if new: if new:
newFileName = str(raw_input('New File name: ')) newFileName = str(raw_input('New File name: '))
oldFileName = newFileName oldFileName = newFileName
else: else:
print('Found in faces:') print('Found in faces:')
print(os.listdir('faces/')) print(os.listdir('faces/'))
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()
dum = input('Press any key to start capture..') dum = input('Press any key to start capture..')
capture(picCount) capture(picCount)
addPerson(label, newFileName, oldFileName) addPerson(label, newFileName, oldFileName)
addName(name) 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") ]
for f in filelist: for f in filelist:
os.remove(os.path.join('train/', f)) os.remove(os.path.join('train/', f))
def addPerson(label, newFileName, oldFileName): def addPerson(label, newFileName, oldFileName):
try: try:
# faceFilePath = '/faces/hesham-saeed2' #faceFilePath = '/faces/hesham-saeed2'
faceFile = 'faces/' + oldFileName faceFile = 'faces/'+ oldFileName
face_recognizer = cv2.createLBPHFaceRecognizer() face_recognizer = cv2.createLBPHFaceRecognizer()
global new global new
if new: if new:
print('new file. skipping load function') print 'new file. skipping load function'
else: else:
print('loading old file') print 'loading old file'
face_recognizer.load(faceFile) face_recognizer.load(faceFile)
except: except:
print('[ Error ] Problem in init/loading LBPHfacerecognizer') print('[ Error ] Problem in init/loading LBPHfacerecognizer')
sys.exit() sys.exit()
images = [] images = []
labels = [] labels = []
image_names = os.listdir('train/') image_names = os.listdir('train/')
for image_name in image_names: for image_name in image_names:
if image_name.startswith('.'): if image_name.startswith('.'):
continue; continue;
image_path = 'train/' + image_name image_path = 'train/' + image_name
image = cv2.imread(image_path) image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml') face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
faces = face_cascade.detectMultiScale(gray, 1.3, 5) faces = face_cascade.detectMultiScale(gray, 1.3, 5)
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.imshow('face',gray[y:y+h, x:x+w])
cv2.waitKey(100) 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))
else: else:
face_recognizer.update(images, np.array(labels)) face_recognizer.update(images, np.array(labels))
face_recognizer.save('faces/' + newFileName) face_recognizer.save('faces/'+newFileName)
print('Updated and saved file in faces/' + newFileName) print('Updated and saved file in faces/'+newFileName)
def capture(count): def capture(count):
try: try:
print('Initializing camera') print('Initializing camera')
camera = PiCamera() camera = PiCamera()
camera.resolution = (640, 480) camera.resolution = (640, 480)
print('[ OK ] Camera') print('[ OK ] Camera')
sleep(2) sleep(2)
except: except:
print('[ Error ] Can not initialize PiCamera') print('[ Error ] Can not initialize PiCamera')
sys.exit() sys.exit()
for i in range(1, count + 1): for i in range(1, count+1):
pic = 'train/' + str(i) + '.jpg' pic = 'train/'+str(i)+'.jpg'
camera.capture(pic) camera.capture(pic)
photo = cv2.imread(pic, 1) photo = cv2.imread(pic,1)
cv2.imshow('Photo', photo) cv2.imshow('Photo',photo)
print('Captured ' + str(i) + '\nPath ' + pic) print('Captured '+str(i)+'\nPath '+ pic)
cv2.waitKey(100) cv2.waitKey(100)
sleep(1) sleep(1)
cv2.destroyAllWindows() cv2.destroyAllWindows()
print('Done!') print('Done!')
def promptNew(): def promptNew():
filemode = raw_input('Make new File? (y/n): ') filemode = raw_input('Make new File? (y/n): ')
if filemode == 'y': if filemode == 'y':
return True return True
elif filemode == 'n': elif filemode == 'n':
return False return False
else: else:
print('incorrect input') print('incorrect input')
promptNew() promptNew()
def addName(newName): def addName(newName):
with open("faces/names.json", "r") as read_file:
exist = False with open("faces/names.json", "r") as read_file:
namesJson = json.load(read_file) exist = False
names = list(namesJson) namesJson = json.load(read_file)
i = 0 names = list(namesJson)
for name in names: i = 0
if newName == name: for name in names:
exist = True if newName == name:
print('name already in names list with index ' + str(i)) exist = True
i += 1 print('name already in names list with index '+str(i))
if not exist: i += 1
names.append(newName) if not exist:
print('Added ' + newName + 'to names.json file') names.append(newName)
with open("faces/names.json", "w") as write_file: print('Added '+newName+'to names.json file')
json.dump(names, write_file) with open("faces/names.json","w") as write_file:
json.dump(names,write_file)
start()