added names.json

This commit is contained in:
HeshamTB 2018-11-23 19:28:15 +00:00
parent 02930cd5a2
commit 6ac1b416c0
2 changed files with 48 additions and 42 deletions

59
main.py
View File

@ -4,6 +4,7 @@ from io import BytesIO
import os
import serial
import sys
import json
try:
import numpy as np
@ -46,18 +47,21 @@ def init():
def start( camera, face_cascade):
j = 0
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:
#camera.capture('img.jpg')
#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, 4)
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)
@ -78,44 +82,19 @@ 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/Hesham-Saeed')
img = image.copy()
face_recognizer.load('faces/m')
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)
@ -125,5 +104,11 @@ def OpenShifter():
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)