-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdatacollect.py
More file actions
80 lines (61 loc) · 2.58 KB
/
datacollect.py
File metadata and controls
80 lines (61 loc) · 2.58 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
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
74
75
76
77
78
79
80
import cv2
import os
# Define the directory to store collected data
output_directory = 'data/'
# Create the output directory if it doesn't exist
if not os.path.exists(output_directory):
os.makedirs(output_directory)
# Initialize the webcam
cap = cv2.VideoCapture(0)
# Set the resolution of the captured video
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
# Define the labels for different signs and their corresponding keys
labels_keys = {
'A': ord('a'),
'B': ord('b'),
'C': ord('c'),
'L': ord('l')
# Add more labels and keys as needed
}
# Initialize variables
current_label = None
current_gesture_count = 0
max_gesture_count = 100 # Number of samples to collect for each label
while True:
ret, frame = cap.read()
if not ret:
break
# Define the hand frame rectangle (x, y, width, height)
hand_rect = (100, 100, 200, 200) # Adjust as needed to fit the hand region
# Draw the rectangle on the frame
cv2.rectangle(frame, (hand_rect[0], hand_rect[1]), (hand_rect[0] + hand_rect[2], hand_rect[1] + hand_rect[3]), (0, 255, 0), 2)
# Display the current frame
cv2.imshow('Collecting Data', frame)
# Check for key press
key = cv2.waitKey(1)
# If 'q' is pressed, quit the program
if key == ord('q'):
break
# If a valid key is pressed, set the current label
if key in labels_keys.values():
current_label = [label for label, keycode in labels_keys.items() if keycode == key][0]
# If the current label is set and the maximum number of samples hasn't been reached
if current_label is not None and current_gesture_count < max_gesture_count:
# Save the current frame as an image
image_filename = f'{current_label}_{current_gesture_count}.jpg'
image_path = os.path.join(output_directory, image_filename)
# Extract the region of interest (ROI) where the hand is supposed to be
roi = frame[hand_rect[1]:hand_rect[1]+hand_rect[3], hand_rect[0]:hand_rect[0]+hand_rect[2]]
# Save the ROI as an image
cv2.imwrite(image_path, roi)
print(f'Saved: {image_path}')
# Increment the gesture count for the current label
current_gesture_count += 1
# If the maximum number of samples has been reached, reset variables
if current_gesture_count >= max_gesture_count:
current_label = None
current_gesture_count = 0
# Release the webcam and close the OpenCV windows
cap.release()
cv2.destroyAllWindows()