-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatacreation.py
93 lines (89 loc) · 2.42 KB
/
datacreation.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
from imutils.video import VideoStream
import argparse
import imutils
import time
import cv2
import os
import shutil
import numpy as np
from pymongo import MongoClient
from bson.objectid import ObjectId
import gridfs
db = MongoClient().Attendance #connects to localhost by default
dic={}
count=1
fid=[]
fs1 = gridfs.GridFS(db)
name = ''
def capture(count):
# Load the Haar cascades
detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
print("[INFO] starting video stream...")
vs = VideoStream(src=0).start()
time.sleep(1.0)
total=0
owd=os.getcwd()
os.chdir('Images')
os.makedirs(str(count).zfill(3))
os.chdir(owd)
name = input("enter your name")
dic[count]=name
print("images are being captured please wait...")
while True:
frame = vs.read()
frame = imutils.resize(frame, width=400)
img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
rects = detector.detectMultiScale(img, scaleFactor=1.1, minNeighbors=4, minSize=(30, 30))
for (x, y, w, h) in rects:
if len(rects)==0:
pass
else:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
p = os.path.sep.join(['Images',str(count).zfill(3),"{}.png".format(str(total).zfill(1))])
cv2.imwrite(p,img[y:y+h,x:x+w])
fid.append(fs1.put(open(p,'rb').read(),filename=name+str(list(dic.keys())[list(dic.values()).index(name)])))
total += 1
cv2.imshow("Frame", frame)
cv2.waitKey(1)
if total==30:
break
cv2.destroyAllWindows()
vs.stop()
print("images successfully captured and stored")
def getimg():
# retrieve what was just stored.
total=1
owd=os.getcwd()
os.chdir('OutputImages')
for i in range(len(fid)):
fptr = fs1.get(fid[i])
dat=fs1.find({"_id":fid[i]})
for i in dat:
label=''.join(filter(lambda x: not x.isdigit(), i.filename))
lid=''.join(filter(lambda x: x.isdigit(), i.filename))
outputdata = fptr.read()
try:
os.mkdir(label)
except:
pass
p1 = label+"/"+str(label)+"-"+str(lid)+"-"+str(total)+".png"
output= open(p1,"wb")
output.write(outputdata)
output.close()
total+=1
os.chdir(owd)
print('images retrieved')
def maiin():
n= int(input("How many persons you want to train"))
if n != 0:
res = True
else:
res= False
count=1
if res is True:
while count<=n:
capture(count)
count+=1
getimg()
return res
#To retrive the images in database and store them in localsystem for training