Compare commits
2 Commits
master
...
webcam-sup
Author | SHA1 | Date | |
---|---|---|---|
617bb779c0 | |||
83de5c2894 |
@ -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.
|
61
main.py
61
main.py
@ -7,28 +7,51 @@ import sys
|
|||||||
import json
|
import json
|
||||||
import datetime
|
import datetime
|
||||||
|
|
||||||
|
piCamAvailable = False
|
||||||
|
|
||||||
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
|
piCamAvailable = True
|
||||||
|
|
||||||
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))
|
||||||
|
piCamAvailable = False;
|
||||||
|
|
||||||
|
try:
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
print('Could not load OpenCV\Closing')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
go = True
|
go = True
|
||||||
print('Initilizing..')
|
piCam = False
|
||||||
|
WebCam = False
|
||||||
|
print('Initilizing Camera ..')
|
||||||
|
if piCamAvailable:
|
||||||
try:
|
try:
|
||||||
camera = PiCamera()
|
camera = PiCamera()
|
||||||
camera.resolution = (640, 480)
|
camera.resolution = (640, 480)
|
||||||
sleep(0.2)
|
go = True
|
||||||
print('[ OK ] Camera')
|
piCam = True
|
||||||
|
print('[ OK ] Started PiCamera')
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Can not initialize PiCamera')
|
print('[ Warning ] Could not start PiCamera. Trying Webcam.. ')
|
||||||
|
piCam = False
|
||||||
go = False
|
go = False
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
camera = cv2.VideoCapture()
|
||||||
|
print('[ OK ] WebCamera')
|
||||||
|
go = True
|
||||||
|
WebCam = True
|
||||||
|
except Exception as ex:
|
||||||
|
print('[ Error ] Could not start Webcam.. \nExiting')
|
||||||
|
go = False
|
||||||
|
sleep(0.2)
|
||||||
try:
|
try:
|
||||||
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
||||||
sleep(1)
|
sleep(1)
|
||||||
@ -41,30 +64,42 @@ def init():
|
|||||||
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, WebCam)
|
||||||
|
|
||||||
else:
|
else:
|
||||||
print('closing')
|
print('closing')
|
||||||
|
|
||||||
def start( camera, face_cascade):
|
def start( camera, face_cascade, WebCam):
|
||||||
j = 0
|
j = 0
|
||||||
|
if not WebCam:
|
||||||
rawCapture = PiRGBArray(camera)
|
rawCapture = PiRGBArray(camera)
|
||||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
# face_recognizer = cv2.face.LBPHFaceRecognizer_create() #cv2.face.createLBPHFaceRecognizer()
|
||||||
|
face_recognizer = cv2.face_LBPHFaceRecognizer('faces/h')
|
||||||
|
# face_recognizer.read('faces/h') # Try on Pi
|
||||||
|
##WIP
|
||||||
|
try:
|
||||||
face_recognizer.load('faces/h')
|
face_recognizer.load('faces/h')
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
|
||||||
names = loadNames()
|
names = loadNames()
|
||||||
while 1:
|
while 1:
|
||||||
try:
|
try:
|
||||||
|
if not WebCam:
|
||||||
camera.capture(rawCapture, format="bgr")
|
camera.capture(rawCapture, format="bgr")
|
||||||
|
img = rawCapture.array
|
||||||
|
else:
|
||||||
|
ret, img = camera.read()
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Can not capture image. Restarting..')
|
print('[ Error ] Can not capture image. Restarting..')
|
||||||
init()
|
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.3,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
|
||||||
@ -110,8 +145,6 @@ def recognize(image,face_recognizer, names):
|
|||||||
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)
|
||||||
|
13
trainer.py
13
trainer.py
@ -7,10 +7,10 @@ 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): '))
|
||||||
@ -32,14 +32,12 @@ def start():
|
|||||||
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'
|
||||||
@ -47,9 +45,9 @@ def addPerson(label, newFileName, 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')
|
||||||
@ -78,7 +76,6 @@ def addPerson(label, newFileName, oldFileName):
|
|||||||
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')
|
||||||
@ -100,7 +97,6 @@ def capture(count):
|
|||||||
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':
|
||||||
@ -111,8 +107,8 @@ def promptNew():
|
|||||||
print('incorrect input')
|
print('incorrect input')
|
||||||
promptNew()
|
promptNew()
|
||||||
|
|
||||||
|
|
||||||
def addName(newName):
|
def addName(newName):
|
||||||
|
|
||||||
with open("faces/names.json", "r") as read_file:
|
with open("faces/names.json", "r") as read_file:
|
||||||
exist = False
|
exist = False
|
||||||
namesJson = json.load(read_file)
|
namesJson = json.load(read_file)
|
||||||
@ -129,5 +125,4 @@ def addName(newName):
|
|||||||
with open("faces/names.json","w") as write_file:
|
with open("faces/names.json","w") as write_file:
|
||||||
json.dump(names,write_file)
|
json.dump(names,write_file)
|
||||||
|
|
||||||
|
|
||||||
start()
|
start()
|
||||||
|
Loading…
Reference in New Issue
Block a user