def annotation_image_list(dataset_name, annotation_set): ds_path = get_annotation_path(dataset_name, annotation_set) vids = glob(ds_path + '*') if vids: vids = [x.split('/')[-1] for x in vids if isdir(x)] vids.sort() out = [] for vid in vids: ims_path = "{ds_path}{vid}/*.jpg".format(ds_path=ds_path, vid=vid) ims = glob(ims_path) ims.sort(key=lambda x: int(right_remove(x.split('/')[-1], '.jpg'))) for im in ims: imnum = right_remove(im.split('/')[-1], '.jpg') txt = im.replace('.jpg', '.txt') # Depending on the 'annotated' variable, the images show up in different colors in the web UI annotated = "not_annotated" if isfile(txt): annotated = "already_annotated" else: auto = im.replace('.jpg','.auto') if isfile(auto): annotated = "automatically_annotated" out.append( (vid, imnum, annotated) ) return out else: return None
def post_import_videos_job(dataset_name, path, method, logs_path=None, minutes=0): dataset_name = quote(dataset_name) if logs_path is None: logs_path = path # Since 'path' probably contains a query, like ending with '*.mkv', this should be removed if not (logs_path[-1] == '/'): logs_path = right_remove(logs_path, logs_path.split('/')[-1]) dc = DatasetConfig(dataset_name) if dc.exists: resolution = dc.get('video_resolution') fps = dc.get('video_fps') cmd = [python_path, "import_videos.py", "--query={}".format(path), "--dataset={}".format(dataset_name), "--resolution={}".format(resolution), "--method={}".format(method), "--fps={}".format(fps), "--logs={}".format(logs_path), "--minutes={}".format(minutes)] job_id = jm.run(cmd, "import_videos") if job_id: return (job_id, 202) else: return (NoContent, 503) else: return (NoContent, 404)
def main(cmd, dataset, run, conf, make_videos): if make_videos: from visualize_tracking import render_video from config import DatasetConfig from apply_mask import Masker mask = Masker(dataset) dc = DatasetConfig(dataset) config_path = "{rp}{ds}_{rn}/world_tracking_optimization.pklz".format(rp=runs_path, ds=dataset, rn=run) if isfile(config_path): config = load(config_path) else: #raise(ValueError("No world tracking optimized configuration exists at {}".format(config_path))) config = WorldTrackingConfig(default_config) calib = Calibration(dataset) munkres = Munkres() ts = Timestamps(dataset) start_stop = None if cmd == "findvids": from glob import glob vidnames = glob('{dsp}{ds}/videos/*.mkv'.format(dsp=datasets_path, ds=dataset)) vidnames = [right_remove(x.split('/')[-1], '.mkv') for x in vidnames] vidnames.sort() outfolder = '{}{}_{}/tracks_world/'.format(runs_path, dataset, run) mkdir(outfolder) else: vidnames = [cmd] outfolder = './' start_stop = (0,500) for v in vidnames: print_flush(v) out_path = "{of}{v}_tracks.pklz".format(of=outfolder, v=v) print_flush("Loading data...") det_path = "{rp}{ds}_{rn}/detections_world/{v}_world.csv".format(rp=runs_path, ds=dataset, rn=run, v=v) detections3D = pd.read_csv(det_path) klt_path = det_path.replace('.csv', '_klt.pklz') klts = load(klt_path) print_flush("Tracking...") tracks = make_tracks(dataset, v, detections3D, klts, munkres, ts, calib, config, start_stop=start_stop) print_flush("Saving tracks...") save(tracks, out_path) if make_videos: vidpath = "{dsp}{ds}/videos/{v}.mkv".format(dsp=datasets_path, ds=dataset, v=v) print_flush("Rendering video...") render_video(tracks, vidpath, out_path.replace('.pklz','.mp4'), calib=calib, mask=mask, fps=dc.get('video_fps')) print_flush("Done!")
def main(cmd, res, dataset, run, conf, fps, coords): res = parse_resolution(res) classnames = get_classnames(dataset) local_output = False csvs = [] if cmd == "findvids": if coords == "pixels": query = "{rp}{ds}_{r}/csv/*.csv".format(rp=runs_path, ds=dataset, r=run) elif coords == "world": query = "{rp}{ds}_{r}/detections_world/*.csv".format(rp=runs_path, ds=dataset, r=run) found = glob(query) found.sort() csvs.extend(found) else: csvs.append(cmd) local_output = True if coords == "pixels": out_folder = '{rp}{ds}_{r}/detections/'.format(rp=runs_path, ds=dataset, r=run) elif coords == "world": out_folder = '{rp}{ds}_{r}/detections_world/'.format(rp=runs_path, ds=dataset, r=run) mkdir(out_folder) for csv_path in csvs: vidname = right_remove(csv_path.split('/')[-1], '.csv') if coords == "world": vidname = right_remove(vidname, '_world') vid_path = "{dsp}{ds}/videos/{v}.mkv".format(dsp=datasets_path, ds=dataset, v=vidname) if local_output: outvid_path = '{}.mp4'.format(vidname) else: outvid_path = '{}{}.mp4'.format(out_folder, vidname) detections = pd.read_csv(csv_path) detections_video(detections, vid_path, outvid_path, classnames, dataset, res, fps=fps, conf_thresh=conf, coords=coords) print_flush(outvid_path) print_flush("Done!")
def get_list_of_videos(dataset_name): dataset_name = quote(dataset_name) vids = glob("{dsp}{dn}/videos/*.mkv".format(dsp=datasets_path, dn=dataset_name)) vids.sort() vids = [right_remove(x.split('/')[-1], '.mkv') for x in vids] if vids: return (vids, 200) else: return (NoContent, 404)
def main(cmd, dataset, imsize, visualize): imsize = parse_resolution(imsize) mask = Masker(dataset) if cmd == "findvids" or cmd == "continue": vidfolder = "{}{}/videos/".format(datasets_path, dataset) kltfolder = "{}{}/klt/".format(datasets_path, dataset) mkdir(kltfolder) allvids = sorted(glob(vidfolder + "*.mkv")) if cmd == "continue": existing = sorted(glob(kltfolder + "*.pklz")) existing = [ right_remove(x.split('/')[-1], '.pklz') for x in existing ] allvids = [ x for x in allvids if not right_remove(x.split('/')[-1], '.mkv') in existing ] for vidpath in allvids: datpath = kltfolder + vidpath.split('/')[-1].replace( '.mkv', '.pklz') if visualize: outvidpath = datpath.replace('.pklz', '_klt.mp4') print_flush("{} -> {} & {}".format(vidpath, datpath, outvidpath)) else: outvidpath = None print_flush("{} -> {}".format(vidpath, datpath)) klt_save(vidpath, datpath, imsize, mask, outvidpath) print_flush("Done!") else: raise (ValueError())
def main(cmd, res, dataset, run, conf, fps, coords): res = parse_resolution(res) classnames = get_classnames(dataset) local_output = False csvs = [] if cmd == "findvids": if coords == "pixels": found = (runs_path / "{}_{}".format(dataset,run) / "csv").glob('*.csv') elif coords == "world": found = (runs_path / "{}_{}".format(dataset,run) / "detections_world").glob('*.csv') found = list(found) found.sort() csvs.extend(found) else: csvs.append(cmd) local_output = True if coords == "pixels": out_folder = runs_path / "{}_{}".format(dataset,run) / "detections" elif coords == "world": out_folder = runs_path / "{}_{}".format(dataset,run) / "detections_world" mkdir(out_folder) for csv_path in csvs: vidname = csv_path.stem if coords == "world": vidname = right_remove(vidname, '_world') vid_path = datasets_path / dataset / "videos" / (vidname+'.mkv') if local_output: outvid_path = Path('.') / '{}.mp4'.format(vidname) else: outvid_path = out_folder / '{}.mp4'.format(vidname) detections = pd.read_csv(csv_path) detections_video(detections, vid_path, outvid_path, classnames, dataset, res, fps=fps, conf_thresh=conf, coords=coords) print_flush(outvid_path) print_flush("Done!")
def get_frame_number(self, t): """ Gets the video name and frame number from a datetime object (t). """ all_logs = glob("{lp}*.log".format(lp=self.logs_path)) all_logs.sort() video_names = [right_remove(x.split('/')[-1], '.log') for x in all_logs] if self.start_times is None: self.start_times = dict() for video_name, log in zip(video_names, all_logs): with open(log, 'r') as f: first_line = f.readline().rstrip() first_time, _ = line_to_datetime(first_line) self.start_times[video_name] = first_time # Find video which starts at or before t, as close as possible best_secs = float("inf") best_vid = None best_log = None for video_name, log in zip(video_names, all_logs): video_start = self.start_times[video_name] dt = t - video_start secs = dt.total_seconds() if secs >= 0: if secs < best_secs: best_secs = secs best_vid = video_name best_log = log if not (best_vid is None): frame_num = self.get_frame_number_given_vidname(t, best_vid) return best_vid, frame_num else: return None, None
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!")
def main(dataset, run, videos): # Note: This main function only works for world coordinate tracks! calib = Calibration(dataset) dc = DatasetConfig(dataset) masker = Masker(dataset) if videos == 'all': from glob import glob files = glob('{rp}{ds}_{r}/tracks_world/*_tracks.pklz'.format( rp=runs_path, ds=dataset, r=run)) video_names = [ right_remove(x.split('/')[-1], '_tracks.pklz') for x in files ] elif videos.startswith('random:'): num = int(left_remove(videos, 'random:')) from glob import glob files = glob('{rp}{ds}_{r}/tracks_world/*_tracks.pklz'.format( rp=runs_path, ds=dataset, r=run)) all_video_names = [ right_remove(x.split('/')[-1], '_tracks.pklz') for x in files ] video_names = [] while len(video_names) < num: video_name = choice(all_video_names) if not video_name in video_names: video_names.append(video_name) # Just in case user wants more videos than there are if len(video_names) == len(all_video_names): break else: # Assumes the user types one or more videos, separated by commas with no spaces video_names = videos.split(',') # In case user includes endings video_names = [right_remove(x.rstrip, '.mkv') for x in video_names] # In case user includes spaces video_names = [x.strip(' ') for x in video_names] print_flush("Chosen videos: ") print_flush(str(video_names)) for video_name in video_names: print_flush(video_name) print_flush("Loading...") tracks = load('{rp}{ds}_{r}/tracks_world/{v}_tracks.pklz'.format( rp=runs_path, ds=dataset, r=run, v=video_name)) vidpath = "{dsp}{ds}/videos/{v}.mkv".format(dsp=datasets_path, ds=dataset, v=video_name) if not isfile(vidpath): raise (ValueError("Incorrect input {}".format(videos))) outvidpath = '{rp}{ds}_{r}/tracks_world/{v}_tracks.mp4'.format( rp=runs_path, ds=dataset, r=run, v=video_name) print_flush("Rendering...") render_video(tracks, vidpath, outvidpath, mask=masker, id_mode="global", calib=calib, fps=dc.get('video_fps')) print_flush("Done!")
def main(cmd, dataset, run, vidres, ssdres, kltres, conf, make_videos): from storage import load, save from folder import datasets_path, runs_path mask = Masker(dataset) #v = '20170516_163607_4C86' #v = '20170516_121024_A586' if cmd == "findvids": from glob import glob vidnames = glob('{}{}/videos/*.mkv'.format(datasets_path, dataset)) vidnames = [right_remove(x.split('/')[-1], '.mkv') for x in vidnames] vidnames.sort() outfolder = '{}{}_{}/tracks/'.format(runs_path, dataset, run) else: vidnames = [cmd] outfolder = './' vidres = parse_resolution(vidres) ssdres = parse_resolution(ssdres) kltres = parse_resolution(kltres) x_factor = float(vidres[0]) / ssdres[0] y_factor = float(vidres[1]) / ssdres[1] det_dims = ('xmin', 'xmax', 'ymin', 'ymax') det_factors = (x_factor, x_factor, y_factor, y_factor) c = Config(vidres, kltres, conf) from folder import mkdir mkdir(outfolder) for v in vidnames: detections = pd.read_csv('{}{}_{}/csv/{}.csv'.format( runs_path, dataset, run, v)) for dim, factor in zip(det_dims, det_factors): detections[dim] = round(detections[dim] * factor).astype(int) klt = load('{}{}/klt/{}.pklz'.format(datasets_path, dataset, v)) klt, klt_frames = convert_klt(klt, c) tracks = [] if len(detections) > 0: tracks = build_tracks(detections, klt, klt_frames, c) print_flush("{} tracks done".format(v)) save(tracks, '{}{}_tracks.pklz'.format(outfolder, v)) else: print_flush( "{} skipping tracking, because there were no detections". format(v)) if make_videos: if tracks: from visualize_tracking import render_video vidpath = "{}{}/videos/{}.mkv".format(datasets_path, dataset, v) render_video(tracks, vidpath, "{}{}_tracks.mp4".format(outfolder, v), mask=mask) print_flush("{} video done".format(v)) else: print_flush( "{} skipping video rendering, because there were no tracks" .format(v)) print_flush("Done!")
def main(cmd, dataset, run, vidres, ssdres, kltres, make_videos): vidres = parse_resolution(vidres) ssdres = parse_resolution(ssdres) kltres = parse_resolution(kltres) x_factor = float(vidres[0]) / ssdres[0] y_factor = float(vidres[1]) / ssdres[1] det_dims = ('xmin', 'xmax', 'ymin', 'ymax') det_factors = (x_factor, x_factor, y_factor, y_factor) calib = Calibration(dataset) ts = Timestamps(dataset) class_data = get_class_data(dataset) class_heights = {d['name']: d['height'] for d in class_data} 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] if cmd == "findvids": from glob import glob vidnames = glob('{dsp}{ds}/videos/*.mkv'.format(dsp=datasets_path, ds=dataset)) vidnames = [right_remove(x.split('/')[-1], '.mkv') for x in vidnames] vidnames.sort() outfolder = '{}{}_{}/detections_world/'.format(runs_path, dataset, run) mkdir(outfolder) else: vidnames = [cmd] outfolder = './' mkdir(outfolder) if make_videos: classnames = get_classnames(dataset) dc = DatasetConfig(dataset) fps = dc.get('video_fps') for v in vidnames: print_flush(v) detections = pd.read_csv('{}{}_{}/csv/{}.csv'.format( runs_path, dataset, run, v)) # Convert pixel coordinate positions from SSD resolution to video resolution # because Calibration assumes video resolution coordinates for dim, factor in zip(det_dims, det_factors): detections[dim] = round(detections[dim] * factor).astype(int) print_flush("Converting point tracks...") klt = load('{}{}/klt/{}.pklz'.format(datasets_path, dataset, v)) klt, klt_frames = convert_klt(klt, klt_config) pts = PointTrackStructure(klt, klt_frames, vidres[0], vidres[1]) outpath = '{of}{v}_world.csv'.format(of=outfolder, v=v) print_flush("Converting to world coordinates...") detections3D = detections_to_3D(detections, pts, calib, ts, v, klt_save_path=outpath.replace( '.csv', '_klt.pklz'), class_heights=class_heights) detections3D.to_csv(outpath, float_format='%.4f') if make_videos: from visualize_detections import detections_video vidpath = "{dsp}{ds}/videos/{v}.mkv".format(dsp=datasets_path, ds=dataset, v=v) print_flush("Rendering video...") detections_video(detections3D, vidpath, outpath.replace('.csv', '.mp4'), classnames, dataset, vidres, fps=fps, conf_thresh=0.0, coords='world') print_flush("Done!")