Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
OmkarShidore authored May 7, 2019
1 parent 7922c03 commit a1ba02f
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 0 deletions.
48 changes: 48 additions & 0 deletions Collector.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# -*- coding: utf-8 -*-
"""
Created on Thu May 2 19:19:15 2019
@author: Omkar Shidore
https://www.github.com/OmkarShidore
"""
import cv2
import os
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
#created folder for saving dataset images of user
createFolder('./dataSet/')

cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Id=input('Enter your id: ')
UserName=input('Enter Name: ')
sampleNum=0
while True:
ret, img = cam.read()
ret, img1 = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)

#incrementing sample number
sampleNum=sampleNum+1
sampleNum1=str(sampleNum)
text=sampleNum
#saving the captured face in the dataset folder
cv2.imwrite("dataSet/User."+Id +'.'+ UserName+'.'+str(sampleNum)+'.'+ ".jpg", gray[y:y+h,x:x+w])

cv2.putText(img1,sampleNum1, (70, 150), cv2.FONT_HERSHEY_SIMPLEX, 3,(255,255,255), 3)
cv2.imshow('Scans',img1)

if cv2.waitKey(1)==27:
break
# break if the sample number is morethan 20
elif sampleNum>100:
break
cam.release()
cv2.destroyAllWindows()
93 changes: 93 additions & 0 deletions modelTrainer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# -*- coding: utf-8 -*-
"""
Created on Thu May 2 20:19:45 2019
@author: Omkar Shidore
https://www.github.com/OmkarShidore
"""

import os
import cv2
import numpy as np
from PIL import Image
print('Module Versions: ')
print('OpenCv: '+cv2.__version__, '\nNumpy: '+np.__version__,'\nPillow>>Image: '+Image.__version__)

#Creatung directories
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
createFolder('./trainner/')

recognizer = cv2.face.LBPHFaceRecognizer_create()
detector= cv2.CascadeClassifier("haarcascade_frontalface_default.xml");

def getImagesAndLabels(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
#empty lists for storing Lables and features
faceSamples=[]
Ids=[]
for imagePath in imagePaths:

pilImage=Image.open(imagePath).convert('L')
imageNp=np.array(pilImage,'uint8')
Id=int(os.path.split(imagePath)[-1].split(".")[1])
faces=detector.detectMultiScale(imageNp)
for (x,y,w,h) in faces:
faceSamples.append(imageNp[y:y+h,x:x+w])
Ids.append(Id)
return faceSamples,Ids
faces,Ids = getImagesAndLabels('dataSet')
#training model on dataSet
recognizer.train(faces, np.array(Ids))
#trainer.yml stored in the folder trainner
recognizer.save('trainner/trainner.yml')

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);


'''
def keyValuePairs(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
Ids=[]
UserNames=[]
for imagePath in imagePaths:
Id=int(os.path.split(imagePath)[-1].split(".")[1])
UserName=str(os.path.split(imagePath)[-1].split(".")[2])
Ids.append(Id)
UserNames.append(UserName)
keyValues = dict(zip(Ids, UserNames))
return Ids,UserNames,keyValues
Ids,UserNames,keyValues=keyValuePairs('dataSet')
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.3,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf>50):
if Id in keyValues.keys():
Id1='User Id: '+str(Id)
Name='Name: '+keyValues[Id]
else:
Id="Null"
Name="Unknown"
else:
Id="Unknown"
cv2.putText(im,Id1,(x+w,y),font,0.7,(0,255,0), 2)
cv2.putText(im,Name,(x+w,y+123),font,0.7,(0,255,0), 2)
cv2.imshow('im',im)
if cv2.waitKey(1)==27:
break
cam.release()
cv2.destroyAllWindows()
'''
86 changes: 86 additions & 0 deletions recognition.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# -*- coding: utf-8 -*-
"""
Created on Fri May 3 00:37:50 2019
@author: Omkar Shidore
https://www.github.com/OmkarShidore
"""
import os
import cv2

recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX

def keyValuePairs(path):
imagePaths=[os.path.join(path,f) for f in os.listdir(path)]
Ids=[]
UserNames=[]
for imagePath in imagePaths:
Id=int(os.path.split(imagePath)[-1].split(".")[1])
UserName=str(os.path.split(imagePath)[-1].split(".")[2])
Ids.append(Id)
UserNames.append(UserName)
keyValues = dict(zip(Ids, UserNames))
return Ids,UserNames,keyValues
Ids,UserNames,keyValues=keyValuePairs('dataSet')

cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.3,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,255,0),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf>50):
if Id in keyValues.keys():
Id1= 'User Id : '+str(Id)
Name='User Name: '+keyValues[Id]
else:
Id="Null"
Name="Unknown"
else:
Id="Unknown"
cv2.putText(im,Id1,(x,y+h+20),font,0.7,(0,255,0), 2)
cv2.putText(im,Name,(x,y+h+38),font,0.7,(0,255,0), 2)
cv2.imshow('im',im)
if cv2.waitKey(1)==27:
break
cam.release()
cv2.destroyAllWindows()

#(124,185,234)
'''
import cv2
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainner/trainner.yml')
cascadePath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascadePath);
cam = cv2.VideoCapture(0)
font = cv2.FONT_HERSHEY_SIMPLEX
while True:
ret, im =cam.read()
gray=cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
faces=faceCascade.detectMultiScale(gray, 1.3,5)
for(x,y,w,h) in faces:
cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),2)
Id, conf = recognizer.predict(gray[y:y+h,x:x+w])
if(conf>50):
if(Id==1):
Id="Anirban"
elif(Id==2):
Id="Sam"
else:
Id="Unknown"
cv2.putText(im,Id,(x,y+h),font,2,(0,255,0), 2)
cv2.imshow('im',im)
if cv2.waitKey(1)==27:
break
cam.release()
cv2.destroyAllWindows()
'''

0 comments on commit a1ba02f

Please sign in to comment.