Ejemplo n.º 1
0
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)
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def post_point_tracks_job(dataset_name, visualize, overwrite):
    assert (type(visualize) == bool)
    assert (type(overwrite) == bool)

    cmd = "findvids"
    if not overwrite:
        cmd = "continue"

    dataset_name = quote(dataset_name)
    dc = DatasetConfig(dataset_name)
    if dc.exists:
        cmd = [
            python_path, "klt.py", "--cmd={}".format(cmd),
            "--dataset={}".format(dataset_name),
            "--imsize={}".format(dc.get('point_track_resolution')),
            "--visualize={}".format(visualize)
        ]

        job_id = jm.run(cmd, "point_tracks")
        if job_id:
            return (job_id, 202)
        else:
            return (NoContent, 503)

    else:
        return (NoContent, 404)
Ejemplo n.º 4
0
def post_autoannotate_job(dataset_name,
                          import_datasets="",
                          epochs=75,
                          resolution="(640,480,3)"):
    dataset_name = quote(dataset_name)
    resolution = quote(resolution)

    dc = DatasetConfig(dataset_name)
    if dc.exists:
        cmd = [
            python_path, "autoannotate.py",
            "--dataset={}".format(dataset_name),
            "--input_shape={}".format(resolution),
            "--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, "autoannotate")
        if job_id:
            return (job_id, 202)
        else:
            return (NoContent, 503)
    else:
        return (NoContent, 404)
Ejemplo n.º 5
0
def post_prepare_annotations_job(dataset_name, less_night=True):
    assert(type(less_night) == bool)
    dataset_name = quote(dataset_name)
    dc = DatasetConfig(dataset_name)
    
    if dc.exists:
        cmd = [python_path, "annotation_preparation.py",
               "--dataset={}".format(dataset_name),
               "--num_ims={}".format(dc.get('images_to_annotate')),
               "--ims_per_vid={}".format(dc.get('images_to_annotate_per_video')),
               "--train_amount={}".format(dc.get('annotation_train_split')),
               "--night={}".format(less_night)]
        
        job_id = jm.run(cmd, "prepare_annotations")
        if job_id:
            return (job_id, 202)
        else:
            return (NoContent, 503)
    else:
        return (NoContent, 404)
Ejemplo n.º 6
0
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)
Ejemplo n.º 7
0
def get_annotation_slideshow(dataset_name):
    dataset_name = quote(dataset_name)
    dc = DatasetConfig(dataset_name)

    if dc.exists:
        imsize = dc.get('video_resolution')
        outpath = datasets_path / dataset_name / "slideshow.mp4"
        res = slideshow(dataset_name, outpath)

        if not res:
            return ("Failed to make slideshow", 404)
        else:
            vid = send_file(str(outpath), mimetype='video/mp4')
            return (vid, 200)
    else:
        return ("Dataset does not exist", 404)
Ejemplo n.º 8
0
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)
Ejemplo n.º 9
0
def post_rare_class_mining_job(dataset_name,
                               class_name,
                               confidence,
                               time_distance,
                               time_sampling,
                               import_datasets="",
                               epochs=75,
                               resolution="(300,300,3)"):
    dataset_name = quote(dataset_name)
    class_name = quote(class_name)
    resolution = quote(resolution)

    dc = DatasetConfig(dataset_name)
    if dc.exists:
        cmd = [
            python_path, "rare_class_mining.py",
            "--dataset={}".format(dataset_name),
            "--class_name={}".format(class_name),
            "--confidence={}".format(confidence),
            "--time_dist={}".format(time_distance),
            "--sampling_rate={}".format(time_sampling),
            "--epochs={}".format(epochs),
            "--input_shape={}".format(resolution),
            "--image_shape={}".format(dc.get('video_resolution'))
        ]

        if import_datasets:
            import_datasets = quote(import_datasets)
            cmd.append("--import_datasets={}".format(import_datasets))

        job_id = jm.run(cmd, "rare_class_mining")
        if job_id:
            return (job_id, 202)
        else:
            return (NoContent, 503)
    else:
        return ("Dataset does not exists or is not configured", 404)
def main(dataset, run, date, gt_csv, det_id, gt_class_name_conversion,
         visualize, patience, per_iteration):

    dc = DatasetConfig(dataset)
    vidres = dc.get('video_resolution')
    width, height, _ = vidres
    if width > height:
        stack_axis = 'v'
    else:
        stack_axis = 'h'

    if gt_class_name_conversion is None:
        print_flush("Not converting class names")
    else:
        print_flush("Using class conversion:")
        print_flush(gt_class_name_conversion)
        gt_class_name_conversion = json.loads(gt_class_name_conversion)
        assert (type(gt_class_name_conversion) == dict)

    print_flush("Interpreting ground truth...")
    gt = interpret_tracks_gt(dataset, date, det_id, gt_csv)

    print_flush("Optimizing...")

    config_min = {
        'time_drop_thresh': 0.1,  # in seconds
        'time_region_check_thresh': 0.1,  # in seconds
        'creation_too_close_thresh': 1,  # in meters
        'is_too_close_thresh': {
            'default': 0.2,
            'bicycle_bicycle': 0.1
        },  # in metres
        'incorrect_class_cost': {
            'default': 100,
            'bicycle_person': 3,
            'person_bicycle': 3
        },  # unitless? Compared with WorldTrack.cost output
        'cost_thresh': {
            'default': 5,
            'bicycle': 5
        },  # unitless? Compared with WorldTrack.cost output
        'mask_margin':
        0,  # in pixels, how close to the borders of the interesting region a track can be
        'cost_dist_weight': 0.5,
        'cost_dir_weight': 0.5,
    }

    config_max = {
        'time_drop_thresh': 7.0,  # in seconds
        'time_region_check_thresh': 2.0,  # in seconds
        'creation_too_close_thresh': 10,  # in meters
        'is_too_close_thresh': {
            'default': 3.0,
            'bicycle_bicycle': 2.0
        },  # in metres
        'incorrect_class_cost': {
            'default': 123456789123456789,
            'bicycle_person': 30,
            'person_bicycle': 30
        },  # unitless? Compared with WorldTrack.cost output
        'cost_thresh': {
            'default': 25,
            'bicycle': 35
        },  # unitless? Compared with WorldTrack.cost output
        'mask_margin':
        15,  # in pixels, how close to the borders of the interesting region a track can be
        'cost_dist_weight': 2.0,
        'cost_dir_weight': 2.0,
    }

    config_min, config_max = map(WorldTrackingConfig, (config_min, config_max))

    base_path = runs_path / "{}_{}".format(dataset, run)
    plot_path = base_path / 'world_tracking_optimization.png'

    config, tracks = optimize_tracking(config_min,
                                       config_max,
                                       dataset,
                                       run,
                                       gt,
                                       gt_class_name_conversion,
                                       plot_path=plot_path,
                                       patience=patience,
                                       n=per_iteration)

    save(config, base_path / 'world_tracking_optimization.pklz')

    if visualize:
        print_flush("Visualizing...")
        visualize_tracks(base_path / 'world_tracking_optimization.mp4',
                         dataset,
                         gt,
                         tracks,
                         stack_axis=stack_axis)

    print_flush("Done!")
def visualize_tracks(outvidpath, dataset, gts, tracks=None, stack_axis='v'):
    import imageio as iio
    from visualize_tracking import _draw_world, draw_world
    from visualize import class_colors
    from apply_mask import Masker
    from config import DatasetConfig

    if not (tracks is None):
        calib = Calibration(dataset)

        # Reset IDs
        tracks = sorted(tracks, key=lambda x: x.history[0][0])
        for track in tracks:
            track.id = i
            i += 1

    dc = DatasetConfig(dataset)

    gts_by_vid = split_lambda(gts, lambda x: x[0])
    assert (len(gts_by_vid) == 1)
    vid = list(gts_by_vid.keys())[0]

    n_colors = 50
    colors = class_colors(n_colors)

    mask = Masker(dataset)

    with iio.get_writer(outvidpath, fps=dc.get('video_fps')) as outvid:
        with iio.get_reader(datasets_path / dataset / "videos" /
                            (vid + '.mkv')) as invid:

            gt_by_frame = split_lambda(gts, lambda x: x[1])
            fns = list(gt_by_frame.keys())
            fns.sort()

            for fn in fns:
                gts_frame = gt_by_frame[fn]

                frame = invid.get_data(fn)
                frame = mask.mask(frame, alpha=0.5)

                if not (tracks is None):
                    tracks_frame = frame.copy()

                for gt in gts_frame:
                    vid, fn, t, x, y, i, c, px, py = gt

                    text = "{} {}".format(c, i)
                    col = colors[i % n_colors]

                    frame = _draw_world(frame, text, px, py, col)

                if not (tracks is None):
                    for track in tracks:
                        draw_world(tracks_frame, track, fn,
                                   colors[track.id % n_colors], calib)

                    if stack_axis == 'h':
                        frame = np.hstack((frame, tracks_frame))
                    elif stack_axis == 'v':
                        frame = np.vstack((frame, tracks_frame))
                    else:
                        raise (ValueError(
                            "Incorrect stack axis {}, try 'h' or 'v'".format(
                                stack_axis)))

                outvid.append_data(frame)
Ejemplo n.º 12
0
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!")
Ejemplo n.º 13
0
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":
        vidnames = list((datasets_path / dataset / "videos").glob('*.mkv'))
        vidnames = [x.stem for x in vidnames]
        vidnames.sort()

        outfolder = runs_path / '{}_{}'.format(dataset,
                                               run) / 'detections_world'
        mkdir(outfolder)
    else:
        vidnames = [cmd]
        outfolder = Path('.')

    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(runs_path / '{}_{}'.format(dataset, run) /
                                 'csv' / (v + '.csv'))

        # 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(datasets_path / dataset / 'klt' / (v + '.pklz'))
        klt, klt_frames = convert_klt(klt, klt_config)
        pts = PointTrackStructure(klt, klt_frames, vidres[0], vidres[1])

        outpath = outfolder / '{v}_world.csv'.format(v=v)

        print_flush("Converting to world coordinates...")
        detections3D = detections_to_3D(
            detections,
            pts,
            calib,
            ts,
            v,
            klt_save_path=outpath.with_name(outpath.stem + '_klt.pklz'),
            class_heights=class_heights)

        detections3D.to_csv(outpath, float_format='%.4f')

        if make_videos:
            from visualize_detections import detections_video
            vidpath = datasets_path / dataset / "videos" / "{}.mkv".format(v)

            print_flush("Rendering video...")
            detections_video(detections3D,
                             vidpath,
                             outpath.with_suffix('.mp4'),
                             classnames,
                             dataset,
                             vidres,
                             fps=fps,
                             conf_thresh=0.0,
                             coords='world')

    print_flush("Done!")
Ejemplo n.º 14
0
def main(dataset, times, images_per_time, interval):
    times = times.strip(
        "'"
    )  # These are added around it by the quote function in server.py, to make sure it is a single argument instead of being split by the spaces

    ts = Timestamps(dataset)

    dc = DatasetConfig(dataset)
    fps = dc.get('video_fps')
    half_interval = int((fps * interval) / 2)  # in frames

    timestrings = times.split(',')
    for timestring in timestrings:
        print_flush(timestring)

        # Intepret the requested times, can look like '2017-05-16 00:49:04.954000'
        splot = timestring.split(' ')
        date = splot[0].split('-')
        time = splot[1].replace('.', ':').split(':')

        year, month, day = map(int, date)
        hour, minute, second, microsecond = map(int, time)

        timestamp = datetime(year, month, day, hour, minute, second,
                             microsecond)
        vid_name, frame_num = ts.get_frame_number(timestamp)

        print_flush("Time found to be {}, frame {}".format(
            vid_name, frame_num))

        if vid_name is None:
            raise (ValueError(
                "This timestamp was incorrect: {} Could it be before the first video?"
                .format(timestring)))

        video_path = datasets_path / dataset / "videos" / (vid_name + '.mkv')

        annot_folder = datasets_path / dataset / "objects" / "train" / vid_name
        log_path = annot_folder / 'frames.log'
        if not log_path.is_file():
            with log_path.open('w') as f:
                f.write("{}.mkv\n".format(vid_name))

        # See which frames were already annotated, to start at the right index
        already_ims = list(annot_folder.glob('*.jpg'))
        if already_ims:
            already_nums = [int(x.stem) for x in already_ims]
            i = max(already_nums) + 1
        else:
            i = 1

        with iio.get_reader(video_path) as vid:
            # Find start and end time, in frames
            start = frame_num - half_interval
            if start < 0:
                start = 0

            stop = frame_num + half_interval
            if stop >= len(vid):
                stop = len(vid) - 1

            with open(log_path, 'a') as log:

                # Choose frames to extract
                frame_nums = np.linspace(start, stop,
                                         images_per_time).astype(int).tolist()
                frame_nums = sorted(list(set(frame_nums)))  # Remove duplicates

                for frame_num in frame_nums:
                    frame = vid.get_data(frame_num)

                    log.write("{} ".format(frame_num))

                    impath = annot_folder / "{}.jpg".format(i)
                    imsave(impath, frame)

                    i += 1

                    print_flush("> Written {}".format(impath))

    print_flush("Done!")
Ejemplo n.º 15
0
def main(cmd, dataset, run, conf, make_videos):   
    from pathlib import Path
    
    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 = runs_path / "{}_{}".format(dataset,run) / "world_tracking_optimization.pklz"
    if config_path.is_file():
        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":
        vidnames = (datasets_path / dataset / "videos").glob('*.mkv')
        vidnames = [x.stem for x in vidnames]
        vidnames.sort()
        
        outfolder = runs_path / "{}_{}".format(dataset,run) / "tracks_world"
        mkdir(outfolder)
    else:
        vidnames = [cmd]
        outfolder = Path('./')
        start_stop = (0,500)
            
    for v in vidnames:
        print_flush(v) 
        out_path = outfolder / (v+'_tracks.pklz')   
        
        print_flush("Loading data...")
        det_path = runs_path / "{}_{}".format(dataset,run) / "detections_world" / (v+'_world.csv')
        detections3D = pd.read_csv(det_path)
        
        klt_path = det_path.with_name(det_path.stem + '_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 = datasets_path / dataset / "videos" / (v+'.mkv')
            print_flush("Rendering video...")
            render_video(tracks, vidpath, out_path.with_suffix('.mp4'), calib=calib, mask=mask, fps=dc.get('video_fps'))

    print_flush("Done!")
Ejemplo n.º 16
0
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!")
Ejemplo n.º 17
0
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!")
Ejemplo n.º 18
0
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!")