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 load_video(self, video_name, directory): """Load a video file from a directory into an opencv capture object""" directory = project.parse_dir(directory, ":videos") video_name = project.get_file_name(video_name, directory, ['avi', 'mov', 'mp4', 'AVI', 'MOV', 'MP4']) print("loading video into window named '" + str( video_name) + "'...") capture = cv2.VideoCapture(directory + video_name) cv2.namedWindow(video_name) # set the properties of the video fps = capture.get(cv2.CAP_PROP_FPS) num_frames = int(capture.get(cv2.CAP_PROP_FRAME_COUNT)) if num_frames <= 0: raise Exception("Video failed to load! " "Did you misspell the video name?") length_sec = num_frames / fps length_msec = int(length_sec * 1000) print("\tfps:", fps) print("\tlength (sec):", length_sec) print("\tlength (frames):", num_frames) # initialize the track bar and the number of ticks it has slider_ticks = int(capture.get(cv2.CAP_PROP_FRAME_WIDTH) / 3) if slider_ticks > num_frames: slider_ticks = num_frames track_bar_name = "frame:" cv2.createTrackbar(track_bar_name, video_name, 0, slider_ticks, self.on_slider) print("video loaded!") return video_name, capture, length_msec, num_frames, slider_ticks, track_bar_name