-
Notifications
You must be signed in to change notification settings - Fork 0
/
takeImage.py
68 lines (65 loc) · 2.34 KB
/
takeImage.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
import csv
import os, cv2
import numpy as np
import pandas as pd
import datetime
import time
# take Image of user
def TakeImage(l1, l2, haarcasecade_path, trainimage_path, message, err_screen,text_to_speech):
if (l1 == "") and (l2==""):
t='Please Enter the your Enrollment Number and Name.'
text_to_speech(t)
elif l1=='':
t='Please Enter the your Enrollment Number.'
text_to_speech(t)
elif l2 == "":
t='Please Enter the your Name.'
text_to_speech(t)
else:
try:
cam = cv2.VideoCapture(0)
detector = cv2.CascadeClassifier(haarcasecade_path)
Enrollment = l1
Name = l2
sampleNum = 0
directory = Enrollment + "_" + Name
path = os.path.join(trainimage_path, directory)
os.mkdir(path)
while True:
ret, img = 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), (255, 0, 0), 2)
sampleNum = sampleNum + 1
cv2.imwrite(
f"{path}\ "
+ Name
+ "_"
+ Enrollment
+ "_"
+ str(sampleNum)
+ ".jpg",
gray[y : y + h, x : x + w],
)
cv2.imshow("Frame", img)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
elif sampleNum > 50:
break
cam.release()
cv2.destroyAllWindows()
row = [Enrollment, Name]
with open(
"C:\\Users\\patel\\OneDrive\\Documents\\E\\FBAS\\StudentDetails\\studentdetails.csv",
"a+",
) as csvFile:
writer = csv.writer(csvFile, delimiter=",")
writer.writerow(row)
csvFile.close()
res = "Images Saved for ER No:" + Enrollment + " Name:" + Name
message.configure(text=res)
text_to_speech(res)
except FileExistsError as F:
F = "Student Data already exists"
text_to_speech(F)