Compare commits
48 Commits
Author | SHA1 | Date | |
---|---|---|---|
fb84cf1bb5 | |||
a82b98154f | |||
4441616ef1 | |||
96c2d4ccc3 | |||
00b476ee0b | |||
c43128f6f5 | |||
48dbd127a2 | |||
|
e6de56c167 | ||
883d8a6697 | |||
|
d11220d70c | ||
29fa3a3335 | |||
|
ef8332f253 | ||
|
3660a2e9eb | ||
|
7d37fa8d91 | ||
|
8ff03abb53 | ||
|
c6c7a80cee | ||
|
e773fc6a8e | ||
|
4f88480583 | ||
|
a8990873ba | ||
|
8e351a1df0 | ||
|
30c8e4b074 | ||
03490f1a25 | |||
|
690ca3493f | ||
|
6e8adb3bb1 | ||
|
6ac1b416c0 | ||
|
02930cd5a2 | ||
a0cd73588c | |||
|
9c6e3999b5 | ||
|
202a3e81d8 | ||
|
a8d42397f2 | ||
|
814d3d2dd1 | ||
|
2adae62a29 | ||
|
f7faddb00d | ||
|
ecd7ad2c48 | ||
|
50bb02ab67 | ||
3f710e9ab3 | |||
9f75eda8a5 | |||
20270cc73a | |||
ac35a52854 | |||
b0ae2b55a5 | |||
8240a81988 | |||
fc85335b59 | |||
9b5c14197f | |||
9461e746c8 | |||
|
41cd83cc1a | ||
|
6b1ac5e3bc | ||
4cc4d6e8be | |||
547a5ee001 |
5
.gitignore
vendored
5
.gitignore
vendored
@ -1 +1,4 @@
|
|||||||
*.[Jj][Pp][Gg]
|
*.[Jj][Pp][Gg]
|
||||||
|
*.pyc
|
||||||
|
*.suo
|
||||||
|
*.png
|
17
.project
Normal file
17
.project
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>FaceRecognition-python</name>
|
||||||
|
<comment></comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.python.pydev.PyDevBuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.python.pydev.pythonNature</nature>
|
||||||
|
</natures>
|
||||||
|
</projectDescription>
|
5
.pydevproject
Normal file
5
.pydevproject
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
|
||||||
|
<?eclipse-pydev version="1.0"?><pydev_project>
|
||||||
|
<pydev_property name="org.python.pydev.PYTHON_PROJECT_INTERPRETER">Default</pydev_property>
|
||||||
|
<pydev_property name="org.python.pydev.PYTHON_PROJECT_VERSION">python interpreter</pydev_property>
|
||||||
|
</pydev_project>
|
26
FaceRecognition-python.pyproj
Normal file
26
FaceRecognition-python.pyproj
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
|
||||||
|
<PropertyGroup>
|
||||||
|
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
|
||||||
|
<SchemaVersion>2.0</SchemaVersion>
|
||||||
|
<ProjectGuid>{c5bcf1ad-e7a4-41be-8f0e-28db4b669eec}</ProjectGuid>
|
||||||
|
<ProjectHome />
|
||||||
|
<StartupFile>main.py</StartupFile>
|
||||||
|
<SearchPath />
|
||||||
|
<WorkingDirectory>.</WorkingDirectory>
|
||||||
|
<OutputPath>.</OutputPath>
|
||||||
|
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
|
||||||
|
<LaunchProvider>Standard Python launcher</LaunchProvider>
|
||||||
|
<InterpreterId />
|
||||||
|
</PropertyGroup>
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
|
||||||
|
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
|
||||||
|
<PropertyGroup>
|
||||||
|
<VisualStudioVersion Condition=" '$(VisualStudioVersion)' == '' ">10.0</VisualStudioVersion>
|
||||||
|
</PropertyGroup>
|
||||||
|
<ItemGroup>
|
||||||
|
<Compile Include="main.py" />
|
||||||
|
<Compile Include="Trainer.py" />
|
||||||
|
</ItemGroup>
|
||||||
|
<Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\Python Tools\Microsoft.PythonTools.targets" />
|
||||||
|
</Project>
|
23
FaceRecognition-python.sln
Normal file
23
FaceRecognition-python.sln
Normal file
@ -0,0 +1,23 @@
|
|||||||
|
|
||||||
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
|
# Visual Studio 15
|
||||||
|
VisualStudioVersion = 15.0.28307.102
|
||||||
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
|
Project("{888888A0-9F3D-457C-B088-3A5042F75D52}") = "FaceRecognition-python", "FaceRecognition-python.pyproj", "{C5BCF1AD-E7A4-41BE-8F0E-28DB4B669EEC}"
|
||||||
|
EndProject
|
||||||
|
Global
|
||||||
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
|
Debug|Any CPU = Debug|Any CPU
|
||||||
|
Release|Any CPU = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ProjectConfigurationPlatforms) = postSolution
|
||||||
|
{C5BCF1AD-E7A4-41BE-8F0E-28DB4B669EEC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{C5BCF1AD-E7A4-41BE-8F0E-28DB4B669EEC}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
|
HideSolutionNode = FALSE
|
||||||
|
EndGlobalSection
|
||||||
|
GlobalSection(ExtensibilityGlobals) = postSolution
|
||||||
|
SolutionGuid = {29568353-A5E1-4ECF-9F70-31492869FB0D}
|
||||||
|
EndGlobalSection
|
||||||
|
EndGlobal
|
1
README.md
Normal file
1
README.md
Normal file
@ -0,0 +1 @@
|
|||||||
|
### Python code to run in Raspberry Pi with PiCamera to recognize certin faces.
|
98
SensorsArd/SensorsArd.ino
Normal file
98
SensorsArd/SensorsArd.ino
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
|
||||||
|
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;
|
||||||
|
boolean IROK = false;
|
||||||
|
boolean UltraOK = false;
|
||||||
|
char pi = 'N';
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
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);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void loop() {
|
||||||
|
|
||||||
|
IROK = false;
|
||||||
|
UltraOK = false;
|
||||||
|
pi = 'N';
|
||||||
|
|
||||||
|
distance = readDistance();
|
||||||
|
if (distance < 70) UltraOK = true;
|
||||||
|
else UltraOK = false;
|
||||||
|
if (digitalRead(ProxSensor) == LOW ) IROK = true;
|
||||||
|
else IROK = false;
|
||||||
|
|
||||||
|
|
||||||
|
if (Serial.available() > 0) {
|
||||||
|
pi = Serial.read();
|
||||||
|
}
|
||||||
|
|
||||||
|
delay(500);
|
||||||
|
|
||||||
|
Serial.println(pi);
|
||||||
|
Serial.println(IROK);
|
||||||
|
Serial.println(UltraOK);
|
||||||
|
|
||||||
|
if (IROK && UltraOK && pi == 'o') {
|
||||||
|
openShifter();
|
||||||
|
}
|
||||||
|
IROK = false;
|
||||||
|
UltraOK = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void openShifter() {
|
||||||
|
digitalWrite(relayPin, HIGH);
|
||||||
|
playBuzzer(300, true);
|
||||||
|
delay(7000);
|
||||||
|
digitalWrite(relayPin, LOW);
|
||||||
|
IROK = false;
|
||||||
|
UltraOK = false;
|
||||||
|
pi = 'N';
|
||||||
|
serialFlush();
|
||||||
|
}
|
||||||
|
|
||||||
|
void serialFlush() {
|
||||||
|
while (Serial.available() > 0) {
|
||||||
|
char t = Serial.read();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void playBuzzer(int time, boolean ok) {
|
||||||
|
tone(buzzerPin, 3000);
|
||||||
|
delay(time / 2);
|
||||||
|
noTone(buzzerPin);
|
||||||
|
delay(100);
|
||||||
|
tone(buzzerPin, 3000);
|
||||||
|
delay(time / 2);
|
||||||
|
noTone(buzzerPin);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
int readDistance() {
|
||||||
|
// Clears the trigPin
|
||||||
|
digitalWrite(trigPin, LOW);
|
||||||
|
delayMicroseconds(2);
|
||||||
|
// Sets the trigPin on HIGH state for 10 micro seconds
|
||||||
|
digitalWrite(trigPin, HIGH);
|
||||||
|
delayMicroseconds(10);
|
||||||
|
digitalWrite(trigPin, LOW);
|
||||||
|
// Reads the echoPin, returns the sound wave travel time in microseconds
|
||||||
|
duration = pulseIn(echoPin, HIGH);
|
||||||
|
// Calculating the distance
|
||||||
|
return duration * 0.034 / 2;
|
||||||
|
|
||||||
|
}
|
28
Trainer.py
28
Trainer.py
@ -1,28 +0,0 @@
|
|||||||
from time import sleep
|
|
||||||
from io import BytesIO
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
|
|
||||||
def train(label, path):
|
|
||||||
try:
|
|
||||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
|
||||||
face_recognizer.load('/faces/hesham-saeed2')
|
|
||||||
except:
|
|
||||||
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
|
|
||||||
images = []
|
|
||||||
labels = []
|
|
||||||
image_names = os.listdir(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)
|
|
||||||
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
|
||||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
|
||||||
for (x,y,w,h) in faces:
|
|
||||||
images.append(gray[y:y+h, x:x+w])
|
|
||||||
labels.append(label)
|
|
||||||
face_recognizer.train(images, np.array(labels))
|
|
||||||
|
|
||||||
|
|
68955
faces/Hesham-Saeed
Normal file
68955
faces/Hesham-Saeed
Normal file
File diff suppressed because it is too large
Load Diff
68955
faces/Hesham-Saeed2
Normal file
68955
faces/Hesham-Saeed2
Normal file
File diff suppressed because it is too large
Load Diff
1
faces/names.json
Normal file
1
faces/names.json
Normal file
@ -0,0 +1 @@
|
|||||||
|
["", "Hesham", "Aidarous"]
|
171
main.py
171
main.py
@ -1,18 +1,29 @@
|
|||||||
|
|
||||||
from time import sleep
|
from time import sleep
|
||||||
from picamera import PiCamera
|
from PIL import Image
|
||||||
from io import BytesIO
|
|
||||||
import cv2
|
|
||||||
import numpy as np
|
|
||||||
import os
|
import os
|
||||||
|
import serial
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
import datetime
|
||||||
|
|
||||||
|
try:
|
||||||
|
import numpy as np
|
||||||
|
from picamera import PiCamera
|
||||||
|
from picamera.array import PiRGBArray
|
||||||
|
import cv2
|
||||||
|
|
||||||
|
except Exception as ex:
|
||||||
|
print('[ Error ] some depandincies are missing\n'+ str(ex.args))
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
def init():
|
def init():
|
||||||
go = True
|
go = True
|
||||||
print('Initilizing Camera and cascade components..')
|
print('Initilizing..')
|
||||||
try:
|
try:
|
||||||
camera = PiCamera()
|
camera = PiCamera()
|
||||||
camera.resolution = (640, 480)
|
camera.resolution = (640, 480)
|
||||||
sleep(2)
|
sleep(0.2)
|
||||||
print('[ OK ] Camera')
|
print('[ OK ] Camera')
|
||||||
except:
|
except:
|
||||||
print('[ Error ] Can not initialize PiCamera')
|
print('[ Error ] Can not initialize PiCamera')
|
||||||
@ -25,79 +36,105 @@ def init():
|
|||||||
except:
|
except:
|
||||||
print('[ Error ] Can not load cascade File')
|
print('[ Error ] Can not load cascade File')
|
||||||
go = False
|
go = False
|
||||||
|
try:
|
||||||
|
os.mkdir('unknown')
|
||||||
|
except OSError as ex:
|
||||||
|
print('Found (unknown) folder')
|
||||||
|
|
||||||
if (go):
|
if (go):
|
||||||
print('Starting Photo loop..')
|
print('Starting Photo loop..')
|
||||||
|
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):
|
||||||
j = 0
|
j = 0
|
||||||
while True:
|
rawCapture = PiRGBArray(camera)
|
||||||
#try:
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||||
camera.capture('img.jpg')
|
face_recognizer.load('faces/h')
|
||||||
img = cv2.imread('img.jpg',1)
|
names = loadNames()
|
||||||
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
while 1:
|
||||||
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
try:
|
||||||
i = 0;
|
camera.capture(rawCapture, format="bgr")
|
||||||
for (x,y,w,h) in faces:
|
except:
|
||||||
i = i + 1
|
print('[ Error ] Can not capture image. Restarting..')
|
||||||
j = j + 1
|
init()
|
||||||
|
img = rawCapture.array
|
||||||
|
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
|
||||||
|
faces = face_cascade.detectMultiScale(gray, 1.3,5)
|
||||||
|
i = 0
|
||||||
|
for (x,y,w,h) in faces:
|
||||||
|
i += 1
|
||||||
|
j += 1
|
||||||
|
try:
|
||||||
roi_gray = gray[y:y+h, x:x+w]
|
roi_gray = gray[y:y+h, x:x+w]
|
||||||
roi_color = img[y:y+h, x:x+w]
|
roi_color = img[y:y+h, x:x+w]
|
||||||
|
person = recognize(roi_gray, face_recognizer, names)
|
||||||
|
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),1)
|
||||||
|
cv2.putText(img, person, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (0,255,0), 2)
|
||||||
|
except:
|
||||||
|
print('[ Error ] Recognition fail')
|
||||||
|
if person == 'unknown':
|
||||||
|
print('Found unknown person')
|
||||||
try:
|
try:
|
||||||
cv2.imwrite('train/'+str(j)+'.jpg',roi_color)
|
RGBImage = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
||||||
except Exception as ex:
|
image = Image.fromarray(RGBImage)
|
||||||
|
image.save('unknown/'+str(datetime.datetime.now())+'.png')
|
||||||
|
except Exception as ex :
|
||||||
|
print('[ Error ] Saving unkown fail')
|
||||||
print(ex)
|
print(ex)
|
||||||
print('Drawing on face ', i)
|
else:
|
||||||
|
print('Found '+person)
|
||||||
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
|
OpenShifter()
|
||||||
cv2.putText(img, recognize(roi_gray), (x, y), cv2.FONT_HERSHEY_PLAIN, 1.7, (255, 0, 0), 2)
|
cv2.imshow('image',img)
|
||||||
cv2.imshow('image',img)
|
cv2.waitKey(1)
|
||||||
cv2.waitKey(100)
|
rawCapture.truncate(0)
|
||||||
if cv2.waitKey(1) & 0xFF == ord('q'):
|
if cv2.waitKey(1) & 0xFF == ord('q'):
|
||||||
cv2.destroyAllWindows()
|
cv2.destroyAllWindows()
|
||||||
break
|
break
|
||||||
|
|
||||||
#except:
|
|
||||||
#print('[ Error ] Unexpected exception')
|
def recognize(image,face_recognizer, names):
|
||||||
#print('Closing')
|
|
||||||
#break
|
try:
|
||||||
|
img = image.copy()
|
||||||
|
label= face_recognizer.predict(img)
|
||||||
|
except:
|
||||||
|
print('[ Error ] Error in Recognize() function')
|
||||||
|
if label[1] > 70:
|
||||||
|
print(str(label))
|
||||||
|
return 'unknown'
|
||||||
|
else:
|
||||||
|
print(str(label) + ' >>'+names[label[0]])
|
||||||
|
return names[label[0]]
|
||||||
|
|
||||||
|
#def DrawSquares(image,gray,faces,color):
|
||||||
|
|
||||||
|
def OpenShifter():
|
||||||
|
try:
|
||||||
|
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/ttyUSB0 ..')
|
||||||
|
try:
|
||||||
|
ser = serial.Serial('/dev/ttyUSB1',9600)
|
||||||
|
ser.open()
|
||||||
|
ser.write('o')
|
||||||
|
ser.close()
|
||||||
|
except:
|
||||||
|
print('[ Error ] Can not connect to Arduino at /dev/ttyUSB1 ..')
|
||||||
|
|
||||||
def train(number):
|
def loadNames():
|
||||||
|
try:
|
||||||
|
with open("faces/names.json", "r") as read_file:
|
||||||
|
data = json.load(read_file)
|
||||||
|
return list(data)
|
||||||
|
except:
|
||||||
|
print('[ Error ] Error in loadNames() function')
|
||||||
|
|
||||||
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','Unknown']
|
|
||||||
face_recognizer = cv2.createLBPHFaceRecognizer()
|
|
||||||
face_recognizer.load('faces/hesham')
|
|
||||||
img = image.copy()
|
|
||||||
label= face_recognizer.predict(img)
|
|
||||||
if label[0] == 1:
|
|
||||||
return 'Hesham'
|
|
||||||
else:
|
|
||||||
return 'Unknown'
|
|
||||||
|
|
||||||
|
|
||||||
init()
|
init()
|
||||||
|
|
||||||
|
|
||||||
|
133
trainer.py
Normal file
133
trainer.py
Normal file
@ -0,0 +1,133 @@
|
|||||||
|
from time import sleep
|
||||||
|
from io import BytesIO
|
||||||
|
from picamera import PiCamera
|
||||||
|
import cv2
|
||||||
|
import numpy as np
|
||||||
|
import os
|
||||||
|
import sys
|
||||||
|
import json
|
||||||
|
|
||||||
|
picCount = 0
|
||||||
|
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()
|
||||||
|
if new:
|
||||||
|
newFileName = str(raw_input('New File name: '))
|
||||||
|
oldFileName = newFileName
|
||||||
|
else:
|
||||||
|
print('Found in faces:')
|
||||||
|
print(os.listdir('faces/'))
|
||||||
|
oldFileName = str(raw_input('Old File name: '))
|
||||||
|
newFileName = str(raw_input('New File name: '))
|
||||||
|
clearTrainFolder()
|
||||||
|
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")]
|
||||||
|
for f in filelist:
|
||||||
|
os.remove(os.path.join('train/', f))
|
||||||
|
|
||||||
|
|
||||||
|
def addPerson(label, newFileName, oldFileName):
|
||||||
|
try:
|
||||||
|
# faceFilePath = '/faces/hesham-saeed2'
|
||||||
|
faceFile = 'faces/' + oldFileName
|
||||||
|
face_recognizer = cv2.createLBPHFaceRecognizer()
|
||||||
|
global new
|
||||||
|
if new:
|
||||||
|
print('new file. skipping load function')
|
||||||
|
else:
|
||||||
|
print('loading old file')
|
||||||
|
face_recognizer.load(faceFile)
|
||||||
|
except:
|
||||||
|
print('[ Error ] Problem in init/loading LBPHfacerecognizer')
|
||||||
|
sys.exit()
|
||||||
|
images = []
|
||||||
|
labels = []
|
||||||
|
image_names = os.listdir('train/')
|
||||||
|
for image_name in image_names:
|
||||||
|
if image_name.startswith('.'):
|
||||||
|
continue;
|
||||||
|
image_path = 'train/' + image_name
|
||||||
|
image = cv2.imread(image_path)
|
||||||
|
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
||||||
|
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
|
||||||
|
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
|
||||||
|
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))
|
||||||
|
else:
|
||||||
|
face_recognizer.update(images, np.array(labels))
|
||||||
|
face_recognizer.save('faces/' + newFileName)
|
||||||
|
print('Updated and saved file in faces/' + newFileName)
|
||||||
|
|
||||||
|
|
||||||
|
def capture(count):
|
||||||
|
try:
|
||||||
|
print('Initializing camera')
|
||||||
|
camera = PiCamera()
|
||||||
|
camera.resolution = (640, 480)
|
||||||
|
print('[ OK ] Camera')
|
||||||
|
sleep(2)
|
||||||
|
except:
|
||||||
|
print('[ Error ] Can not initialize PiCamera')
|
||||||
|
sys.exit()
|
||||||
|
for i in range(1, count + 1):
|
||||||
|
pic = 'train/' + str(i) + '.jpg'
|
||||||
|
camera.capture(pic)
|
||||||
|
photo = cv2.imread(pic, 1)
|
||||||
|
cv2.imshow('Photo', photo)
|
||||||
|
print('Captured ' + str(i) + '\nPath ' + pic)
|
||||||
|
cv2.waitKey(100)
|
||||||
|
sleep(1)
|
||||||
|
cv2.destroyAllWindows()
|
||||||
|
print('Done!')
|
||||||
|
|
||||||
|
|
||||||
|
def promptNew():
|
||||||
|
filemode = raw_input('Make new File? (y/n): ')
|
||||||
|
if filemode == 'y':
|
||||||
|
return True
|
||||||
|
elif filemode == 'n':
|
||||||
|
return False
|
||||||
|
else:
|
||||||
|
print('incorrect input')
|
||||||
|
promptNew()
|
||||||
|
|
||||||
|
|
||||||
|
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)
|
||||||
|
|
||||||
|
|
||||||
|
start()
|
BIN
untitled.pyc
BIN
untitled.pyc
Binary file not shown.
Loading…
Reference in New Issue
Block a user