def post_train_detector_job(dataset_name, run_name, epochs, import_datasets=""): dataset_name = quote(dataset_name) run_name = quote(run_name) rc = RunConfig(dataset_name, run_name) dc = DatasetConfig(dataset_name) if rc.exists and dc.exists: cmd = [ python_path, "training_script.py", "--name={}".format(dataset_name), "--experiment={}".format(run_name), "--input_shape={}".format(rc.get('detector_resolution')), "--train_data_dir=fjlfbwjefrlbwelrfb_man_we_need_a_better_detector_codebase", "--batch_size={}".format(rc.get('detection_training_batch_size')), "--image_shape={}".format(dc.get('video_resolution')), "--epochs={}".format(epochs) ] if import_datasets: import_datasets = quote(import_datasets) cmd.append("--import_datasets={}".format(import_datasets)) job_id = jm.run(cmd, "train_detector") if job_id: return (job_id, 202) else: return (NoContent, 503) else: return (NoContent, 404)
def post_detect_objects_job(dataset_name, run_name): dataset_name = quote(dataset_name) run_name = quote(run_name) rc = RunConfig(dataset_name, run_name) if rc.exists: cmd = [python_path, "detect_csv.py", "--dataset={}".format(dataset_name), "--run={}".format(run_name), "--res={}".format(rc.get("detector_resolution")), "--conf={}".format(rc.get("confidence_threshold")), "--bs={}".format(rc.get("detection_batch_size"))] job_id = jm.run(cmd, "detect_objects") if job_id: return (job_id, 202) else: return (NoContent, 503) else: return (NoContent, 404)
def post_visualize_detections_job(dataset_name, run_name, confidence_threshold, coords): dataset_name = quote(dataset_name) run_name = quote(run_name) rc = RunConfig(dataset_name, run_name) dc = DatasetConfig(dataset_name) if rc.exists and dc.exists: cmd = [python_path, "visualize_detections.py", "--cmd=findvids", "--dataset={}".format(dataset_name), "--run={}".format(run_name), "--res={}".format(rc.get("detector_resolution")), "--conf={}".format(confidence_threshold), "--fps={}".format(dc.get('video_fps')), "--coords={}".format(coords)] job_id = jm.run(cmd, "visualize_detections") if job_id: return (job_id, 202) else: return (NoContent, 503) else: return (NoContent, 404)
def post_detections_to_world_coordinates_job(dataset_name, run_name, make_videos): dataset_name = quote(dataset_name) run_name = quote(run_name) rc = RunConfig(dataset_name, run_name) dc = DatasetConfig(dataset_name) if rc.exists and dc.exists: cmd = [ python_path, "detections_world.py", "--cmd=findvids", "--dataset={}".format(dataset_name), "--run={}".format(run_name), "--make_videos={}".format(make_videos), "--ssdres={}".format(rc.get("detector_resolution")), "--vidres={}".format(dc.get('video_resolution')), "--kltres={}".format(dc.get('point_track_resolution')) ] job_id = jm.run(cmd, "detections_to_world") if job_id: return (job_id, 202) else: return (NoContent, 503) else: return (NoContent, 404)
def main(dataset, run, n_clips, clip_length): dc = DatasetConfig(dataset) rc = RunConfig(dataset, run) mask = Masker(dataset) classes = get_classnames(dataset) num_classes = len(classes) + 1 calib = Calibration(dataset) dataset_path = "{dsp}{ds}/".format(dsp=datasets_path, ds=dataset) run_path = "{rp}{ds}_{r}/".format(rp=runs_path, ds=dataset, r=run) # Grab a bunch of videos vids_query = "{dsp}videos/*.mkv".format(dsp=dataset_path) all_vids = glob(vids_query) all_vids = [right_remove(x.split('/')[-1], '.mkv') for x in all_vids] all_vids.sort() vids = [] if n_clips > len(all_vids): n_clips = len(all_vids) if n_clips == len(all_vids): vids = all_vids else: while len(vids) < n_clips: vid = choice(all_vids) if not vid in vids: vids.append(vid) print_flush(vids) # Find out what has been run on all of these videos, what to include include_klt = True include_pixeldets = True include_worlddets = True include_worldtracks = True klts = [] pixeldets = [] worlddets = [] worldtracks = [] # Point tracks need to be converted for faster access vidres = dc.get('video_resolution') kltres = dc.get('point_track_resolution') class KLTConfig(object): klt_x_factor = 0 klt_y_factor = 0 klt_config = KLTConfig() klt_config.klt_x_factor = vidres[0] / kltres[0] klt_config.klt_y_factor = vidres[1] / kltres[1] ssdres = rc.get('detector_resolution') x_scale = vidres[0] / ssdres[0] y_scale = vidres[1] / ssdres[1] colors = class_colors(num_classes) for vid in vids: f = get_klt_path(dataset_path, vid) if not isfile(f): include_klt = False else: klt = load(f) klt, klt_frames = convert_klt(klt, klt_config) pts = (klt, klt_frames, class_colors(n_cols_klts)) klts.append(pts) f = get_pixeldet_path(run_path, vid) if not isfile(f): include_pixeldets = False else: dets = pd.read_csv(f) pixeldets.append((dets, colors, x_scale, y_scale)) f = get_worlddet_path(run_path, vid) if not isfile(f): include_worlddets = False else: dets = pd.read_csv(f) worlddets.append((dets, colors, calib)) f = get_worldtracks_path(run_path, vid) if not isfile(f): include_worldtracks = False else: tracks = load(f) worldtracks.append((tracks, class_colors(n_cols_tracks), calib)) print_flush("Point tracks: {}".format(include_klt)) print_flush("Pixel coordinate detections: {}".format(include_pixeldets)) print_flush("World coordinate detections: {}".format(include_worlddets)) print_flush("World coordinate tracks: {}".format(include_worldtracks)) # Decide where to start and stop in the videos clip_length = clip_length * dc.get( 'video_fps') # convert from seconds to frames print_flush("Clip length in frames: {}".format(clip_length)) clips = [] for vid in vids: start, stop = make_clip(vid, clip_length, dataset_path) clips.append((start, stop)) incs = [ include_klt, include_pixeldets, include_worlddets, include_worldtracks ] funs = [klt_frame, pixeldet_frame, worlddet_frame, worldtracks_frame] dats = [klts, pixeldets, worlddets, worldtracks] nams = [ "Point tracks", "Detections in pixel coordinates", "Detections in world coordinates", "Tracks in world coordinates" ] print_flush(clips) with iio.get_writer("{trp}summary.mp4".format(trp=run_path), fps=dc.get('video_fps')) as outvid: for i_vid, vid in enumerate(vids): print_flush(vid) old_prog = 0 with iio.get_reader("{dsp}videos/{v}.mkv".format(dsp=dataset_path, v=vid)) as invid: start, stop = clips[i_vid] for i_frame in range(start, stop): frame = invid.get_data(i_frame) pieces = [] for inc, fun, dat, nam in zip(incs, funs, dats, nams): if inc: piece = fun(dat[i_vid], mask.mask(frame.copy(), alpha=0.5), i_frame) draw_text(piece, vid, i_frame, nam) pieces.append(piece) outvid.append_data(join(pieces)) prog = float(i_frame - start) / (stop - start) if prog - old_prog > 0.1: print_flush("{}%".format(round(prog * 100))) old_prog = prog print_flush("Done!")