-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathCapture_Image.py
75 lines (58 loc) · 2.02 KB
/
Capture_Image.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
import csv
import cv2
import os
# counting the numbers
def is_number(s):
try:
float(s)
return True
except ValueError:
pass
try:
import unicodedata
unicodedata.numeric(s)
return True
except (TypeError, ValueError):
pass
return False
# Take image function
def takeImages():
Id = input("Enter Your Id: ")
name = input("Enter Your Name: ")
if(is_number(Id) and name.isalpha()):
cam = cv2.VideoCapture(0)
harcascadePath = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(harcascadePath)
sampleNum = 0
while(True):
ret, img = cam.read()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5, minSize=(30,30),flags = cv2.CASCADE_SCALE_IMAGE)
for(x,y,w,h) in faces:
cv2.rectangle(img, (x, y), (x+w, y+h), (10, 159, 255), 2)
#incrementing sample number
sampleNum = sampleNum+1
#saving the captured face in the dataset folder TrainingImage
cv2.imwrite("TrainingImage" + os.sep +name + "."+Id + '.' +
str(sampleNum) + ".jpg", gray[y:y+h, x:x+w])
#display the frame
cv2.imshow('frame', img)
#wait for 100 miliseconds
if cv2.waitKey(100) & 0xFF == ord('q'):
break
# break if the sample number is more than 100
elif sampleNum > 100:
break
cam.release()
cv2.destroyAllWindows()
res = "Images Saved for ID : " + Id + " Name : " + name
row = [Id, name]
with open("StudentDetails"+os.sep+"StudentDetails.csv", 'a+') as csvFile:
writer = csv.writer(csvFile)
writer.writerow(row)
csvFile.close()
else:
if(is_number(Id)):
print("Enter Alphabetical Name")
if(name.isalpha()):
print("Enter Numeric ID")