forked from adarshmalapaka/ground-plane-segmentation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_collection.py
146 lines (107 loc) · 3.25 KB
/
data_collection.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
135
136
137
138
139
140
141
142
143
144
145
146
# data_collection.py
"""
Brief: Teleoperates a robot around and records a HD video for a duration of 5 minutes to be used as
a datset for training a sematic segmentation Deep Learning model.
Course: ENPM673 - Perception for Autonomous Robotics [Project-04]
University of Maryland, College Park (MD)
Date: 25th April, 2022
"""
import os
import time
import RPi.GPIO as gpio
from datetime import datetime
from picamera import PiCamera
from picamera.array import PiRGBArray
gpio.setwarnings(False)
gpio.setmode(gpio.BOARD)
# Setting up the motor pins on the Motor Driver
gpio.setup(31, gpio.OUT) # IN1
gpio.setup(33, gpio.OUT) # IN2
gpio.setup(35, gpio.OUT) # IN3
gpio.setup(37, gpio.OUT) # IN4
def stopRobot():
# Set all pins low
gpio.output(31, False)
gpio.output(33, False)
gpio.output(35, False)
gpio.output(37, False)
def moveForward(tf):
# For Left wheels
gpio.output(31, True)
gpio.output(33, False)
# For Right wheels
gpio.output(35, False)
gpio.output(37, True)
# Move forward for 'tf' seconds
time.sleep(tf)
# Send all pins low
stopRobot()
def moveReverse(tf):
# For Left wheels
gpio.output(31, False)
gpio.output(33, True)
# For Right wheels
gpio.output(35, True)
gpio.output(37, False)
# Move backward for 'tf' seconds
time.sleep(tf)
# Send all pins low
stopRobot()
def rotateLeft(tf):
# For Left wheels
gpio.output(31, False)
gpio.output(33, True)
# For Right wheels
gpio.output(35, False)
gpio.output(37, True)
# Rotate left for 'tf/4' seconds
time.sleep(tf/4.0)
# Send all pins low
stopRobot()
def rotateRight(tf):
# For Left wheels
gpio.output(31, True)
gpio.output(33, False)
# For Right wheels
gpio.output(35, True)
gpio.output(37, False)
# Rotate right for 'tf/4' seconds
time.sleep(tf/4.0)
# Send all pins low
stopRobot()
def keyInput(event):
print("Key: ", event)
key_press = event
tf = 1 # move bot 1 second
if key_press.lower() == 'w':
moveForward(tf)
elif key_press.lower() == 'z':
moveReverse(tf)
elif key_press.lower() == 'a':
rotateLeft(tf)
elif key_press.lower() == 's':
rotateRight(tf)
else:
print("Invalid key pressed!!")
camera = PiCamera() # Initialize the PiCamera instance
rawCapture = PiRGBArray(camera)
camera.resolution = (1280, 720)
camera.framerate = 30
camera.rotation = 180 # Camera feed is rotated by 180 deg
camera.start_preview()
file_name = datetime.now().strftime('%Y%m%d%H%M%S')
camera.start_recording("/home/pi/Videos/" + file_name + ".h264")
start_time = time.time()
while True:
key_press = input("Select the driving mode: ")
# Stop the bot and recording after 300 seconds (5 minutes) or by pressing 'p'
if key_press == 'p' or time.time() - start_time >= 300:
stopRobot()
gpio.cleanup() # Clean up the motor GPIO pins
break
keyInput(key_press)
camera.stop_recording()
camera.stop_preview()
# Convert the .h264 video file to .mp4
command = 'MP4Box -add /home/pi/Videos/' + file_name + '.h264 -fps 30 /home/pi/Videos/' + file_name + '.mp4'
os.system(command)