Esempio n. 1
0
    def save_video(self, video, audio, path, experimental_ffmpeg=False):
        with tempdir() as dirpath:
            # Save the video file
            writer = sio.FFmpegWriter(dirpath + "/tmp.avi",
                                      inputdict={
                                          '-r': str(self.video_rate) + "/1",
                                      },
                                      outputdict={
                                          '-r': str(self.video_rate) + "/1",
                                      })
            for i in range(video.shape[0]):
                writer.writeFrame(np.rollaxis(video[i, :, :, :], 0, 3))
            writer.close()

            # Save the audio file
            wav.write(dirpath + "/tmp.wav", self.audio_rate, audio)

            in1 = ffmpeg.input(dirpath + "/tmp.avi")
            in2 = ffmpeg.input(dirpath + "/tmp.wav")
            if experimental_ffmpeg:
                out = ffmpeg.output(in1['v'],
                                    in2['a'],
                                    path,
                                    strict='-2',
                                    loglevel="panic")
            else:
                out = ffmpeg.output(in1['v'], in2['a'], path, loglevel="panic")
            out.run()
Esempio n. 2
0
    def save_video(self, video, audio, path, fs, overwrite=True, experimental_ffmpeg=False, scale=None):
        if not os.path.isabs(path):
            path = os.getcwd() + "/" + path

        with tempdir() as dirpath:
            # Save the video file
            writer = sio.FFmpegWriter(dirpath + "/tmp.avi",
                                      inputdict={'-r': str(self.video_rate) + "/1", },
                                      outputdict={'-r': str(self.video_rate) + "/1", }
                                      )
            for i in range(video.shape[0]):
                frame = np.rollaxis(video[i, :, :, :], 0, 3)

                if scale is not None:
                    frame = tf.rescale(frame, scale, anti_aliasing=True, multichannel=True, mode='reflect')

                writer.writeFrame(frame)
            writer.close()

            # Save the audio file
            wav.write(dirpath + "/tmp.wav", fs, audio)

            in1 = ffmpeg.input(dirpath + "/tmp.avi")
            in2 = ffmpeg.input(dirpath + "/tmp.wav")
            if experimental_ffmpeg:
                out = ffmpeg.output(in1['v'], in2['a'], path, strict='-2', loglevel="panic")
            else:
                out = ffmpeg.output(in1['v'], in2['a'], path, loglevel="panic")

            if overwrite:
                out = out.overwrite_output()
            out.run()
Esempio n. 3
0
    def __start__(self, name):
        self._maskfile = None
        self._resultfiles = {}
        self._masknames = tuple(maskname for maskname in self.masks.keys())
        basename = _os.path.splitext(name)[0]
        ensure_directory(self.maskdir)
        ensure_directory(self.resultdir)

        for roiname, roi in get_items(self.ROIs):
            if roi.is_empty():
                continue
            resultpath = _os.path.join(
                self.resultdir,
                "Results_{0}_{1}.csv".format(basename, roiname))
            # TODO: create mask file
            try:
                self._resultfiles[roiname] = open(resultpath, 'w')
                self._resultfiles[roiname].write('Slice,' + ','.join(
                    _mask_entries(m) for m in self._masknames))
                self._resultfiles[roiname].write('\n')
            except:
                _print_exc()
                print_status("*** could not open: {0}".format(resultpath))
        if len(self._resultfiles) == 0:
            raise RuntimeError("nothing to output for {0}".format(name))

        # maskpath = _os.path.join(self.maskdir, "MASK_{0}.avi".format(basename, roiname))
        maskpath = _os.path.join(self.maskdir,
                                 "MASK_{0}.mp4".format(basename, roiname))
        try:
            self._maskfile = _vio.FFmpegWriter(maskpath,
                                               outputdict=OUTPUT_SPECS)
        except:
            _print_exc()
            print_status("*** could not open: {0}".format(maskpath))
Esempio n. 4
0
 def _record_frame():
     global video_out
     last_frame_time = None
     out_fps = 30
     out_frame_interval = 1.0 / out_fps
     while not video_out_should_stop:
         frame_index = video_out_queue.get()
         if frame_index is None:
             break
         assert frame_index in data_source._frames
         frame = data_source._frames[frame_index]['bgr']
         h, w, _ = frame.shape
         if video_out is None:
             # video_out = cv.VideoWriter(
             #     args.record_video, cv.VideoWriter_fourcc(*'H264'),
             #     out_fps, (w, h),
             # )
             video_out = skv.FFmpegWriter(args.record_video)
         now_time = time.time()
         if last_frame_time is not None:
             time_diff = now_time - last_frame_time
             while time_diff > 0.0:
                 #video_out.write(frame)
                 video_out.writeFrame(np.flip(frame, axis=-1))
                 time_diff -= out_frame_interval
         last_frame_time = now_time
     #video_out.release()
     video_out.close()
     with video_out_done:
         video_out_done.notify_all()
Esempio n. 5
0
    def run(self):
        """ Continually write images to the filename specified by a command queue. """
        if not self.camera.is_running:
            self.camera.start()
        while True:
            if not self.cmd_q.empty():
                cmd = self.cmd_q.get()
                if cmd[0] == 'stop':
                    self.out.close()
                    self.recording = False
                elif cmd[0] == 'start':
                    filename = cmd[1]
                    self.out = si.FFmpegWriter(filename)
                    self.recording = True
                    self.count = 0

            if self.recording:
                if self.count == 0:
                    image, _, _ = self.camera.frames()

                    if self.data_buf is None:
                        self.data_buf = np.zeros(
                            [1, image.height, image.width, image.channels])
                    self.data_buf[0, ...] = image.raw_data
                    self.out.writeFrame(self.data_buf)

                self.count += 1
                if self.count == self.rate:
                    self.count = 0
Esempio n. 6
0
def predict(data_dir, output_dir, checkpoint, batch_size):
    in_place, output_video_t = build_forward(INPUT_SIZE, NUM_FRAMES,
                                             tf.estimator.ModeKeys.PREDICT)

    saver = tf.train.Saver()
    with tf.Session() as sess:
      try:
        saver.restore(sess, 'weights/{}_best'.format(checkpoint))
      except Exception as e:
        print('{}: {} is not a valid checkpoint to restore from.'.format(e, checkpoint))

      prediction_folder = os.path.join(output_dir, 'output')
      # check that prediction folder exists
      if not os.path.exists(prediction_folder):
        os.mkdir(prediction_folder) 
        
      input_videos = glob.glob(os.path.join(data_dir, '/*.mp4'))
      
      for ivf in input_videos:
        in_video = vidio.vread(ivf)
        out_video = sess.run([output_video_t],
            { in_place : in_video[:NUM_FRAMES, :INPUT_SIZE, :INPUT_SIZE] })
        base_filename = ivf.split('/')[-1]
        out_filename = os.path.join(prediction_folder, base_filename)
        writer = vidio.FFmpegWriter(out_filename)
        for i in range(out_video.shape[0]):
          writer.writeFrame(out_video[i,...])
        writer.close()
Esempio n. 7
0
def main():
    # parse the arguments
    parser = argparse.ArgumentParser(description='Retouch video partially.')
    parser.add_argument('--src_path',
                        type=str,
                        default='../originals',
                        help='source path')
    parser.add_argument('--dst_path',
                        type=str,
                        default='../retouch_temporal_videos',
                        help='destination path')
    parser.add_argument('--intensity',
                        type=str,
                        default='strong',
                        help='strong or weak')
    args = parser.parse_args()

    SRC_PATH = args.src_path
    DST_PATH = args.dst_path
    INTENSITY = args.intensity

    methods = ["blur", "median", "noise"]

    fnames = glob(join(SRC_PATH, "*.mp4"))

    for fname in fnames:
        vOriginal = np.array(vio.vread(fname, inputdict={}))
        vRetouched = np.zeros(vOriginal.shape)

        fn, w, h, c = vOriginal.shape

        start_fn = int(fn / 3)
        end_fn = int(fn * 2 / 3)

        for method in methods:

            print("processing {}".format(
                join(DST_PATH, INTENSITY, method,
                     fname.split("\\")[-1])))

            for idx in range(fn):
                if idx >= start_fn and idx <= end_fn:
                    vRetouched[idx] = manipulate(vOriginal[idx],
                                                 method,
                                                 intensity=INTENSITY)
                else:
                    vRetouched[idx] = vOriginal[idx]

            write_option['-b:v'] = "800k"
            writer = vio.FFmpegWriter(filename=join(DST_PATH, INTENSITY,
                                                    method,
                                                    fname.split("\\")[-1]),
                                      inputdict={'-r': fps},
                                      outputdict=write_option)
            for frame in vRetouched:
                writer.writeFrame(frame)
            writer.close()
Esempio n. 8
0
    def __init__(self, vreader, output_record_path="", output_video_path="", heatmap=False):
        # Parameters
        self.vreader = vreader
        self.heatmap = heatmap
        self.output_video_flag = True if output_video_path else False
        self.output_record_flag = True if output_record_path else False
        self.vwriter = skv.FFmpegWriter(output_video_path) if self.output_video_flag else None
        self.results_recorder = open(output_record_path, "w") if self.output_record_flag else None

        # Initialization actions
        self._initialize_results_recorder()
 def decrypt_vid(self, src, dest):
     vid = skv.vread(src)
     # extract LSB and then set to 0
     lsb = np.bitwise_and(vid, 1).astype(bool)
     vid[:] = np.bitwise_and(vid, ~1)
     binary = self.binarize_4d(vid)
     self.arnold_4d(lsb, n=(192 - len(vid)))
     # localize
     xor = np.bitwise_xor(binary, lsb)
     params = {'-c:v': 'ffv1'}
     with skv.FFmpegWriter(dest, outputdict={'-c:v': 'ffv1'}) as f:
         [f.writeFrame(xor[n] * 192) for n in range(len(vid))]
 def tamper_vid(self, src, dest, n_tamper=20, save=False):
     vid = skv.vread(src)
     t, x, y, _ = vid.shape
     rand_t = np.random.randint(t, size=n_tamper)
     rand_x = np.random.randint(x, size=n_tamper)
     rand_y = np.random.randint(y, size=n_tamper)
     rand_z = np.random.randint(255, size=n_tamper)
     for n in range(n_tamper):
         vid[rand_t[n], rand_x[n]:rand_y[n], rand_x[n]:
             rand_y[n], :] = rand_z[n]
     with skv.FFmpegWriter(dest, outputdict={'-c:v': 'ffv1'}) as f:
         [f.writeFrame(vid[n]) for n in range(len(vid))]
     if save:
         tmp = np.zeros(vid.shape, dtype=vid.dtype)
         for n in range(n_tamper):
             tmp[rand_t[n], rand_x[n]:rand_y[n], rand_x[n]:
                 rand_y[n], :] = rand_z[n]
         with skv.FFmpegWriter(
                 dest.replace('.avi', '_tmp.avi'),
                 outputdict={'-c:v': 'ffv1'}) as f:
             [f.writeFrame(tmp[n]) for n in range(len(tmp))]
Esempio n. 11
0
def process_directories(model_dirs, out_dir, gpu_idx=0):
    """Load the latest snapshot in each of the given model directories, and
    render a video for each test variant corresponding to the train variants in
    the model. Note that the video will show all models in sequence."""
    set_seeds(DEFAULT_SEED)
    register_envs()

    if out_dir:
        os.makedirs(out_dir, exist_ok=True)

    if gpu_idx is None:
        dev = torch.device('cpu')
    else:
        dev = torch.device(f'cuda:{gpu_idx}')

    # we're going to keep a video writer for each env
    writers_by_env = {}

    for model_idx, model_dir in enumerate(model_dirs):
        print(f"Loading model from '{model_dir}' "
              f"(model {model_idx+1}/{len(model_dirs)})")
        latest_path = get_latest_path(os.path.join(model_dir,
                                                   "itr_LATEST.pkl"))
        model = load_state_dict_or_model(latest_path)
        model = model.to(dev)
        test_envs = get_test_envs(model.env_ids_and_names)

        for test_env in test_envs:
            if test_env not in writers_by_env:
                vid_name = generate_vid_name(test_env)
                out_path = os.path.join(out_dir, vid_name)
                print(f"  Writing video for '{test_env}' to '{out_path}'")
                writer = vidio.FFmpegWriter(out_path,
                                            outputdict={
                                                '-r': str(DEFAULT_FPS),
                                                '-vcodec': 'libx264',
                                                '-pix_fmt': 'yuv420p',
                                            })
                writers_by_env[test_env] = writer
            else:
                print(f"  Writing video for '{test_env}'")
                writer = writers_by_env.get(test_env)
            # will be something like, e.g., "movetocorner-testjitter.mp4"
            for frame in generate_frames(model=model,
                                         env_name=test_env,
                                         dev=dev,
                                         seed=DEFAULT_SEED + model_idx,
                                         ntraj=NTRAJ_PER_MODEL,
                                         fps=DEFAULT_FPS):
                writer.writeFrame(frame)

    for writer in writers_by_env.values():
        writer.close()
Esempio n. 12
0
    def combine_graphs_to_video(self, frame_rate='120/1'):
        all_graphs_paths = glob(os.path.join(self.get_graph_dir(), "*.png"))
        total_graphs_num = len(all_graphs_paths)
        self.vwriter = skv.FFmpegWriter(self.get_video_path(),
                                        inputdict={'-r': frame_rate},
                                        outputdict={'-r': frame_rate})
        print("Generate video...")
        for idx in range(total_graphs_num):
            print("\r{}/{}".format(idx, total_graphs_num), end="", flush=True)
            graph_path = self.get_graph_path(idx)
            im = ski.imread(graph_path)
            self.vwriter.writeFrame(im)

        self.vwriter.close()
 def encrypt_vid(self, src, dest):
     """
     Runs encryption on videos. Assumes a square matrix divisible by 4.
     """
     vid = skv.vread(src)
     # set LSB to 0
     vid[:] = np.bitwise_and(vid, ~1)
     binary = self.binarize_4d(vid)
     self.arnold_4d(binary, n=len(vid))
     # embed watermark
     log.info('Embedding watermark and saving')
     vid[:] = np.bitwise_or(vid, binary)
     params = {'-c:v': 'ffv1'}
     with skv.FFmpegWriter(dest, outputdict={'-c:v': 'ffv1'}) as f:
         [f.writeFrame(vid[n]) for n in range(len(vid))]
def write_snippets_to_disk(idxs,
                           output_path,
                           name: str = 'Output',
                           fps: int = 10,
                           only_frames: bool = False):
    idx = 0
    for start, end, conf in idxs:
        frames = job.Parallel(
            n_jobs=MAX_JOBS, verbose=0)(job.delayed(cv2.imread)(join(
                output_path, SUBFOLDER_PROCESSED, f'{j}.jpg'))
                                        for j in range(start, end))
        frames = [
            np.zeros((850, 850,
                      3), dtype=np.uint8) if f is None or f.size == 0 else f
            for f in frames
        ]
        max_height = max(frames, key=lambda img: img.shape[0]).shape[0]
        max_width = max(frames, key=lambda img: img.shape[1]).shape[1]
        avg_size = sum([f.shape[0] for f in frames]) / len(frames)
        frames = [
            utl.pad_image_to_size(f, (max_height, max_width)) for f in frames
        ]

        #out = cv2.VideoWriter(join(output_path, SUBFOLDER_RESULTS, f'{name}_{i}.a'), cv2.VideoWriter_fourcc('H', '2', '6', '4'), fps, size)
        if not only_frames:
            out = io.FFmpegWriter(join(output_path, SUBFOLDER_RESULTS,
                                       f'{name}_{idx}_{conf}.mp4'),
                                  inputdict={
                                      '-r': str(fps),
                                      '-s': f'{max_width}x{max_height}'
                                  },
                                  outputdict={
                                      '-vcodec': 'libx264',
                                      '-crf': '0',
                                      '-preset': 'slow'
                                  })
            for frame in frames:
                out.writeFrame(frame[:, :, [2, 1, 0]])
            out.close()
        else:
            for i, frame in enumerate(frames):
                cv2.imwrite(
                    join(output_path, SUBFOLDER_RESULTS,
                         f'{name}_{idx}_{conf}_{i}.png'), frame)

        idx += 1
Esempio n. 15
0
def get_video(video_name, start_secs, end_secs, vid_quality=1, save_dir=DEFAULT_PATH, table_name=VIDEO_TABLE_NAME):
    """Retrieve video from start seconds up to end seconds. 
    Also adjust the shrink the video frame rate according to vid_quality.
    """
    pool = happybase.ConnectionPool(size=3)
    with pool.connection() as connection:
        table = connection.table(table_name)
        # first get the frame rate
        metadata = table.row(video_name, columns=[b'mtd:fr', b'mtd:rows', b'mtd:cols', b'mtd:channels'])
        #import pdb; pdb.set_trace()
        framerate = metadata.get(b'mtd:fr', DEFAULT_FRAMERATE).decode()
        row_count = int(metadata[b'mtd:rows'].decode())
        col_count = int(metadata[b'mtd:cols'].decode())
        channels = int(metadata[b'mtd:channels'].decode())
        if len(framerate.split('/')) > 1:
            num, denum = framerate.split('/')
            framerate = int(num)/int(denum)
        framerate = round(framerate)  #frame rate = how many frames make one sec
        start_frame = start_secs * framerate
        end_frame = end_secs * framerate
        framerate = round(framerate/vid_quality)       # adjust the frame rate according to the video quality
        cols = []
        for i in range(start_frame, end_frame, vid_quality):
            cols.append(('cts:%s' %  i).encode())
        video_data = table.row(video_name, columns=cols)
        #temp_dir = tempfile.mkdtemp()
        filepath = os.path.join(save_dir, os.path.basename(video_name))
        vwriter = videoio.FFmpegWriter(filepath, inputdict={'-r': str(framerate), }, 
                                       outputdict={
                                           '-vcodec': 'libx264',
                                           '-pix_fmt': 'yuv420p',
                                           '-r': str(framerate),
                                           })
        for col in cols:
            b64_string = video_data.get(col, '')
            if b64_string:
                try:
                    frame = np.frombuffer(base64.b64decode(b64_string), np.uint8)
                    frame = np.reshape(frame, (row_count, col_count, channels))
                    vwriter.writeFrame(frame)
                except Exception as ex:
                    print('error reading frame: %s, error: %s' % (col, str(ex)))
        vwriter.close()
        return filepath
Esempio n. 16
0
def save_video(movpath, fname_mov_orig, fname_mov_rig, fname_AC, fname_ACbf,
               dsratio):
    """

    Parameters
    ----------
    movpath :

    fname_mov_orig :

    fname_mov_rig :

    fname_AC :

    fname_ACbf :

    dsratio :


    Returns
    -------


    """
    mov_orig = np.load(fname_mov_orig, mmap_mode='r')
    mov_rig = np.load(fname_mov_rig, mmap_mode='r')
    mov_ac = np.load(fname_AC, mmap_mode='r')
    mov_acbf = np.load(fname_ACbf, mmap_mode='r')
    vw = skv.FFmpegWriter(movpath,
                          inputdict={'-framerate': '30'},
                          outputdict={'-r': '30'})
    for fidx in range(0, mov_orig.shape[0], dsratio):
        print("writing frame: " + str(fidx))
        fm_orig = mov_orig[fidx, :, :] * 255
        fm_rig = mov_rig[fidx, :, :] * 255
        fm_acbf = mov_acbf[fidx, :, :] * 255
        fm_ac = mov_ac[fidx, :, :] * 255
        fm = np.concatenate([
            np.concatenate([fm_orig, fm_rig], axis=1),
            np.concatenate([fm_acbf, fm_ac], axis=1)
        ],
                            axis=0)
        vw.writeFrame(fm)
    vw.close()
Esempio n. 17
0
def generate_video(path, lenght):

    writer = io.FFmpegWriter(
        "D:\Grabacion.avi",
        outputdict={
            '-vcodec': 'libx264',  #use the h.264 codec
            '-crf': '0',  #set the constant rate factor to 0, which is lossless
            '-preset':
            'veryslow'  #the slower the better compression, in princple, try 
            #other options see https://trac.ffmpeg.org/wiki/Encode/H.264
        })

    for i in range(lenght):
        img = cv2.imread(path + '\\' + f'img{i}.jpg')
        frame = np.asarray(img)
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        writer.writeFrame(frame[:, :, ::-1])

    writer.close()
def create_video(path):
    writer = io.FFmpegWriter("output_video.avi",
                             outputdict={
                                 '-vcodec': 'libx264',
                                 '-crf': '0',
                                 '-preset': 'veryslow'
                             })

    cap = cv2.VideoCapture(path)
    func = np.vectorize(lambda x: round(x, 1))

    while True:
        ret, frame = cap.read()
        if not ret:
            break

        img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        img = cv2.resize(img, (480, 270))
        arr = np.asarray(img) / 255.0
        arr = func(arr)

        elements = set(tuple(list(arr.flatten())))

        range_colors = np.linspace(0, 255, len(elements))
        data = dict(zip(elements, range_colors))

        for i in range(270):
            for j in range(480):
                value = arr[i][j]
                img[i][j] = data[value]

        #img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
        cv2.imshow('frame', img)

        writer.writeFrame(img)

        if cv2.waitKey(1) == ord('q'):
            break

    cap.release()
    writer.close()
    cv2.destroyAllWindows()
Esempio n. 19
0
def draw_predictions_video(results,
                           video,
                           markersize=4,
                           cmap_name=DEFAULT_COLORMAP,
                           p_cutoff=PCUTOFF):
    """Draw pre-calculated joint locations into video frames.
    """
    video_path = Path(video)

    clip = VideoFileClip(str(video_path)).to_RGB()

    ny, nx, fps = clip.h, clip.w, clip.fps

    df = pd.read_hdf(results)
    scorer = 'reichler'

    joints = list(np.unique(df.columns.get_level_values(1)))

    cmap = plt.cm.get_cmap(cmap_name, len(joints))

    clip_out = io.FFmpegWriter(str(video_path.with_suffix('.labeled.mp4')),
                               inputdict={'-r': str(clip.fps)},
                               outputdict={'-r': str(clip.fps)})

    num_frames = math.ceil(clip.duration * clip.fps)

    for index, frame in enumerate(
            tqdm(clip.iter_frames(dtype='uint8'), total=num_frames)):
        for bpindex, bp in enumerate(joints):
            if df[scorer][bp]['likelihood'].values[index] > p_cutoff:
                xc = int(df[scorer][bp]['x'].values[index])
                yc = int(df[scorer][bp]['y'].values[index])
                # rr, cc = circle_perimeter(yc, xc, radius)
                # tqdm.write(str((xc, yc, bp)))
                rr, cc = draw.circle(yc, xc, markersize, shape=(ny, nx))
                frame[rr, cc, :] = [c * 255 for c in cmap(bpindex)[:3]]

        clip_out.writeFrame(frame)

    clip_out.close()
    clip.close()
Esempio n. 20
0
def main(out_path, fps, demo_paths):
    all_demos = load_demos(demo_paths)
    frame_segments = map(get_frames, all_demos)
    out_dir = os.path.dirname(out_path)
    if out_dir:
        print(f"Will make dir '{out_dir}' if it does not exist yet")
        os.makedirs(out_dir, exist_ok=True)
    print(f"Writing video to '{out_path}'")
    writer = vidio.FFmpegWriter(out_path,
                                outputdict={
                                    '-r': str(fps),
                                    '-vcodec': 'libx264',
                                    '-pix_fmt': 'yuv420p',
                                })
    nframes = 0
    for frame_segment in frame_segments:
        print(f"Writing frame segment ({nframes} frames written so far)")
        for frame in frame_segment:
            writer.writeFrame(frame)
            nframes += 1
    print(f"Done! ({nframes} frames written in total)")
Esempio n. 21
0
    def triggerRecord(self):
        if self.record.get() == 0:
            with self.record_lock:

                written_video_frames = 0
                pupil_video_writer = skv.FFmpegWriter(
                    'data_acquisition/pupil_video.avi',
                    outputdict={
                        '-vcodec': 'libx264',
                        '-b': '30000000',
                        '-vf': 'setpts=4*PTS'
                    })

                for frame in self.pupil_video_frames:
                    pupil_video_writer.writeFrame(frame)
                    written_video_frames += 1
                    print "wrote a frame!"

                print "finished writing!"
                pupil_video_writer.close()
                self.pupil_video_frames = []

                frame_number = 0
                pupil_data_file = open('data_acquisition/pupil_data.txt', 'w+')

                for frame_number in range(1, len(self.pupil_data_list) + 1):
                    pupil_center, pupil_radius = self.pupil_data_list[
                        frame_number - 1]
                    if pupil_center is None or pupil_radius is None:
                        pupil_data_file.write(
                            "Frame: %d No pupil detected!\n" % frame_number)
                    else:
                        pupil_data_file.write(
                            "Frame: %d Pupil Center: %s Pupil Radius: %d\n" %
                            (frame_number, pupil_center, pupil_radius))

                print "video frames vs data frames", written_video_frames, frame_number
                pupil_data_file.close()
                self.pupil_data_list = []
Esempio n. 22
0
def main():
    flags = parse_args()

    images = sorted(os.listdir(os.path.join(flags.data, 'depth')))
    frames = []
    for i, image in enumerate(images):
        print(f"Reading image {image}", end='\r')
        path = os.path.join(flags.data, 'depth', image)
        depth = np.load(path)
        max_depth = 7.5
        depth_m = depth / 1000.0
        depth_map = np.clip(1.0 - depth_m / max_depth, 0.0, 1.0)
        depth_map = cm.inferno(depth_map)

        frames.append((depth_map * 255).astype(np.uint8))

    writer = io.FFmpegWriter(os.path.join(flags.data, 'depth_video.mp4'))
    try:
        for i, frame in enumerate(frames):
            print(f"Writing frame {i:06}" + " " * 10, end='\r')
            writer.writeFrame(frame)
    finally:
        writer.close()
Esempio n. 23
0
def make_video_ffmpeg(mat,
                      save_path,
                      fps=60,
                      outputdict={'-vcodec': 'rawvideo'},
                      normalize=True,
                      vmin=None,
                      vmax=None,
                      clip=False,
                      clip_low=50,
                      clip_high=99,
                      gray=False):
    """Make a video from mat
    Args:
        mat: (num_frames, num_rows, num_cols)
    """
    from skvideo import io as skio
    if isinstance(mat, torch.Tensor):
        mat = mat.detach().cpu().numpy()
    if gray and mat.ndim > 3:
        mat = mat.reshape(-1, *mat.shape[-2:])
    if normalize:
        from matplotlib.colors import Normalize
        if clip:
            vmin = get_percentile(mat, clip_low)
            vmax = get_percentile(mat, clip_high)
        if vmin is None:
            vmin = mat.min()
        if vmax is None:
            vmax = mat.max()
        norm = Normalize(vmin=vmin, vmax=vmax, clip=clip)
        mat = 255 * norm(mat)
    mat = mat.astype(np.uint8)
    writer = skio.FFmpegWriter(save_path, {'-r': f'{fps}'},
                               outputdict=outputdict)
    for i in range(len(mat)):
        writer.writeFrame(mat[i])
    writer.close()
Esempio n. 24
0
	def parseVideo(self, vname, vtype, dst_path):
		"""parse Video and write as a mp4 file

	   	# arguments
	   		vname: input video name
	   		dst_path: destination directory path, not file name
		"""

		# Data format of cliped_Frames is
		# [num_frames, num_clips, 256, 256, 3]
		if vtype == "MVQP":
			height = 1080
			width = 2048
		elif vtype == "MCLV":
			height = 1080
			width = 1920
		elif vtype == "XIPH2":
			height = 1080
			width = 1920
		elif vtype == "MCML":
			height = 2160
			width = 3840

		cliped_Frames = []
		num_clips = 0
		print("***********: ", vname)


		##################################### Store the cliped video as numpy array in self.cliped_Frames
		with  open(vname, 'rb') as stream:
			m = mmap.mmap(stream.fileno(), 0, access=mmap.ACCESS_READ)

			for idx in range(self.num_frames):
				try:	# if number of video frame is less than self.num_frames, it stops reading video
					YUV =  np.zeros([height, width, 3], dtype=np.uint8)
					YUV[:, :, 0] = np.frombuffer(m, dtype=np.uint8, count=width * height, offset = int(idx * width * height * 1.5)).reshape(height, width)
					YUV[:, :, 1] = np.frombuffer(m, dtype=np.uint8, count=(width // 2) * (height // 2), offset = int(idx * width * height * 1.5 + width * height )).reshape(height // 2, width // 2).repeat(2, axis=0).repeat(2, axis=1)
					YUV[:, :, 2] = np.frombuffer(m, dtype=np.uint8, count=(width // 2) * (height // 2), offset = int(idx * width * height * 1.5 + width * height * 1.25 )).reshape(height // 2, width // 2).repeat(2, axis=0).repeat(2, axis=1)
					RGB = cv2.cvtColor(YUV, cv2.COLOR_YUV2RGB)
				except Exception as e:
					print(e)
					break


				# Divide large size frame into 256x256 size video
				num_clips, clips = self.clip_frame(RGB, height, width, flag=False)
				cliped_Frames.append(clips)



		###################################### Write the numpy array as mp4 file
		cliped_Frames = np.array(cliped_Frames)
		# bitrates = ["500k", "600k", "700k", "800k"]
		bitrates = ["27M"]

		
		for clip_idx in range(num_clips):
			clip = cliped_Frames[:, clip_idx]
		
			for bitrate in bitrates:
				write_option['-b:v'] = bitrate
				dir_path = join(dst_path, bitrate)
				try:
					makedirs(dir_path)
				except:
					pass
				output_fname = join(dir_path, "{}_{}.mp4".format(basename_without_extension(vname), str(clip_idx)))
				writer = vio.FFmpegWriter(filename=output_fname, inputdict={'-r': fps, '-pix_fmt': 'rgb24'}, outputdict=write_option)
		
				for frame in clip:
					writer.writeFrame(frame)

				writer.close()
Esempio n. 25
0
def main():

	# parse the arguments
	parser = argparse.ArgumentParser(description='Retouch video.')
	parser.add_argument('--src_path', type=str, default='./trainS_input', help='source path')
	parser.add_argument('--dst_path', type=str, default='./trainS_output', help='destination path')
	parser.add_argument('--intensity', type=str, default='strong', help='strong or weak')
	args = parser.parse_args()

	# source directory validation check
	src_path 	= args.src_path
	dst_path 	= args.dst_path
	intensity  	= args.intensity

	# method validation check
	bitrates = ["500k", "600k", "700k", "800k"]
	methods = ["blur", "median", "noise"]
	# methods = ["original"]

	# set destination directory name
	for method in methods:
		for bitrate in bitrates:
			try:
				makedirs(join(dst_path, "retouch_"+intensity, method, bitrate))
				pass
			except FileExistsError:
				pass

	counter = 1
	fnames = glob.glob(join(src_path, "*k", "*.mp4"))
	print("%8s| file name" % "counter")

	# retouch video
	for fname in fnames:
		# video read
		meta = vio.ffprobe(fname)
		vid = np.array(vio.vread(fname))
		vid_retouched = np.zeros(vid.shape)
		fn, w, h, c = vid.shape
		if w != 256 or h != 256 or c != 3: 
			print("================ wrong size file: \"{}\"".format(fname))
			continue

		# parse bitrate from file name
		bitrate = fname.split("\\")[-2]

		for method in methods:
			# get manipulated frame 
			for i in range(fn):
			    vid_retouched[i,:,:,:] = manipulate(vid[i,:,:,:], method, intensity=intensity) # manipulate.py 참고

			vid_retouched = vid_retouched.astype(np.uint8)

			# set output file name
			output_file = join(dst_path, "retouch_"+intensity, method, bitrate, basename(fname))
			print("%8d: %s" % (counter , output_file))
			counter += 1
			
			# load writer with parameter
			# "-vcodec = libx264" 	: h.264 codec
			# "-r = 30" 			: fps
			# "-g = 4"				: GOP size
			# "-bf = 0" 			: number of b frame
			# "-b:v = bitrate" 		: bitrate
			# "-pix_fmt = yuv420p"	: color space
			write_option = {'-vcodec': 'libx264', '-r': '30', '-g': '4', '-bf': '0', '-b:v': bitrate, '-pix_fmt': 'yuv420p'}
			writer = vio.FFmpegWriter(filename=output_file, inputdict={'-r': '30'}, outputdict=write_option)
			for i in range(fn):
				writer.writeFrame(vid_retouched[i, :, :, :])
			# writer.writeFrame(vid_retouched)
			writer.close()

	print("Process end on directory \"%s\"" % src_path)
Esempio n. 26
0
params_mean.interpolate('cubic')

fig, ax = plt.subplots(7,1)
for i, x in enumerate(['x','y','a','b','t', 'v', 'e']):
    ax[i].scatter(params_mean[x], s=0.5, alpha=0.2, c='k')

##########################

#fig, ax = plt.subplots(4,1)
thetas = np.linspace(0, np.pi*2, num=200, endpoint=False)

emod = measure.EllipseModel()

fn = '/home/lab/pupil_vids/nick3_track2.mp4'
#fourcc = cv2.VideoWriter_fourcc(*"X264")
writer = io.FFmpegWriter(fn)

ell_params = fitutils.clean_lists(x_list, y_list, a_list, b_list, t_list, v_list, n_list)
ell_params = basic_filter(ell_params, ix, iy, rad, e_thresh=0.6)
ell_out = fitutils.filter_outliers(df_fill, neighbors=1000)
ell_smooth = fitutils.smooth_estimates(ell_out, hl=5)

vid = cv2.VideoCapture(vid_fn)
#vid.set(cv2.CAP_PROP_POS_MSEC, 350000)
frame_counter = count()
cv2.namedWindow('run', flags=cv2.WINDOW_NORMAL)
for i in xrange(len(ell_smooth)):
    k = cv2.waitKey(1) & 0xFF
    if k == ord('\r'):
        break
Esempio n. 27
0
        generator = blur_rdn.BlurRDN(2, 3, 64, 64, 16, 8).to(DEVICE)
    elif net_type == "esrgen16":
        generator = newer_esrgan_generator.esrgan16(2, pretrained=False).to(DEVICE)
    elif net_type == "esrgen23":
        generator = newer_esrgan_generator.esrgan23(2, pretrained=False).to(DEVICE)

    d = torch.load(checkpoint_path)
    generator.load_state_dict(d["generator"])
    generator.eval()

    vcsan = cv2.VideoCapture(movie_path)
    suc, img = vcsan.read()

    out = vio.FFmpegWriter(save_name + ".mp4", outputdict={
        '-vcodec': 'libx264',
        '-crf': str(compression_level),
        '-preset': 'veryslow'
    })
    out_bic = vio.FFmpegWriter(save_name + "bic.mp4", outputdict={
        '-vcodec': 'libx264',
        '-crf': str(compression_level),
        '-preset': 'veryslow'
    })

    ind = 0
    progbar = pyprind.ProgBar(end_frame - start_frame + 1, title="Inference")
    bic = transforms.Resize((img.shape[0] * scale, img.shape[1] * scale), interpolation=PIL.Image.BICUBIC)
    while suc:
        if start_frame <= ind <= end_frame:
            with torch.no_grad():
                img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) / 255
Esempio n. 28
0
def main():

    # parse the arguments
    parser = argparse.ArgumentParser(description='Retouch video.')
    parser.add_argument('--src_path',
                        type=str,
                        default='../27M',
                        help='source path')
    parser.add_argument('--gt_path',
                        type=str,
                        default='../gt',
                        help='ground truth path')
    parser.add_argument('--dst_path',
                        type=str,
                        default='../retouch_spatial',
                        help='destination path')
    parser.add_argument('--intensity',
                        type=str,
                        default='extreme',
                        help='strong or weak')
    args = parser.parse_args()

    # source directory validation check
    src_path = args.src_path
    gt_path = args.gt_path
    dst_path = args.dst_path
    intensity = args.intensity

    # method validation check
    bitrate = "27M"
    methods = ["blur", "median", "noise"]
    # methods = ["original"]

    # set destination directory name
    for method in methods:
        try:
            makedirs(join(dst_path, intensity, method))
            pass
        except FileExistsError:
            pass

    counter = 1
    fnames = glob.glob(join(src_path, "*.mp4"))
    gt_names = glob.glob(join(gt_path, "*.png"))
    print("%8s| file name" % "counter")

    # retouch video
    for fname in fnames:
        gt = None
        for gt_name in gt_names:
            if basename(fname).split('.')[0] in gt_name:
                gt = im2double(cv2.imread(gt_name, cv2.IMREAD_COLOR))
        # video read
        meta = vio.ffprobe(fname)
        vid = np.array(vio.vread(fname))
        vid_retouched = np.zeros(vid.shape)
        fn, w, h, c = vid.shape

        # parse bitrate from file name

        for method in methods:
            # get manipulated frame
            for i in range(fn):
                retouched = manipulate(vid[i, :, :, :],
                                       method,
                                       intensity=intensity)  # manipulate.py 참고
                original = vid[i, :, :, :]
                retouched_spatial = gt * retouched + (1 - gt) * original
                vid_retouched[i, :, :, :] = retouched_spatial

            vid_retouched = vid_retouched.astype(np.uint8)

            # load writer with parameter
            # "-vcodec = libx264"   : h.264 codec
            # "-r = 30"             : fps
            # "-g = 4"              : GOP size
            # "-bf = 0"             : number of b frame
            # "-b:v = bitrate"      : bitrate
            # "-pix_fmt = yuv420p"  : color space
            output_file = join(dst_path, intensity, method, basename(fname))
            write_option = {
                '-vcodec': 'libx264',
                '-r': '30',
                '-g': '4',
                '-bf': '0',
                '-b:v': bitrate,
                '-pix_fmt': 'yuv420p'
            }
            writer = vio.FFmpegWriter(filename=output_file,
                                      inputdict={'-r': '30'},
                                      outputdict=write_option)
            for i in range(fn):
                writer.writeFrame(vid_retouched[i, :, :, :])
            # writer.writeFrame(vid_retouched)
            writer.close()

            # set output file name

            print("%8d: %s" % (counter, output_file))
            counter += 1

    print("Process end on directory \"%s\"" % src_path)
                dataset.data_std,
                dataset.dim_to_ignore,
            )

            restored = convert_to_3d(restored, dataset.kinematic_tree, False)

            vid_3D = create_video_3d(
                restored,
                [0, 0, 1],
                dataset,
                "test_3d",
                use_limits=True,
                use_posInd=True,
            )

            writer = vio.FFmpegWriter(
                path.join(save_path, f"kps_3d_world_unproc#{nr}.mp4"))

            for frame in vid_3D:
                writer.writeFrame(frame)

            writer.close()

            vid = np.stack(vid, axis=0)

            writer = vio.FFmpegWriter(
                path.join(save_path, f"test_video_kps_img_expmap#{nr}.mp4"))

            for frame in vid:
                writer.writeFrame(frame)

            writer.close()
Esempio n. 30
0
def inference(name: str,
              net: torch.nn.Module,
              device: torch.device,
              length: float = 0,
              start: float = 0,
              batch: int = 1,
              cut: bool = False,
              normalize: bool = False,
              crf: int = 17,
              tune: str = None) -> None:
    net.eval()
    norm = ds.get_normalization()
    trn = trf.get_predict_transform(*ds.predict_res)

    cap = cv2.VideoCapture(ds.SAVE_DIR + 'data/video/' + name + '.mp4')
    fps = cap.get(cv2.CAP_PROP_FPS)
    cap.set(cv2.CAP_PROP_POS_FRAMES, int(round(start * fps)))

    w, h = ds.predict_res
    w *= ds.scale
    h *= ds.scale
    if normalize:
        path = ds.SAVE_DIR + 'data/output/' + name + '_sr_n.mp4'
    else:
        path = ds.SAVE_DIR + 'data/output/' + name + '_sr.mp4'

    outputdict = {
        '-vcodec': 'libx264',
        '-crf': '%d' % crf,
        '-preset': 'veryslow',
        '-r': '%g' % fps
    }
    if tune is not None:
        outputdict['-tune'] = tune
    out = vio.FFmpegWriter(path,
                           inputdict={
                               '-r': '%g' % fps,
                           },
                           outputdict=outputdict)

    i = 0
    if length != 0:
        total = int(round(length * fps))
    else:
        total = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    iter_bar = pyprind.ProgBar(total,
                               title='Inference ' + name,
                               stream=sys.stdout)

    frame_list = []
    end = False

    with torch.no_grad():
        while True:
            ret, frame = cap.read()
            if not ret or (length != 0 and i >= length * fps):
                if not end:
                    end = True
                else:
                    break
            else:
                frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
                frame = norm(trn(image=frame)["image"]).to(device)
                frame_list.append(frame)
            if len(frame_list) == batch or (end and len(frame_list) > 0):
                frames = torch.stack(frame_list)
                if cut:
                    pieces = utils.cut_image(frames)
                    out_pieces = []
                    for piece in pieces:
                        out_pieces.append(net(piece))
                    output = utils.glue_image(out_pieces)
                else:
                    output = net(frames)
                for j in range(len(frame_list)):
                    if normalize:
                        out_frame = correct_colors(output[j, :, :, :],
                                                   frames[j, :, :, :])
                    else:
                        out_frame = output[j, :, :, :]
                    out.writeFrame(
                        cv2.cvtColor(convert_to_cv_8bit(out_frame),
                                     cv2.COLOR_RGB2BGR))
                frame_list.clear()
            i += 1
            iter_bar.update()
    cap.release()
    out.close()