-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPhotoCapture.py
66 lines (57 loc) · 1.71 KB
/
PhotoCapture.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
import imutils
import time
# pip install opencv-python
import cv2
import csv
import os
# using haar cascade to detect face
cascade = "haarcascade_frontalface_default.xml"
detector = cv2.CascadeClassifier(cascade)
Name = str(input("Enter your Name : "))
Roll_Number = int(input("Enter your Roll_Number : "))
dataset = "dataset"
sub_data = Name
path = os.path.join(dataset, sub_data)
if not os.path.isdir(path):
os.makedirs(path)
print(sub_data)
# write name and roll number into csv file
info = [str(Name), str(Roll_Number)]
with open("student.csv", "a") as csvFile:
write = csv.writer(csvFile)
write.writerow(info)
csvFile.close()
print("Starting video stream...")
# open web camera to capture imagess
cam = cv2.VideoCapture(0, cv2.CAP_DSHOW)
time.sleep(2.0)
total = 0
print("Press c to capture the Image q to quit.")
# capture a maximum of 150 images
while True: # total < 150:
print(total)
_, frame = cam.read()
# scale the image to 400x400 size
img = imutils.resize(frame, width=400)
# detect the face area rectangle
rects = detector.detectMultiScale(
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY),
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30),
)
# show the images in the screen
cv2.imshow("Frame", frame)
# check if the key pressed is q
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
if key == ord("c"):
for x, y, w, h in rects:
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
p = os.path.sep.join([path, "{}.png".format(str(total).zfill(5))])
# capture the images as png
cv2.imwrite(p, img)
# total += 1
cam.release()
cv2.destroyAllWindows()