def extract_data(video, filter_name, hdf): if os.path.exists(hdf): return print(' Creating %r...' % hdf) pg('extract_some', config={'file': video, 'filter': filter_name, 'hdf': hdf})
def run_pg_script(model, model_config, done_file): # if os.path.exists(done_file): # return pg(model, model_config) with open(done_file, 'w') as f: f.write('done :-)\n')
def join_video_29_fixed(output, dirname, pattern, fps): """ Note that the pattern is a Python regex: pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 """ from procgraph.core.registrar_other import register_model_spec # @UnresolvedImport from procgraph import pg # @UnresolvedImport register_model_spec(""" --- model join_video_helper_29 config output config dirname config pattern config images_per_second |files_from_dir dir=$dirname regexp=$pattern fps=$images_per_second| \ --> |imread_rgb| \ --> |fix_frame_rate fps=29.97| \ --> |mencoder quiet=1 file=$output timestamps=0| """) params = dict(dirname=dirname, pattern=pattern, output=output, images_per_second=fps) pg('join_video_helper_29', params)
def join_video(output, dirname, pattern, fps): """ Note that the pattern is a Python regex: pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 """ register_model_spec(""" --- model join_video_helper config output config dirname config pattern config fps |files_from_dir dir=$dirname regexp=$pattern| -->file #file --> |print| file --> |imread_rgb| --> rgb #file --> |imread| --> |torgb| --> rgb rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps| """) pg('join_video_helper', dict(dirname=dirname, pattern=pattern, output=output, fps=fps))
def join_video_29(output, dirname, pattern, fps): """ Note that the pattern is a Python regex: pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 """ register_model_spec(""" --- model join_video_helper_29 config output config dirname config pattern config factor |files_from_dir dir=$dirname regexp=$pattern| -->file file --> |imread_rgb| --> rgb rgb --> |rewrite_timestamps interval=$factor| -> retimed retimed --> |info| # XXX this doesn't work #retimed -> |fix_frame_rate fps=29.97| --> fill #fill --> |info| retimed -> |mencoder quiet=1 file=$output timestamps=0| """) factor = 1.0 / fps pg('join_video_helper_29', dict(dirname=dirname, pattern=pattern, output=output, factor=factor))
def main(): usage = 'Convert the old ER1 logs into the new format' parser = OptionParser(usage=usage) parser.add_option("--out", help="Destination base directory") (options, args) = parser.parse_args() if options.out is None: raise Exception('Please supply out') for logdir in dirs: logname = os.path.basename(logdir) outdir = os.path.join(options.out, logname) done_file = os.path.join(outdir, 'finished.txt') if os.path.exists(done_file): print('Skipping %r' % logdir) continue try: pg('er1convert', dict(logdir=logdir, outdir=outdir)) with open(done_file, 'w') as f: f.write('done :-)\n') except Exception as e: print e continue
def join_video(output, dirname, pattern, fps): """ Note that the pattern is a Python regex: pg-video-join -d anim-simple/ -p '.*.png' -o anim-simple-collate.mp4 --fps 1 """ register_model_spec(""" --- model join_video_helper config output config dirname config pattern config fps |files_from_dir dir=$dirname regexp=$pattern| -->file #file --> |print| file --> |imread_rgb| --> rgb #file --> |imread| --> |torgb| --> rgb rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps| """) pg('join_video_helper', dict(dirname=dirname, pattern=pattern, output=output, fps=fps))
def create_video_servo_both(bag, out): if os.path.exists(out): return out pg('video_servo_all', config=dict(bag=bag, out=out)) return out
def main(): usage = '' parser = OptionParser(usage=usage) parser.add_option("--base", default='.', help="Base directory") parser.add_option("--out", help="Destination base directory") (options, args) = parser.parse_args() for logdir in dirs: logname = os.path.basename(logdir) outdir = os.path.join(options.out, logname) done_file = os.path.join(outdir, 'finished.txt') if os.path.exists(done_file): print('Skipping %r' % logdir) continue try: pg('er1convert', dict(logdir=logdir, outdir=outdir)) with open(done_file, 'w') as f: f.write('done :-)\n') except Exception as e: print e continue
def join_video_29_fixed(output, dirname, pattern, fps): """ Note that the pattern is a Python regex: pg-video-join -d anim-simple/ -p '*.png' -o anim-simple-collate.mp4 --fps 1 """ from procgraph.core.registrar_other import register_model_spec from procgraph import pg register_model_spec(""" --- model join_video_helper_29 config output config dirname config pattern config images_per_second |files_from_dir dir=$dirname regexp=$pattern fps=$images_per_second| \ --> |imread_rgb| \ --> |fix_frame_rate fps=29.97| \ --> |mencoder quiet=1 file=$output timestamps=0| """) params = dict(dirname=dirname, pattern=pattern, output=output, images_per_second=fps) pg('join_video_helper_29', params)
def create_video(data_central, id_robot, id_agent, model='boot_log2movie', model_params={}, # {'plotter.zoom': 2}, suffix='', id_episode=''): logger.info('Creating video for robot: %r agent: %r episode %r' % (id_robot, id_agent, id_episode)) ds = data_central.get_dir_structure() basename = ds.get_video_basename(id_robot=id_robot, id_agent=id_agent, id_episode=id_episode) if suffix: basename += '-%s' % suffix logdir = ds.get_simulated_logs_dir() # TODO: check has_procgraph import procgraph_vehicles # @UnusedImport from procgraph import pg # logger.info('Writing to %r.' % basename) config = dict(logdir=logdir, id_robot=id_robot, id_agent=id_agent, id_episode=id_episode, output=basename, **model_params) pg(model, config=config, stats=False) ds.file_is_done(basename, desc="")
def create_video_servo_multi(id_explog, out_base): explog = get_conftools_explogs().instance(id_explog) bag = explog.get_bagfile() if os.path.exists(out_base + '.fcpxml'): print('Already exists: %s' % out_base) return pg('video_servo_multi', config=dict(bag=bag, out_base=out_base))
def video_visualize_diffeo_stream1(id_stream, out): """ Creates a video for a stream. """ from procgraph import pg config = dict(out=out, stream=id_stream) pg('visualize_diffeo_stream1', config=config, additional_modules=['procgraph_diffeo']) return out
def create_video_bootdata(bag, id_robot, out_base, md): # # if os.path.exists(out_base + '.fcpxml'): # FIXME # print('Already exists: %s' % out_base) # return md['pg_model'] = 'video_bootdata' md['id_robot'] = id_robot config = dict(bag=bag, out_base=out_base, id_robot=id_robot, md=md) pg('video_bootdata', config=config)
def join_four(video1, video2, video3, video4, out): print('video1: %s' % video1) print('video2: %s' % video2) print('video3: %s' % video3) print('video4: %s' % video4) if not os.path.exists(out): config = dict(video1=video1, video2=video2, video3=video3, video4=video4, out=out) pg('join_four', config=config) return out
def create_visualization(log, pg_model): out = log + '-%s.avi' % pg_model if os.path.exists(out): logger.info('Skipping because %r already exists.' % out) return from procgraph import pg import procgraph_vehicles #@UnusedImport import fly_behaviors.visualization #@UnusedImport config = {'file': log, 'output': out} pg(pg_model, config=config)
def extract_data(video, filter_name, hdf): if os.path.exists(hdf): return print(' Creating %r...' % hdf) pg('extract_some', config={ 'file': video, 'filter': filter_name, 'hdf': hdf })
def d8n_make_video_from_bag(bag_filename, topic, out): """ Creates a video out of the topic in the bag. topic: the topic name (any image-like topic) out: an .mp4 file. Note that needs a bunch more dependencies to be installed. Until we fix the dependencies: sudo pip install SystemCmd==1.2 ros_node_utils==1.0 ConfTools==1.8 QuickApp==1.2.2 sudo apt-get install -y mplayer mencoder sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install -y ffmpeg #gstreamer0.10-ffmpeg """ try: import procgraph_ros # @UnusedImport from procgraph import pg except ImportError: raise # pg -m procgraph_ros bag2mp4 --bag $bag --topic $topic --out $out model = 'bag2mp4_fixfps' # model = 'bag2mp4' tmpdir = create_tmpdir() out_tmp = os.path.join(tmpdir, os.path.basename(out)) logger.debug('Writing temp file to %s' % out_tmp) logger.debug('(You can use mplayer to follow along.)') pg(model, config=dict(bag=bag_filename, topic=topic, out=out_tmp)) md = out_tmp + '.metadata.yaml' if os.path.exists(md): os.unlink(md) dn = os.path.dirname(out) if not os.path.exists(dn): os.makedirs(dn) shutil.copyfile(out_tmp, out) logger.info('Created: %s' % out) info = out_tmp + '.info.yaml' if os.path.exists(info): os.unlink(info)
def video_crop(filename, output=None): if output is None: base, ext = os.path.splitext(filename) output = '%s-crop%s' % (base, ext) mask = get_motion_mask(filename) #print mask H, W = mask.shape useless_line = [np.min(mask[i, :]) for i in range(H)] useless_col = [np.min(mask[:, i]) for i in range(W)] useful_line = np.logical_not(useless_line) useful_col = np.logical_not(useless_col) #print useful_line #print useful_col crop_top = np.min(np.nonzero(useful_line)[0]) crop_bottom = H - np.max(np.nonzero(useful_line)[0]) crop_left = np.min(np.nonzero(useful_col)[0]) crop_right = W - np.max(np.nonzero(useful_col)[0]) print('Cropping left %s right %s top %s bottom %s' % (crop_left, crop_right, crop_top, crop_bottom)) register_model_spec(""" --- model crop_movie config video 'video' config output config top = 0 config left = 0 config right = 0 config bottom = 0 |mplayer file=$video stats=1| --> rgb rgb --> |crop left=$left top=$top bottom=$bottom right=$right| --> rgb2 rgb2 --> |mencoder quiet=1 file=$output timestamps=0| """) pg( 'crop_movie', dict(top=crop_top, bottom=crop_bottom, left=crop_left, right=crop_right, video=filename, output=output))
def get_motion_mask(filename): model = pg('movie_stats', dict(video=filename)) rgb_min = model.get_output('rgb_min') rgb_max = model.get_output('rgb_max') no_motion = np.min(rgb_min == rgb_max, axis=2) def percentage(x): return (100.0 * np.sum(x.flat) / x.size) print('Immobile: %.1f%% of the image.' % percentage(no_motion)) bgcolor = rgb_min[0, 0, :] print('bgcolor: %s' % bgcolor) whiter = rgb_min[:, :, 0] == bgcolor[0] whiteg = rgb_min[:, :, 1] == bgcolor[1] whiteb = rgb_min[:, :, 2] == bgcolor[2] white = np.logical_and(np.logical_and(whiter, whiteg), whiteb) print('White: %.1f%% of the image.' % percentage(white)) mask = np.logical_and(white, no_motion) print('Both: %.1f%% of the image.' % percentage(mask)) return mask
def convert_rawseeds2hdf(logdir, hdf): if os.path.exists(hdf): print("Already done: %r exists." % hdf) return if not os.path.exists(logdir): raise Exception('Logdir %r does not exist.' % logdir) tmp = hdf + '.part' if os.path.exists(tmp): print('Deleting previous temp file %r.' % tmp) os.unlink(tmp) pg('rawseeds2hdf', {'logdir': logdir, 'file': tmp}) print("Renaming temporary file %r to %r." % (tmp, hdf)) os.rename(tmp, hdf)
def video_crop(filename, output=None): if output is None: base, ext = os.path.splitext(filename) output = '%s-crop%s' % (base, ext) mask = get_motion_mask(filename) #print mask H, W = mask.shape useless_line = [np.min(mask[i, :]) for i in range(H)] useless_col = [np.min(mask[:, i]) for i in range(W)] useful_line = np.logical_not(useless_line) useful_col = np.logical_not(useless_col) #print useful_line #print useful_col crop_top = np.min(np.nonzero(useful_line)[0]) crop_bottom = H - np.max(np.nonzero(useful_line)[0]) crop_left = np.min(np.nonzero(useful_col)[0]) crop_right = W - np.max(np.nonzero(useful_col)[0]) print('Cropping left %s right %s top %s bottom %s' % (crop_left, crop_right, crop_top, crop_bottom)) register_model_spec(""" --- model crop_movie config video 'video' config output config top = 0 config left = 0 config right = 0 config bottom = 0 |mplayer file=$video stats=1| --> rgb rgb --> |crop left=$left top=$top bottom=$bottom right=$right| --> rgb2 rgb2 --> |mencoder quiet=1 file=$output timestamps=0| """) pg('crop_movie', dict(top=crop_top, bottom=crop_bottom, left=crop_left, right=crop_right, video=filename, output=output))
def join_video(output, dirname, pattern, fps): register_model_spec(""" --- model join_video_helper config output config dirname config pattern config fps |files_from_dir dir=$dirname regexp=$pattern| -->file #file --> |print| file --> |imread_rgb| --> rgb #file --> |imread| --> |torgb| --> rgb rgb --> |mencoder quiet=1 file=$output timestamps=0 fps=$fps| """) pg('join_video_helper', dict(dirname=dirname, pattern=pattern, output=output, fps=fps))
def convert_hdf2bpi(model, hdf, bpi, params): ''' Model: any procgraph model taking as config "hdf" and "file". ''' if os.path.exists(bpi): print("Already done: %r exists." % bpi) return if not os.path.exists(hdf): raise Exception('File %r does not exist.' % hdf) tmp = bpi + '.part' if os.path.exists(tmp): print('Deleting previous temp file %r.' % tmp) os.unlink(tmp) # Update the config. config = dict(**params) config.update({'hdf': hdf, 'file': tmp}) print("using %r" % config) # Actually run the model. pg(model, config) print("Renaming temporary file %r to %r." % (tmp, bpi)) os.rename(tmp, bpi)
def get_motion_mask(filename): register_model_spec(""" --- model movie_stats config video 'video' output rgb_min output rgb_max |mplayer file=$video stats=1| --> rgb rgb --> |minimum_over_time| --> |output name=rgb_min| rgb --> |maximum_over_time| --> |output name=rgb_max| """) model = pg('movie_stats', dict(video=filename)) rgb_min = model.get_output('rgb_min') rgb_max = model.get_output('rgb_max') no_motion = np.min(rgb_min == rgb_max, axis=2) def percentage(x): return (100.0 * np.sum(x.flat) / x.size) print('Immobile: %.1f%% of the image.' % percentage(no_motion)) bgcolor = rgb_min[0, 0, :] print('bgcolor: %s' % bgcolor) whiter = rgb_min[:, :, 0] == bgcolor[0] whiteg = rgb_min[:, :, 1] == bgcolor[1] whiteb = rgb_min[:, :, 2] == bgcolor[2] white = np.logical_and(np.logical_and(whiter, whiteg), whiteb) print('White: %.1f%% of the image.' % percentage(white)) mask = np.logical_and(white, no_motion) print('Both: %.1f%% of the image.' % percentage(mask)) return mask
def join_two(video1, video2, out): if not os.path.exists(out): pg('join_two', config=dict(video1=video1, video2=video2, out=out)) return out
def aer_tracker_plot(tracks, width): config = dict(log=tracks, width=width) pg("aer_track_plot", config)
def aer_particles_plot(tracks, width): log = find_log_for_tracks(tracks) config = dict(log=log, tracks=tracks, width=width) pg('aer_particles_plot', config, stats=True)
def create_video_cam(bag, topic, out, md={}): if not os.path.exists(out): import procgraph_ros # @UnusedImport pg('video_cam', config=dict(bag=bag, topic=topic, out=out, md=md)) return out
def d8n_make_video_from_bag( bag_filename: str, topic: str, out: str, t0: Optional[float] = None, t1: Optional[float] = None, ): """ Creates a video out of the topic in the bag. topic: the topic name (any image-like topic) out: an .mp4 file. Returns the name of the created file. raises NotEnoughFramesInSlice if there are less than 3 frames in slice Note that needs a bunch more dependencies to be installed. Until we fix the dependencies: sudo pip install SystemCmd==1.2 ros_node_utils==1.0 ConfTools==1.8 QuickApp==1.2.2 sudo apt-get install -y mplayer mencoder sudo add-apt-repository ppa:mc3man/trusty-media sudo apt-get update sudo apt-get install -y ffmpeg """ try: import procgraph_ros from procgraph import pg except ImportError: raise # pg -m procgraph_ros bag2mp4 --bag $bag --topic $topic --out $out stop_at = 10 min_messages = 5 actual_count, count, stopped_early = count_messages_in_slice( bag_filename, topic, t0, t1, stop_at=stop_at) msg = "Creating video for topic %r, which has %d messages " "in the entire log." % ( topic, count, ) logger.info(msg) if not stopped_early and (actual_count != count): msg = "However, the actual count in [%s, %s] is %s" % (t0, t1, actual_count) logger.info(msg) if actual_count < min_messages: msg = ("Topic %r has only %d messages in slice (%d total), too few" " to make a video (min: %s).\nFile: %s" % (topic, actual_count, count, min_messages, bag_filename)) msg += "\nt0: %s" % t0 msg += "\nt1: %s" % t1 bag = rosbag.Bag(bag_filename) msg += "\nstart: %s" % bag.get_start_time() msg += "\nend: %s" % bag.get_end_time() if actual_count == count: msg += "\n" + dtu.indent(get_summary_of_bag_messages(bag), " info: ") bag.close() raise NotEnoughFramesInSlice(msg) model = "bag2mp4_fixfps_limit" tmpdir = dtu.create_tmpdir() out_tmp = os.path.join(tmpdir, os.path.basename(out)) try: logger.debug("Writing temp file to %s" % out_tmp) logger.debug("(You can use mplayer to follow along.)") pg(model, config=dict(bag=bag_filename, topic=topic, out=out_tmp, t0=t0, t1=t1)) md = out_tmp + ".metadata.yaml" if os.path.exists(md): os.unlink(md) dn = os.path.dirname(out) if not os.path.exists(dn): dtu.mkdirs_thread_safe(dn) shutil.copyfile(out_tmp, out) logger.info("Created: %s" % out) info = out_tmp + ".info.yaml" if os.path.exists(info): os.unlink(info) return out finally: if os.path.exists(out_tmp): os.unlink(out_tmp) if os.path.exists(tmpdir): shutil.rmtree(tmpdir)
def aer_resolver_plot(tracks, width): config = dict(log=tracks, width=width) pg('aer_resolver_plot', config)
def pg_video_bg_depth(video, background, perc, every, out): if os.path.exists(out): return out pg('video_bg_depth', config=dict(video=video, bg=background, perc=perc, every=every, out=out)) return out
def create_video_laser(bag, topic, out, md={}): if not os.path.exists(out): pg('video_hokuyo', config=dict(bag=bag, topic=topic, out=out, md=md)) return out
def create_video_servo_error(bag, out): if os.path.exists(out): return out out_base = os.path.splitext(out)[0] pg('video_servo_error', config=dict(bag=bag, out_base=out_base)) return out
def average(video, out): if not os.path.exists(out): pg('video_average', config=dict(video=video, out=out)) return out
def aer_video_meat(log, dt): import procgraph_aer # @UnusedImport config = dict(filename=log, interval=dt) pg('aer_events_show', config)