Compare commits
1 Commits
master
...
fix-import
Author | SHA1 | Date | |
---|---|---|---|
e17b2bf461 |
35
main.py
35
main.py
@ -1,21 +1,20 @@
|
|||||||
|
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from PIL import Image
|
|
||||||
import os
|
|
||||||
import serial
|
|
||||||
import sys
|
import sys
|
||||||
|
import os
|
||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from picamera import PiCamera
|
from picamera import PiCamera
|
||||||
from picamera.array import PiRGBArray
|
from picamera.array import PiRGBArray
|
||||||
import cv2
|
import serial
|
||||||
|
import cv2
|
||||||
|
|
||||||
except Exception as ex:
|
except Exception as ex:
|
||||||
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
|
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
go = True
|
go = True
|
||||||
@ -36,17 +35,17 @@ def init():
|
|||||||
except:
|
except:
|
||||||
print('[ Error ] Can not load cascade File')
|
print('[ Error ] Can not load cascade File')
|
||||||
go = False
|
go = False
|
||||||
try:
|
try:
|
||||||
os.mkdir('unknown')
|
os.mkdir('unknown')
|
||||||
except OSError as ex:
|
except OSError as ex:
|
||||||
print('Found (unknown) folder')
|
print('Found (unknown) folder')
|
||||||
|
|
||||||
if (go):
|
if (go):
|
||||||
print('Starting Photo loop..')
|
print('Starting Photo loop..')
|
||||||
print('Known people are '+str(loadNames()))
|
print('Known people are '+str(loadNames()))
|
||||||
start(camera, face_cascade)
|
start(camera, face_cascade)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('closing')
|
print('closing')
|
||||||
|
|
||||||
def start( camera, face_cascade):
|
def start( camera, face_cascade):
|
||||||
@ -88,16 +87,16 @@ def start( camera, face_cascade):
|
|||||||
else:
|
else:
|
||||||
print('Found '+person)
|
print('Found '+person)
|
||||||
OpenShifter()
|
OpenShifter()
|
||||||
cv2.imshow('image',img)
|
cv2.imshow('image',img)
|
||||||
cv2.waitKey(1)
|
cv2.waitKey(1)
|
||||||
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
|
||||||
|
|
||||||
|
|
||||||
def recognize(image,face_recognizer, names):
|
def recognize(image,face_recognizer, names):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
img = image.copy()
|
img = image.copy()
|
||||||
label= face_recognizer.predict(img)
|
label= face_recognizer.predict(img)
|
||||||
@ -127,7 +126,7 @@ def OpenShifter():
|
|||||||
ser.close()
|
ser.close()
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
|
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
|
||||||
|
|
||||||
def loadNames():
|
def loadNames():
|
||||||
try:
|
try:
|
||||||
with open("faces/names.json", "r") as read_file:
|
with open("faces/names.json", "r") as read_file:
|
||||||
|
211
trainer.py
211
trainer.py
@ -7,127 +7,122 @@ 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()
|
start()
|
||||||
|
Loading…
Reference in New Issue
Block a user