Skip to content

Commit 205e3a6

Browse files
authored
Add files via upload
1 parent 3369c91 commit 205e3a6

11 files changed

+204727
-0
lines changed

database_connect.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import sqlite3 as sql
2+
import cv2
3+
import numpy as np
4+
import sys
5+
import time
6+
from PIL import Image
7+
8+
conn = sql.connect('test.db')
9+
c = conn.cursor()
10+
11+
id = int(raw_input('Enter id: '))
12+
name = raw_input('Enter Name: ')
13+
age = int(raw_input('Enter age: '))
14+
15+
#c.execute('CREATE TABLE Facedetails (id int PRIMARY KEY,name text,age int) ')
16+
c.execute("INSERT INTO Facedetails(id,name,age) VALUES(?,?,?)",(id,name,age))
17+
'''
18+
find = raw_input('Enter id to find : ')
19+
c = c.execute("SELECT * FROM Facedetails WHERE id = " + str(find))
20+
for row in c:
21+
print 'Id = ',row[0]
22+
print 'name = ',row[1]
23+
print 'age = ',row[2]
24+
'''
25+
conn.commit()
26+
conn.close()
27+
28+
print "Database Upated Successfully"

face_recog.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
import cv2
3+
import numpy as np
4+
import sys
5+
6+
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
7+
cam = cv2.VideoCapture(0)
8+
ret,img = cam.read()
9+
10+
name = raw_input('Enter user name for dataset')
11+
img_number = 0
12+
while True:
13+
# Capture frame-by-frame
14+
ret, img = cam.read()
15+
16+
t1=time.time()
17+
18+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19+
20+
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30, 30),flags=cv2.cv.CV_HAAR_SCALE_IMAGE
21+
)
22+
23+
# Draw a rectangle around the faces
24+
for (x, y, w, h) in faces:
25+
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
26+
27+
# Display the resulting frame
28+
cv2.imshow('Output', img)
29+
30+
print(time.time()-t1)
31+
32+
if(img_number > 20):
33+
break
34+
if cv2.waitKey(33) & 0xFF == 27:
35+
break
36+
cam.release()
37+
cv2.destroyAllWindows()

face_recog_dataset_generator.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import time
2+
import cv2
3+
import numpy as np
4+
import sys
5+
6+
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
7+
cam = cv2.VideoCapture(0)
8+
ret,img = cam.read()
9+
10+
id = raw_input('Enter your id')
11+
img_number = 0
12+
while True:
13+
# Capture frame-by-frame
14+
ret, img = cam.read()
15+
16+
t1=time.time()
17+
18+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
19+
20+
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=5,minSize=(30, 30),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
21+
22+
# Draw a rectangle around the faces
23+
for (x, y, w, h) in faces:
24+
img_number = img_number + 1
25+
cv2.imwrite("dataset/{0}.{1}.jpg".format(str(id), str(img_number)), gray[y:y + h, x:x + w])
26+
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
27+
28+
# Display the resulting frame
29+
cv2.imshow('Output', img)
30+
31+
print(time.time()-t1)
32+
33+
if(img_number > 200):
34+
break
35+
if cv2.waitKey(33) & 0xFF == 27:
36+
break
37+
cam.release()
38+
cv2.destroyAllWindows()

face_recog_detector.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import time
2+
import cv2
3+
import sqlite3 as sql
4+
import numpy as np
5+
import sys
6+
import os
7+
8+
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')
9+
cam = cv2.VideoCapture(0)
10+
rec = cv2.createLBPHFaceRecognizer()
11+
rec.load('trainingDataset.yml')
12+
id = 0
13+
font = cv2.FONT_HERSHEY_SIMPLEX
14+
15+
conn = sql.connect('test.db')
16+
c = conn.cursor()
17+
18+
while True:
19+
ret, img = cam.read()
20+
t1=time.time()
21+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
22+
faces = faceCascade.detectMultiScale(gray,scaleFactor=1.3,minNeighbors=5,minSize=(30, 30),flags=cv2.cv.CV_HAAR_SCALE_IMAGE)
23+
for (x, y, w, h) in faces:
24+
cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)
25+
id,conf = rec.predict(gray[y:y+h,x:x+w])
26+
c = c.execute("SELECT * FROM Facedetails WHERE id = " + str(id))
27+
for row in c:
28+
cv2.putText(img, 'Id : '+str(row[0]), (x, y + h + 10), font, 0.5, (200, 255, 255), 1)
29+
cv2.putText(img, 'Name : '+str(row[1]), (x, y + h + 25), font, 0.5, (200, 255, 255), 1)
30+
cv2.putText(img, 'Age : '+str(row[2]), (x, y + h + 37), font, 0.5, (200, 255, 255), 1)
31+
cv2.putText(img, 'conf : '+ str(conf), (x, y + h + 50), font, 0.5, (200, 255, 255), 1)
32+
cv2.imshow('Output', img)
33+
print(time.time()-t1)
34+
if cv2.waitKey(33) & 0xFF == 27:
35+
break
36+
cam.release()
37+
cv2.destroyAllWindows()

face_recog_trainer.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import cv2
3+
import numpy as np
4+
from PIL import Image
5+
6+
recognizer = cv2.createLBPHFaceRecognizer()
7+
path = 'dataset'
8+
strvar = 'dataset\\'
9+
10+
11+
def getimgid():
12+
global path
13+
imagePaths = [os.path.join(path, f) for f in os.listdir(path)]
14+
15+
faceSamples = []
16+
ids = []
17+
for imagePath in imagePaths:
18+
pilImage = Image.open(imagePath)
19+
imageNp = np.array(pilImage, 'uint8')
20+
id = int(imagePath.strip(strvar).strip('.jpg').split('.')[0])
21+
faceSamples.append(imageNp)
22+
ids.append(id)
23+
cv2.imshow('training', imageNp)
24+
cv2.waitKey(10)
25+
return np.array(ids), faceSamples
26+
27+
ids, faces = getimgid()
28+
recognizer.train(faces, ids)
29+
recognizer.save('trainingDataset.yml')
30+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)