-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare_dataset
70 lines (54 loc) · 1.94 KB
/
prepare_dataset
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
import os
import cv2
# Define the new data directory path
DATA_DIR = r"C:\Users\user\Downloads\ASL_dataset"
# Create the main data directory if it doesn't exist
if not os.path.exists(DATA_DIR):
os.makedirs(DATA_DIR)
number_of_classes = 36 # For A-Z
dataset_size = 100 # Number of samples per class
cap = cv2.VideoCapture(0)
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(f"Collecting data for class {j}. Press 's' to start capturing...")
# Wait until user presses 's' to start capturing
while True:
ret, frame = cap.read()
if not ret:
print("Failed to access the camera. Exiting...")
cap.release()
cv2.destroyAllWindows()
exit()
cv2.putText(frame, f"Press 's' to start class {j} samples", (50, 50),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Frame", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord('s'): # Start when 's' is pressed
break
elif key == ord('q'): # Quit program
cap.release()
cv2.destroyAllWindows()
exit()
# Start collecting dataset_size number of images
counter = 0
while counter < dataset_size:
ret, frame = cap.read()
if not ret:
print("Failed to capture frame. Exiting...")
cap.release()
cv2.destroyAllWindows()
exit()
cv2.imshow("Frame", frame)
cv2.imwrite(os.path.join(class_dir, f"{counter}.jpg"), frame)
counter += 1
key = cv2.waitKey(1) & 0xFF
if key == ord('q'): # Quit if 'q' is pressed
cap.release()
cv2.destroyAllWindows()
exit()
print(f"Class {j} data collection complete!")
print("All 26 classes collected. Process finished!")
cap.release()
cv2.destroyAllWindows()