-
Notifications
You must be signed in to change notification settings - Fork 0
/
grab_images.py
134 lines (107 loc) · 3.64 KB
/
grab_images.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# Description: Simple script to capture images to create our hand gesture dataset.
#
# References:
# - https://www.geeksforgeeks.org/extract-video-frames-from-webcam-and-save-to-images-using-python/
# -------------------------------------------------------------------------------
# nativ imports
import os
import sys
import time
import datetime
# third party imports
import cv2
def menu():
"""
Displays a menu with options for the user.
"""
opts = {
0: "Accelerate to the left",
1: "Don't accelerate",
2: "Accelerate to the right",
3: "Exit",
}
for key, value in opts.items():
print(f"{key} -- {value}")
def capture_img_set(option, no_imgs_to_capture, out_dir):
"""
Capture a set of images using the webcam.
Parameters:
- option (int): the chosen menu option
- no_imgs_to_capture (int): number of images to be captured
- out_dir (str): output directory for saving captured images
"""
now = datetime.datetime.now()
# Format it as a string
timestamp = now.strftime("%Y%m%d_%H%M%S")
acronyms = {
0: "acc_l", # Accelerate to the left
1: "acc_n", # Don't accelerate
2: "acc_r", # Accelerate to the right
}
# Opens the built camera of laptop to capture video.
cap = cv2.VideoCapture(0)
for i in range(no_imgs_to_capture):
ret, frame = cap.read()
# delay between frames
time.sleep(0.3)
# This condition prevents from infinite looping in case video ends.
if not ret:
break
# Save frame by frame into disk using imwrite method
path = os.path.join(out_dir, f"{acronyms[option]}_{i}_{option}_{timestamp}.jpg")
print(path)
cv2.imwrite(path, frame)
filename = os.path.basename(path)
print(f"{filename} ... captured.")
print("\n\nCapturing images...done.")
print(f'{"-" :->82}')
cap.release()
cv2.destroyAllWindows()
def check_dirs(directory):
"""
Capture a set of images using the webcam.
Parameters:
- option (int): the chosen menu option
- no_imgs_to_capture (int): number of images to be captured
- out_dir (str): output directory for saving captured images
"""
print(f"type({directory}): {type(directory)}")
dir_check = os.path.isdir(directory)
if not dir_check:
os.makedirs(directory)
print(f"dataset directory created: {directory}")
else:
print(f"{directory} already exists.")
def main():
while True:
menu()
option = ""
try:
option = int(input("Select option from menu: "))
except ValueError:
print("Wrong option, select valid option.")
if option == 3:
sys.exit()
else:
no_imgs_to_capture = int(
input("How many images do you want to capture? \n")
)
print(f'{"-":->82}')
if option == 0:
data_dir = "try_dataset/"
check_dirs(data_dir)
time.sleep(3)
capture_img_set(option, no_imgs_to_capture, os.path.join(data_dir, "acc_l"))
if option == 1:
data_dir = "try_dataset/"
check_dirs(data_dir)
time.sleep(3)
capture_img_set(option, no_imgs_to_capture, os.path.join(data_dir, "acc_n"))
if option == 2:
data_dir = "try_dataset/"
check_dirs(data_dir)
time.sleep(3)
capture_img_set(option, no_imgs_to_capture, os.path.join(data_dir, "acc_r"))
print("IMAGE CAPTURE...DONE.")
if __name__ == "__main__":
main()