-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_student.py
69 lines (56 loc) · 3.77 KB
/
add_student.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import cv2 # openCV
import numpy as np # for numpy arrays
import sqlite3
import dlib
import os # for creating folders
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 512)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 512)
#cap.set(6, cv2.VideoWriter.fourcc('M', 'J', 'P', 'G'))
detector = dlib.get_frontal_face_detector()
def insertOrUpdate(Id, Name, roll) : # this function is for database
connect = sqlite3.connect("Face-DataBase") # connecting to the database
cmd = "SELECT * FROM Students WHERE ID = " + Id # selecting the row of an id into consideration
cursor = connect.execute(cmd)
isRecordExist = False
for row in cursor: # checking wheather the id exist or not
isRecordExist = True
# print(isRecordExist)
if isRecordExist == True:
print("record is updated") # updating name and roll no
connect.execute("UPDATE Students SET Name = ? WHERE ID = ?",(Name, Id))
connect.execute("UPDATE Students SET Roll = ? WHERE ID = ?",(roll, Id))
else:
print("record is entered")
params = (Id, Name, roll) # insering a new student data
connect.execute("INSERT INTO Students(ID, Name, Roll) VALUES(?, ?, ?)", params)
connect.commit() # commiting into the database
connect.close() # closing the connection
name = input("Enter student's name : ")
roll = input("Enter student's Roll Number : ")
Id = roll[-2:]
insertOrUpdate(Id, name, roll) # calling the sqlite3 database
folderName = "user" + Id # creating the person or user folder
folderPath = os.path.join(os.path.dirname(os.path.realpath(__file__)), "dataset/"+folderName)
if not os.path.exists(folderPath):
os.makedirs(folderPath)
sampleNum = 0
while(True):
ret, img = cap.read() # reading the camera input
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # Converting to GrayScale
dets = detector(img, 1)
for i, d in enumerate(dets): # loop will run for each face detected
sampleNum += 1
cv2.imwrite(folderPath + "/User." + Id + "." + str(sampleNum) + ".jpg",
img[d.top():d.bottom(), d.left():d.right()],
[int(cv2.IMWRITE_JPEG_QUALITY), 1000000]) # Saving the faces
size = img.shape
print(size)
cv2.rectangle(img, (d.left(), d.top()) ,(d.right(), d.bottom()),(0,255,0) ,2) # Forming the rectangle
cv2.waitKey(200) # waiting time of 200 milisecond
cv2.imshow('frame', img) # showing the video input from camera on window
cv2.waitKey(1)
if(sampleNum >= 20): # will take 20 faces
break
cap.release() # turning the webcam off
cv2.destroyAllWindows() # Closing all the opened windows