merge #3

Merged
Hesham merged 3 commits from perf into master 2018-11-24 16:22:08 +01:00
3 changed files with 84 additions and 49 deletions

View File

@ -3,24 +3,28 @@ const int trigPin = 9;
const int echoPin = 10;
const int ProxSensor = 2;
const int relayPin = 12;
const int buzzerPin = 3;
int inputVal = 0;
long duration;
int distance;
char piInput = 'N';
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(relayPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(ProxSensor,INPUT);
digitalWrite(buzzerPin, LOW);
digitalWrite(relayPin, LOW);
Serial.begin(9600); // Starts the serial communication
pinMode(ProxSensor,INPUT); //Pin 2 is connected to the output of proximity sensor
Serial.begin(9600);
}
void loop() {
boolean IROK = false;
boolean UltraOK = false;
// Clears the trigPin
@ -45,6 +49,7 @@ void loop() {
if (IROK && UltraOK && Serial.available() > 0 ){
if (Serial.read() == 'O') {
digitalWrite(relayPin, HIGH);
playBuzzer(2,false);
delay(5000);
digitalWrite(relayPin, LOW);
IROK = false;
@ -64,7 +69,26 @@ void serialFlush(){
while(Serial.available() > 0) {
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
View File

@ -4,10 +4,12 @@ from io import BytesIO
import os
import serial
import sys
import json
try:
import numpy as np
from picamera import PiCamera
from picamera.array import PiRGBArray
import cv2
except Exception as ex:
@ -20,7 +22,7 @@ def init():
try:
camera = PiCamera()
camera.resolution = (640, 480)
sleep(2)
sleep(0.2)
print('[ OK ] Camera')
except:
print('[ Error ] Can not initialize PiCamera')
@ -44,16 +46,22 @@ def init():
def start( camera, face_cascade):
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:
camera.capture('img.jpg')
img = cv2.imread('img.jpg',1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.1, 4)
#camera.capture('img.jpg')
#img = cv2.imread('img.jpg',1)
camera.capture(rawCapture, format="bgr")
img = rawCapture.array
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.2,5)
i = 0;
for (x,y,w,h) in faces:
i += 1
j += 1
j += 1
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
person = recognize(roi_gray)
@ -65,6 +73,7 @@ def start( camera, face_cascade):
cv2.imshow('image',img)
cv2.waitKey(100)
rawCapture.truncate(0)
if cv2.waitKey(1) & 0xFF == ord('q'):
cv2.destroyAllWindows()
break
@ -73,52 +82,33 @@ def start( camera, face_cascade):
#print('[ Error ] Unexpected exception')
#print('Closing')
#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):
subjects = ['','Hesham','Saeed']
names = loadNames()
face_recognizer = cv2.createLBPHFaceRecognizer()
face_recognizer.load('faces/m')
img = image.copy()
img = image.copy()
label= face_recognizer.predict(img)
print(label)
if label[0] == 1 and int(label[1]) < 120:
return 'Hesham'
elif label[0] == 2 and int(label[1]) < 120:
return 'Safwan'
elif label[0] == 4 and int(label[1]) < 120:
return 'Hesham2'
if label[1] > 120:
return 'unknown'
else:
return 'Unknown'
return names[label[0]]
def OpenShifter():
try:
ser = serial.Serial('/dev/ttyUSB1',9600)
ser = serial.Serial('/dev/ttyUSB0',9600)
#ser.open()
ser.write('O')
ser.close()
except Exception as ex:
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()

View File

@ -5,6 +5,7 @@ import cv2
import numpy as np
import os
import sys
import json
picCount = 0
@ -13,6 +14,7 @@ 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()
@ -25,11 +27,11 @@ def start():
oldFileName = str(raw_input('Old File name: '))
newFileName = str(raw_input('New File name: '))
clearTrainFolder()
print('Press any key to start capture..')
dum = input('')
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") ]
@ -64,6 +66,8 @@ def addPerson(label, newFileName, oldFileName):
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))
@ -102,5 +106,22 @@ def promptNew():
else:
print('incorrect input')
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)