def get_map(file_name, directory=None): """ Get a map as a list of tuples [(long0, lat0), (long1, lat1), ...]. Two possible file types for maps are txt and gpx. You will either need to specify the :gpx or :maps directory, or give a file extension for the file name. If no directory and no file extension is given, gpx is assumed """ if file_name.endswith('gpx'): file_type = "gpx" elif file_name.endswith(log_file_type): file_type = log_file_type else: raise ValueError("Invalid file extension: %s" % file_name) if directory is None: directory = ":maps" if file_type == "gpx": directory = project.interpret_dir(directory) file_name = project.get_file_name(file_name, directory, 'gpx') gps_map = _get_gpx_map(file_name, directory) else: directory = project.interpret_dir(directory) file_name = project.get_file_name(file_name, directory, log_file_type) gps_map = _get_txt_map(file_name, directory) print("Using map named", file_name) print("Length of map is", len(gps_map)) return gps_map
def __init__(self, file_name, directory): # Format the file name. Mac doesn't support colons in file names # If file format is provided but not a name, insert timestamp if file_name is None or \ file_name.replace("." + log_file_type, "") == "": file_name = filename_now() + "." + log_file_type elif len(file_name) < 4 or file_name[-4:] != "." + log_file_type: file_name += "." + log_file_type # Parse the input directory using the project module if directory == ":today": # for creating logs directory = project.interpret_dir(log_directory) + todays_log_folder() elif directory is None: directory = project.interpret_dir(log_directory) else: if directory[-1] != "/": directory += "/" if not os.path.isdir(directory): directory = project.interpret_dir(log_directory) + directory if not os.path.exists(directory): os.makedirs(directory) print("Writing to:", directory + file_name) self.time0 = time.time() self.log_start = self.time0 self.data_file = open(directory + file_name, 'w+') self.queue = []
def save_frame(self, frame=None, image_name=None, add_timestamp=True, directory=None): """ Save the current (or provided) frame as a png. By default it saves it to the images directory """ # you can add a timestamp to the image or make the timestamp the name if image_name is None: image_name = "" elif image_name is not None and add_timestamp: image_name += " " if add_timestamp: image_name += time.strftime("%c").replace(":", ";") if not image_name.endswith(".png"): image_name += ".png" print("Frame saved as " + str(image_name), end=" ") # select default directory if directory is None: directory = project.interpret_dir(":images") print("in directory:\n" + directory) if not os.path.isdir(directory): os.makedirs(directory) if directory[-1] != "/": directory += "/" # if no frame is provided, use the last frame if frame is None: frame = self.frame cv2.imwrite(directory + image_name, frame)
def __init__(self, file_name, directory=None): # pick a subdirectory of logs self.directory = project.parse_dir(directory, log_directory, lambda x: datetime.strptime(x, log_folder_format)) # for external use. Get the picked local directory self.local_dir = self.directory[self.directory.rfind("/", 0, -1) + 1:] # parse the file name. Get the full directory of the file self.file_name = project.get_file_name( file_name, self.directory, log_file_type) self.file_name_no_ext = self.file_name.split(".")[0] print("Using file named '%s'" % self.file_name) # read the whole file as a string with open(self.directory + self.file_name, 'r') as data_file: self.contents = data_file.read() # try to parse the name as a timestamp. If it succeeds, see if the # file is obsolete. Otherwise, do nothing try: if (datetime.strptime(self.directory.split("/")[-2], '%b %d %Y') <= datetime.strptime(obsolete_data, '%b %d %Y')): print("WARNING: You are using a data set that is obsolete " "with the current parser. Continue? (y/n)", end="") proceed = None while proceed != "" and proceed != "y" and proceed != "n": proceed = input(": ").lower() if proceed == "n": sys.exit(1) except ValueError: pass self.data = [] # the parsed data of the file self.iter_index = 0 # the character index of the file pickle_file_name = self.file_name[:-len(log_file_type)] + pickle_file_type log_pickle_dir = project.interpret_dir(pickle_directory) + self.local_dir if os.path.isfile(log_pickle_dir + pickle_file_name): print("Using pickled data") time0 = time.time() with open(log_pickle_dir + pickle_file_name, 'rb') as pickle_file: self.data = pickle.load(pickle_file) print("Took %s seconds" % (time.time() - time0)) else: # parse the file and write it to a pickle file print("Using raw file") time0 = time.time() self.create_data() print("Took %s seconds" % (time.time() - time0)) if not os.path.isdir(log_pickle_dir): os.mkdir(log_pickle_dir) with open(log_pickle_dir + pickle_file_name, 'wb') as pickle_file: pickle.dump(self.data, pickle_file, pickle.HIGHEST_PROTOCOL) print("Wrote pickle to: " + log_pickle_dir + pickle_file_name)
def start_recording(self, fps=32, video_name=None, add_timestamp=True, output_dir=None, width=None, height=None, with_frame=None): """ Initialize the Capture's video writer. :param fps: The playback FPS of the video. This number can be finicky as the capture's current FPS may not match the video's output FPS. This is because video playback takes less computation than analyzing the video in this setting. :param video_name: The name of the video. If "", a time stamp will automatically be inserted :param add_timestamp: An optional parameter specifying whether the time should be included. True by default :param output_dir: directory to put video in :param width: provide a width and force the video to that size :param width: provide a height and force the video to that size :param with_frame: A numpy array containing a frame of the stream. This frame will be used to define the size of the video :return: None """ video_format = 'avi' codec = 'MJPG' if video_name is None: video_name = "" elif video_name is not None and add_timestamp: video_name += " " if add_timestamp: video_name += time.strftime("%c").replace(":", ";") video_name += "." + video_format if output_dir is None: output_dir = project.interpret_dir(":videos") else: output_dir += "/" if not os.path.isdir(output_dir): os.makedirs(output_dir) output_dir += video_name fourcc = cv2.VideoWriter_fourcc(*codec) self.recording = cv2.VideoWriter() if width is None and with_frame is None: self.recorder_width = self.width else: if width is not None: self.recorder_width = width elif with_frame is not None: self.recorder_width = with_frame.shape[1] if height is None and with_frame is None: self.recorder_height = self.height else: if height is not None: self.recorder_height = height elif with_frame is not None: self.recorder_height = with_frame.shape[0] print(self.recorder_width, self.recorder_height) self.recording.open(output_dir, fourcc, fps, (self.recorder_width, self.recorder_height), True) self.recorder_output_dir = output_dir print("Initialized video named '%s'." % video_name) self.is_recording = True