-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdataset_creator.py
139 lines (105 loc) · 3.53 KB
/
dataset_creator.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Jan 14 12:49:56 2019
@author: nilesh
"""
#importing libraries
import cv2,os,pickle
import face_recognition as fr
#creating blank lists
known_face_encodings_list=[]
known_names=[]
ids=[]
font=cv2.FONT_HERSHEY_SIMPLEX
#creating image's directory
try:
cwd=os.getcwd()
print(cwd)
os.mkdir(cwd+"/dataset_images")
except:
print()
#funtions for Capturing image
def image_taker(dir_name,student_id):
cam=cv2.VideoCapture(0)
counter=0
flag=0
while cam.isOpened():
frame=cam.read()[1]
#converting BGR frame to RGB frame
rgb_frame=frame[:,:,::-1]
#getting locations of faces present
faces=fr.face_locations(rgb_frame)
for (top,right,bottom,left) in faces:
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
cv2.putText(frame, "Press 'C' to capture image", (0 , 35), font, 1.0, (255, 255, 255), 2)
cv2.imshow("live",frame)
#handler
if cv2.waitKey(100) & 0xFF==ord('q'):
if flag==0:
#remove if image is not created to avoid any issue
os.rmdir(dir_name)
break
if cv2.waitKey(100) & 0xFF==ord('c'):
#saving imaegs
cv2.imwrite(dir_name+"/image,"+str(student_id)+","+str(counter)+".jpg",frame)
flag=1
print("captured")
cv2.destroyAllWindows()
cam.release()
cv2.destroyAllWindows()
#setting up student Id
choice='y'
#getting last student id and creating next id
try:
with open("ids.txt",'rb') as file_data:
labels=pickle.load(file_data)
student_id=max(labels)+1
except FileNotFoundError:
student_id=0
while(choice=='y'):
print(student_id)
student_name=input("Enter student name: ")
#defining directory name where images will be stored
cwd=os.getcwd()+"/dataset_images/"
dir_name=cwd+student_name+","+str(student_id)
#using try to avoid error when directory is already present
try:
os.mkdir(dir_name)
print("check")
image_taker(dir_name,student_id)
print("check")
except:
print("Student name already exits.")
choice=input("Add another:")
if choice=='y':
student_id=student_id+1
#getting face encoding from stored images
dataset_dir_name=os.getcwd()+"/dataset_images"
folder_names=os.listdir(dataset_dir_name)
for i in folder_names:
dir_name=dataset_dir_name+"/"+i
face_names=os.listdir(dir_name)
for face_name in face_names:
image_name=dir_name+"/"+face_name
print(image_name)
#loading images using face_recognition library
known_face= fr.load_image_file(image_name)
print(known_face)
#getting encodings of faces
known_face_encoding=fr.face_encodings(known_face)[0]
student_name=i.split(",")[0]
student_id_in=int(i.split(",")[1])
#appending encodings,ids, names into lists
known_face_encodings_list.append(known_face_encoding)
known_names.append(student_name)
ids.append(student_id_in)
print(known_names)
print(ids)
#storing data in files using pickle
with open("encodings.txt",'wb') as file_data:
pickle.dump(known_face_encodings_list,file_data)
with open("name.txt",'wb') as file_data:
pickle.dump(known_names,file_data)
with open("ids.txt",'wb') as file_data:
pickle.dump(ids,file_data)