-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_collection.py
More file actions
40 lines (33 loc) · 1.19 KB
/
data_collection.py
File metadata and controls
40 lines (33 loc) · 1.19 KB
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
import os
import cv2
# Directory to store collected images
DATA_DIR = './data'
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
number_of_classes = 3 # Change this based on the number of signs you want to collect
dataset_size = 100 # Number of images per class
cap = cv2.VideoCapture(0) # Change to 1 or 2 if the wrong camera is selected
for j in range(number_of_classes):
class_dir = os.path.join(DATA_DIR, str(j))
if not os.path.exists(class_dir):
os.makedirs(class_dir)
print('Collecting data for class {}'.format(j))
# Wait for user to be ready
while True:
ret, frame = cap.read()
cv2.putText(frame, 'Press "Q" to start collecting data', (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Start collecting images
counter = 0
while counter < dataset_size:
ret, frame = cap.read()
cv2.imshow('frame', frame)
cv2.imwrite(os.path.join(class_dir, f'{counter}.jpg'), frame)
counter += 1
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()