def get_data(cap, file, t0, out, name): dt = 1 / cap.get(cv2.CAP_PROP_FPS) cur_time = 0 prev_vel = 0 prev_altitude = 0 prev_time = 0 start = False dropped_frames = 0 frame_index = 1 prev_stage = None time_file = open(name + '.meta', 'w') with open(CONFIG_FILE_PATH, 'r') as spacex_dict_file: spacex_dict = json.load(spacex_dict_file) session = general_extract.RelativeExtract( spacex_dict, anchor_range=spacex_dict['anchor'][0]) util = Util(session) if util.find_anchor(cap, start=ANCHOR_SEARCH_START_TIME_FRACTION, end=ANCHOR_SEARCH_END_TIME_FRACTION): util.skip_from_launch(cap, 'sign', t0) _, frame = cap.read() _, t0 = session.extract_number(frame, 'time') _, v0 = session.extract_number(frame, 'velocity') dec, a0 = session.extract_number(frame, 'altitude') if dec: a0 /= DECIMAL_CONVERSION if t0 is not None: prev_time = t0 - dt prev_vel = v0 prev_altitude = a0 cur_time = rtnd(t0, 3) while frame is not None: _, velocity = session.extract_number(frame, 'velocity') dec, altitude = session.extract_number(frame, 'altitude') if dec and altitude is not None: altitude /= DECIMAL_CONVERSION show_frame(frame) if cv2.waitKey(1) & 0xff == ord('q'): break cur_stage = session.get_template_index(frame, 'stage') + 1 if velocity is not None and altitude is not None and \ (check_data(prev_vel, prev_time, velocity/KMH, cur_time, prev_altitude, altitude) or check_stage_switch(cur_stage, prev_stage)): velocity /= KMH if cur_stage is not None and cur_stage != prev_stage: prev_stage = cur_stage time_file.write( json.dumps( OrderedDict([('time', rtnd(cur_time, PRECISION)), ('stage', cur_stage)])) + '\n') time_file.flush() json_data = data_to_json(cur_time, velocity, altitude) if out: print(data_to_json(cur_time, velocity, altitude)) if start: write_to_file(file, json_data) prev_vel = velocity prev_altitude = altitude prev_time = cur_time if velocity > LAUNCH_VELOCITY: start = True else: dropped_frames += 1 if dropped_frames % DROPPED_FRAME_THRESH == 0: print( 'Frame number {} was dropped ({}) which is {:.2f}% of the total frames' .format(frame_index, dropped_frames, 100 * dropped_frames / frame_index)) _, frame = cap.read() if start: cur_time += dt frame_index += 1 cv2.destroyAllWindows() time_file.close()
def data_to_json(time, velocity, altitude): return json.dumps( OrderedDict([('time', rtnd(time, PRECISION)), ('velocity', rtnd(velocity, PRECISION)), ('altitude', altitude)]))