-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCollector.py
48 lines (43 loc) · 1.45 KB
/
Collector.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
# -*- coding: utf-8 -*-
"""
Created on Thu May 2 19:19:15 2019
@author: Omkar Shidore
https://www.github.com/OmkarShidore
"""
import cv2
import os
def createFolder(directory):
try:
if not os.path.exists(directory):
os.makedirs(directory)
except OSError:
print ('Error: Creating directory. ' + directory)
#created folder for saving dataset images of user
createFolder('./dataSet/')
cam = cv2.VideoCapture(0)
detector=cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
Id=input('Enter your id: ')
UserName=input('Enter Name: ')
sampleNum=0
while True:
ret, img = cam.read()
ret, img1 = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),2)
#incrementing sample number
sampleNum=sampleNum+1
sampleNum1=str(sampleNum)
text=sampleNum
#saving the captured face in the dataset folder
cv2.imwrite("dataSet/User."+Id +'.'+ UserName+'.'+str(sampleNum)+'.'+ ".jpg", gray[y:y+h,x:x+w])
cv2.putText(img1,sampleNum1, (70, 150), cv2.FONT_HERSHEY_SIMPLEX, 3,(255,255,255), 3)
cv2.imshow('Scans',img1)
if cv2.waitKey(1)==27:
break
# break if the sample number is morethan 20
elif sampleNum>100:
break
cam.release()
cv2.destroyAllWindows()