Integrated hardware commands to Facial Recognition Software.

This commit is contained in:
Batuhan Berk Başoğlu 2020-11-14 16:38:19 -05:00
parent c5bf048621
commit cf228272a8
7 changed files with 170 additions and 177 deletions

View file

@ -7,11 +7,13 @@ import numpy as np
import Facial_Recognition_Render as fr
import _pickle as cPickle
import glob
'import Hardware.Motor' #Line 225-228
faceWidth = 320
faceHeight = 320
SKIP_FRAMES = 1
def alignFace(imFace, landmarks):
l_x = landmarks[39][0]
l_y = landmarks[39][1]
@ -22,19 +24,19 @@ def alignFace(imFace, landmarks):
# Convert from radians to degrees
angle = math.atan2(dy, dx) * 180.0 / math.pi
eyesCenter = ((l_x + r_x)*0.5, (l_y + r_y)*0.5)
eyesCenter = ((l_x + r_x) * 0.5, (l_y + r_y) * 0.5)
rotMatrix = cv2.getRotationMatrix2D(eyesCenter, angle, 1)
alignedImFace = np.zeros(imFace.shape, dtype=np.uint8)
alignedImFace = cv2.warpAffine(imFace, rotMatrix, (imFace.shape[1],imFace.shape[0]))
alignedImFace = cv2.warpAffine(imFace, rotMatrix, (imFace.shape[1], imFace.shape[0]))
return alignedImFace
def face_detector_haarcascade(image):
def face_detector_haarcascade(image):
grey = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
resize_fx = 1
resize_fy = 1
grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation = cv2.INTER_AREA)
grey = cv2.resize(grey, dsize=None, fx=resize_fx, fy=resize_fy, interpolation=cv2.INTER_AREA)
pwd = sys.path[0]
classfier = cv2.CascadeClassifier(pwd + "/Facial_models/haarcascade_frontalface_alt2.xml")
@ -44,30 +46,31 @@ def face_detector_haarcascade(image):
if len(faceRects) > 0:
for faceRect in faceRects:
x, y, w, h = faceRect
x = int(x/resize_fx)
y = int(y/resize_fy)
w = int(w/resize_fx)
h = int(h/resize_fy)
x = int(x / resize_fx)
y = int(y / resize_fy)
w = int(w / resize_fx)
h = int(h / resize_fy)
cv2.rectangle(image, (x - 10, y - 10), (x + w + 10, y + h + 10), (0, 255, 0), 5)
return image
def face_detector_ssd(image):
def face_detector_ssd(image):
pwd = sys.path[0]
net = cv2.dnn.readNetFromCaffe(pwd+"/Facial_models/deploy.prototxt", pwd+"/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel")
net = cv2.dnn.readNetFromCaffe(pwd + "/Facial_models/deploy.prototxt",
pwd + "/Facial_models/res10_300x300_ssd_iter_140000_fp16.caffemodel")
resize = (300, 300)
confidence_thres = 0.65
blob = cv2.dnn.blobFromImage(cv2.resize(image, dsize=resize), 1.0, resize, (104.0, 177.0, 123.0))
# blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
# blob = cv2.dnn.blobFromImage(cv2.resize(image, (300, 300)), 1.0, (300, 300), (104.0, 177.0, 123.0))
net.setInput(blob)
detections = net.forward()
h,w,c=image.shape
h, w, c = image.shape
for i in range(0, detections.shape[2]):
confidence = detections[0, 0, i, 2]
@ -76,13 +79,14 @@ def face_detector_ssd(image):
(startX, startY, endX, endY) = box.astype("int")
text = "{:.2f}%".format(confidence * 100)
y = startY - 10 if startY - 10 > 10 else startY + 10
cv2.rectangle(image, (startX, startY), (endX, endY),(0, 255,0), 5)
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 255, 0), 5)
cv2.putText(image, text, (startX, y), cv2.FONT_HERSHEY_SIMPLEX, 1.00, (0, 255, 0), 3)
return image
def training_data_loader():
imagesFolder = sys.path[0]+"/Facial_images/face_rec/train/"
imagesFolder = sys.path[0] + "/Facial_images/face_rec/train/"
subfolders = []
for x in os.listdir(imagesFolder):
@ -107,7 +111,7 @@ def training_data_loader():
labelsFaceTrain = []
faceDetector = dlib.get_frontal_face_detector()
landmarkDetector = dlib.shape_predictor(sys.path[0]+"/Facial_models/shape_predictor_68_face_landmarks.dat")
landmarkDetector = dlib.shape_predictor(sys.path[0] + "/Facial_models/shape_predictor_68_face_landmarks.dat")
for j, imagePath in enumerate(imagePaths):
im = cv2.imread(imagePath, 0)
@ -120,10 +124,10 @@ def training_data_loader():
if len(landmarks) == 68:
x1Limit = landmarks[0][0] - (landmarks[36][0] - landmarks[0][0])
x2Limit = landmarks[16][0] + (landmarks[16][0] - landmarks[45][0])
y1Limit = landmarks[27][1] - 3*(landmarks[30][1] - landmarks[27][1])
y1Limit = landmarks[27][1] - 3 * (landmarks[30][1] - landmarks[27][1])
y2Limit = landmarks[8][1] + (landmarks[30][1] - landmarks[29][1])
x1 = max(x1Limit,0)
x1 = max(x1Limit, 0)
x2 = min(x2Limit, imWidth)
y1 = max(y1Limit, 0)
y2 = min(y2Limit, imHeight)
@ -132,63 +136,64 @@ def training_data_loader():
alignedFace = alignFace(imFace, landmarks)
alignedFace = cv2.resize(alignedFace, (faceHeight, faceWidth))
imagesFaceTrain.append(np.float32(alignedFace)/255.0)
imagesFaceTrain.append(np.float32(alignedFace) / 255.0)
labelsFaceTrain.append(labels[j])
return imagesFaceTrain, labelsFaceTrain, labelsMap
def training_recognizer(rec_type):
def training_recognizer(rec_type):
imagesFaceTrain, labelsFaceTrain, labelsMap = training_data_loader()
if (rec_type=='LBPH'):
if (rec_type == 'LBPH'):
faceRecognizer = cv2.face.LBPHFaceRecognizer_create()
print("Training using LBPH Faces")
elif (rec_type=='Eigen'):
elif (rec_type == 'Eigen'):
faceRecognizer = cv2.face.EigenFaceRecognizer_create()
print("Training using Eigen Faces")
elif (rec_type=='Fisher'):
elif (rec_type == 'Fisher'):
faceRecognizer = cv2.face.FisherFaceRecognizer_create()
print("Training using Fisher Faces")
faceRecognizer.train(imagesFaceTrain, np.array(labelsFaceTrain))
faceRecognizer.write(sys.path[0]+'/Facial_models/face_rec_model.yml')
faceRecognizer.write(sys.path[0] + '/Facial_models/face_rec_model.yml')
with open(sys.path[0]+'/Facial_models/labels_map.pkl', 'wb') as f:
with open(sys.path[0] + '/Facial_models/labels_map.pkl', 'wb') as f:
cPickle.dump(labelsMap, f)
def face_recognition_inference(rec_type):
#testFiles = glob.glob(sys.path[0]+'/Facial_test_images/face_rec/test/*.jpg')
#testFiles.sort()
# testFiles = glob.glob(sys.path[0]+'/Facial_test_images/face_rec/test/*.jpg')
# testFiles.sort()
i = 0
correct = 0
error = 0
faceDetector = dlib.get_frontal_face_detector()
print(sys.path[0])
landmarkDetector = dlib.shape_predictor(sys.path[0]+'/Facial_models/shape_predictor_68_face_landmarks.dat')
landmarkDetector = dlib.shape_predictor(sys.path[0] + '/Facial_models/shape_predictor_68_face_landmarks.dat')
if (rec_type=='LBPH'):
if (rec_type == 'LBPH'):
faceRecognizer = cv2.face.LBPHFaceRecognizer_create()
print("Test using LBPH Faces")
elif (rec_type=='Eigen'):
elif (rec_type == 'Eigen'):
faceRecognizer = cv2.face.EigenFaceRecognizer_create()
print("Test using Eigen Faces")
elif (rec_type=='Fisher'):
elif (rec_type == 'Fisher'):
faceRecognizer = cv2.face.FisherFaceRecognizer_create()
print("Test using Fisher Faces")
faceRecognizer.read(sys.path[0]+'/Facial_models/face_rec_model.yml')
labelsMap = np.load(sys.path[0]+'/Facial_models/labels_map.pkl', allow_pickle=True)
faceRecognizer.read(sys.path[0] + '/Facial_models/face_rec_model.yml')
labelsMap = np.load(sys.path[0] + '/Facial_models/labels_map.pkl', allow_pickle=True)
cam = cv2.VideoCapture(0)
while(True):
#imagePath = testFiles[i]
while (True):
# imagePath = testFiles[i]
success, original = cam.read()
im = cv2.resize(original, (640, 480))
i += 1
im = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY)
imHeight, imWidth = im.shape[:2]
landmarks = fr.getLandmarks(faceDetector, landmarkDetector, im)
@ -197,10 +202,10 @@ def face_recognition_inference(rec_type):
if len(landmarks) == 68:
x1Limit = landmarks[0][0] - (landmarks[36][0] - landmarks[0][0])
x2Limit = landmarks[16][0] + (landmarks[16][0] - landmarks[45][0])
y1Limit = landmarks[27][1] - 3*(landmarks[30][1] - landmarks[27][1])
y1Limit = landmarks[27][1] - 3 * (landmarks[30][1] - landmarks[27][1])
y2Limit = landmarks[8][1] + (landmarks[30][1] - landmarks[29][1])
x1 = max(x1Limit,0)
x1 = max(x1Limit, 0)
x2 = min(x2Limit, imWidth)
y1 = max(y1Limit, 0)
y2 = min(y2Limit, imHeight)
@ -208,26 +213,29 @@ def face_recognition_inference(rec_type):
alignedFace = alignFace(imFace, landmarks)
alignedFace = cv2.resize(alignedFace, (faceHeight, faceWidth))
imFaceFloat = np.float32(alignedFace)/255.0
imFaceFloat = np.float32(alignedFace) / 255.0
predictedLabel = -1
predictedLabel, score = faceRecognizer.predict(imFaceFloat)
center = ( int((x1 + x2) /2), int((y1 + y2)/2) )
radius = int((y2-y1)/2.0)
text = '{} {}%'.format(labelsMap[predictedLabel],round(score, 5))
center = (int((x1 + x2) / 2), int((y1 + y2) / 2))
radius = int((y2 - y1) / 2.0)
text = '{} {}%'.format(labelsMap[predictedLabel], round(score, 5))
cv2.rectangle(original, (x1, y1), (x2, y2), (0, 255, 0), 5)
cv2.putText(original, text, (x1, y1-10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 3)
cv2.putText(original, text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 255, 0), 3)
'Hardware.Motor.Motor.stop_motor()'
'Hardware.Motor.Motor.start_motor()'
'Hardware.Motor.Motor.stop_motor()'
'Hardware.Motor.Motor.start_alarm()'
cv2.imshow('Face Recognition Demo', original)
k = cv2.waitKey(10)
cam.release()
cv2.destroyAllWindows()
if __name__=="__main__":
if __name__ == "__main__":
mode = 'test'
rec_type = 'Fisher' # 'LBPH' 'Fisher' 'Eigen'
@ -236,10 +244,6 @@ if __name__=="__main__":
elif (mode == 'test'):
face_recognition_inference(rec_type)
# video process (keep it in case if needed)
'''
cameraCapture = cv2.VideoCapture(1)
@ -274,6 +278,3 @@ if __name__=="__main__":
cv2.waitKey()
cv2.destroyAllWindows()
'''

View file

@ -1,105 +0,0 @@
import RPi.GPIO as GPIO
from time import sleep
class Motor:
print("Starting of the program")
def __init__(self):
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
#preset GPIO ports for 2 motors
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
# preset the port for buttons and alarm
GPIO.setup(5,GPIO.IN) # start motor button, initially True
GPIO.setup(13,GPIO.IN) # stop motor button, initially True
GPIO.setup(16,GPIO.IN) # start alarm button, initially True
GPIO.setup(26,GPIO.OUT) # alarm output
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
#utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN1.start(0)
self.EN2.start(0)
#stop signals for motors and alarm
self.motorStop=False
self.alarmStop=False
def start_motor(self):
while (not self.motorStop) or (not GPIO.input(5)): #break the loop when motor stop signal is detected
print ("FORWARD MOTION")
self.motorStop=self.stop_motor()
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
GPIO.cleanup()
def stop_motor(self):
userStop=input("Stop the motor? choose between Y/N")
if (userStop=="Y") or (not GPIO.input(13)):
print("stopping motor...")
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
print("motor stops")
return True
elif userStop=="N":
return False
else:
self.stop_motor(self)
def start_alarm(self):
while (not self.alarmStop) or (not GPIO.input(16)):
self.alarmStop=self.stop_alarm()
GPIO.output(26,True)
GPIO.cleanup()
def stop_alarm(self):
stopRequest=input("Turn off the alarm? choose between Y/N")
if stopRequest=="Y":
print("Alarm turning off...")
GPIO.output(26,False)
print("Alarm is off")
return True
elif stopRequest=="N":
return False
else:
self.stop_alarm()
if __name__=="__main__":
#print("Execute function...")
motor1=Motor()
#motor1.start_motor()
motor1.start_alarm()

98
Hardware/Motor.py Normal file
View file

@ -0,0 +1,98 @@
import RPi.GPIO as GPIO
from time import sleep
class Motor:
def __init__(self):
print("Starting of the program")
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
# preset GPIO ports for 2 motors
self.Motor1 = {'EN': 25, 'input1': 24, 'input2': 23}
self.Motor2 = {'EN': 17, 'input1': 27, 'input2': 22}
# preset the port for buttons and alarm
GPIO.setup(5, GPIO.IN) # start motor button, initially True
GPIO.setup(13, GPIO.IN) # stop motor button, initially True
GPIO.setup(16, GPIO.IN) # start alarm button, initially True
GPIO.setup(26, GPIO.OUT) # alarm output
for x in self.Motor1:
GPIO.setup(self.Motor1[x], GPIO.OUT)
GPIO.setup(self.Motor2[x], GPIO.OUT)
# utilize PWM function, enable motors and frequency is 100Hz
self.EN1 = GPIO.PWM(self.Motor1['EN'], 100)
self.EN2 = GPIO.PWM(self.Motor2['EN'], 100)
self.EN1.start(0)
self.EN2.start(0)
# stop signals for motors and alarm
self.motorStop = False
self.alarmStop = False
def start_motor(self):
while (not self.motorStop) or (not GPIO.input(5)): # break the loop when motor stop signal is detected
print("FORWARD MOTION")
self.motorStop = self.stop_motor()
self.EN1.ChangeDutyCycle(50)
self.EN2.ChangeDutyCycle(50)
GPIO.output(self.Motor1['input1'], GPIO.HIGH)
GPIO.output(self.Motor1['input2'], GPIO.LOW)
GPIO.output(self.Motor2['input1'], GPIO.HIGH)
GPIO.output(self.Motor2['input2'], GPIO.LOW)
GPIO.cleanup()
def stop_motor(self):
userStop = input("Stop the motor? choose between Y/N")
if (userStop == "Y") or (not GPIO.input(13)):
print("stopping motor...")
self.EN1.ChangeDutyCycle(0)
self.EN2.ChangeDutyCycle(0)
print("motor stops")
return True
elif userStop == "N":
return False
else:
self.stop_motor(self)
def start_alarm(self):
while (not self.alarmStop) or (not GPIO.input(16)):
self.alarmStop = self.stop_alarm()
GPIO.output(26, True)
GPIO.cleanup()
def stop_alarm(self):
stopRequest = input("Turn off the alarm? choose between Y/N")
if stopRequest == "Y":
print("Alarm turning off...")
GPIO.output(26, False)
print("Alarm is off")
return True
elif stopRequest == "N":
return False
else:
self.stop_alarm()
if __name__ == "__main__":
# print("Execute function...")
motor1 = Motor()
# motor1.start_motor()
motor1.start_alarm()

Binary file not shown.

View file

@ -8,24 +8,23 @@ def start():
count = 0
users = DBHelper.db.child("Users").get()
try:
for user in users.each():
for x in users.each():
count = +1
for x in range(20):
for y in range(20):
if not os.path.isdir("Facial_images/face_rec/train/User_" + str(count)):
os.makedirs("Photos_of_Users/User_" + str(count))
DBHelper.download_user_photo("User_" + str(count) + "/" + str(x) + ".jpg")
DBHelper.download_user_photo("User_" + str(count) + "/" + str(y) + ".jpg")
except:
print("No Users are registered.")
count = 0
try:
for user in users.each():
for x in users.each():
count = +1
for x in range(20):
for y in range(20):
if not os.path.isdir("Photos_of_Thieves/Thief_" + str(count)):
os.makedirs("Photos_of_Thieves/Thief_" + str(count))
DBHelper.download_thief_photo("Thief_" + str(count) + "/" + str(x) + ".jpg")
DBHelper.download_thief_photo("Thief_" + str(count) + "/" + str(y) + ".jpg")
except:
print("No Thieves for now.")
Facial_Recognition_Wrapper.training_recognizer("Fisher")
Facial_Recognition_Wrapper.face_recognition_inference("Fisher")