Skip to content

Instantly share code, notes, and snippets.

@gb103
Last active January 6, 2021 10:57
Show Gist options
  • Save gb103/cb82725a6f96536505d9e295bcdcc56d to your computer and use it in GitHub Desktop.
Save gb103/cb82725a6f96536505d9e295bcdcc56d to your computer and use it in GitHub Desktop.
To capture the face images
import cv2
import numpy as np
import sqlite3
#faceDetect=cv2.CascadeClassifier('haarcascade_frontalface_default.xml');
faceDetect = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
cam=cv2.VideoCapture(0);
def insertOrUpdate(Id,Name,Age,Gen,CR):
conn=sqlite3.connect("FaceBase.db")
cmd="SELECT * FROM People WHERE ID="+str(Id)
cursor=conn.execute(cmd)
isRecordExist=0
for row in cursor:
isRecordExist=1
if(isRecordExist==1):
cmd="UPDATE People SET Name="+str(Name)+"WHERE ID="+str(Id)
cmd2="UPDATE People SET Age="+str(Age)+"WHERE ID="+str(Id)
cmd3="UPDATE People SET Gender="+str(Gen)+"WHERE ID="+str(Id)
cmd4="UPDATE People SET CR="+str(CR)+"WHERE ID="+str(Id)
conn.execute(cmd)
else:
params = (Id,Name,Age,Gen,CR)
cmd="INSERT INTO People(ID,Name,Age,Gender,CR) Values(?, ?, ?, ?, ?)"
cmd2=""
cmd3=""
cmd4=""
conn.execute(cmd, params)
conn.execute(cmd2)
conn.execute(cmd3)
conn.execute(cmd4)
conn.commit()
conn.close()
Id=input('Enter User Id')
name=input('Enter User Name')
age=input('Enter User Age')
gen=input('Enter User Gender')
cr=input('Enter User Criminal Records')
insertOrUpdate(Id,name,age,gen,cr)
sampleNum=0
while(True):
ret,img=cam.read();
gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
faces=faceDetect.detectMultiScale(gray,1.3,5);
for(x,y,w,h) in faces:
sampleNum=sampleNum+1;
cv2.imwrite("dataSet/User."+str(Id)+"."+str(sampleNum)+".jpg",gray[y:y+h,x:x+w])
cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv2.waitKey(100);
cv2.imshow("Face",img);
cv2.waitKey(1);
if(sampleNum>50):
break;
cam.release()
cv2.destroyAllWindows()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment