def __init__(self): """ initializes the thread without starting to capture the data """ super().__init__() self.active = True self.driver = Driver.instance() self.camera = Camera.instance() # define directories and file paths date_str = datetime.today().strftime("%Y-%m-%d-%H-%M-%S") self.log_dir = f"{const.Storage.DATA}/{date_str}" self.img_dir = f"{self.log_dir}/img/" self.log_path = f"{self.log_dir}/log.csv" self.img_extension = "npy" # ensure that the necessary directories exist os.mkdir(self.log_dir) os.mkdir(self.img_dir) assert os.path.isdir(self.log_dir), "data directory could not be created" assert os.path.isdir(self.img_dir), "image directory could not be created"
def follow_road(self): """ drives autonomously without explicit instructions by propagating the camera input through a convolutional neural network that predicts a steering angle NOTE this mode has no particular destination and therfore does not terminate unless the driver is stopped explicitly """ steering_net = SteeringNet() steering_net.load(const.Storage.DEFAULT_STEERING_MODEL) self.start_driving() with Camera.instance() as camera: while self.recorder_thread is not None: image = camera.image predicted_angle = steering_net.predict(image, self.angle) self.angle = predicted_angle time.sleep(0.25) self.stop_driving()
def run(self): """ starts capturing the data (image, angle, previous angle) Raises: OSError: in case the log file cannot be created OSError: in case the log file cannot be opened after being created """ with Camera.instance() as camera: try: # create log file and write headers with open(self.log_path, "w+") as log: writer = csv.writer(log) writer.writerow(["image", "angle", "previous_angle"]) except OSError: raise OSError("The log file could not be created.") previous_angle = 0.0 while self.active: if camera.image is None: continue # skip loop if no image provided # save image img_filename = datetime.today().strftime("%H-%M-%S-%f") + "." + self.img_extension np.save(self.img_dir + img_filename, camera.image) try: # write data to csv file with open(self.log_path, "a") as log: writer = csv.writer(log) angle = str(round(self.driver.angle, 3)) previous_angle = str(previous_angle) writer.writerow([img_filename, angle, previous_angle]) except OSError: raise OSError("The log file could not be opened.") previous_angle = angle # update previous angle for next loop time.sleep(self.CAPTURE_INTERVAL)