Exemplo n.º 1
0
def maskVideo_rect(videoPath, outputFolder, reader='ImageIO'):
    """Function containing a pipeline which masks a desired ROI in a given video (provided by the maskROIs.csv file)
    and saves the result"""

    # get the maskCoords from the csv file
    df = pd.read_csv(os.path.join(os.path.split(videoPath)[0], "maskROIs.csv"),
                     index_col=0)
    maskCoords = tuple(df.loc[os.path.split(videoPath)[-1]])

    # reader
    if reader == 'ImageIO':
        video = pims.ImageIOReader(videoPath)
    elif reader == 'PyAV':
        video = pims.Video(videoPath)

    fps = video.frame_rate

    # pipeline
    r = maskCoords
    masking_pipeline = pims.pipeline(maskFrame_rect)
    kwargs = dict(maskCoords=r)
    processed_video = masking_pipeline(video, **kwargs)

    # writing to disk
    try:
        os.mkdir(outputFolder)
    except OSError:
        print(
            outputFolder,
            "already exists! Continuing with the process without creating it.")

    outputFilename = os.path.join(
        outputFolder,
        os.path.split(videoPath)[-1].strip(".mp4") + "_masked.mp4")
    imageio.mimwrite(outputFilename, processed_video, fps=fps)
Exemplo n.º 2
0
def processVideo(videoPath, outputFolder, shape='circle'):
    """
    Pipeline containing function to trim, mask and crop a video (provided by the process.csv file)
    shape: 'circle' or 'polygon'
    """

    print('processVideo called with: ', videoPath, outputFolder)
    outputFilename = os.path.join(
        outputFolder,
        os.path.split(videoPath)[-1].strip(".mp4") + "_processed.mp4")
    if os.path.exists(outputFilename):
        print(outputFilename, "already exists. Terminating!")
        return None

    # get the required data from the process.csv file
    df = pd.read_csv(os.path.join(os.path.split(videoPath)[0], "process.csv"),
                     index_col=0)
    start = df.loc[os.path.split(videoPath)[-1], 'start']
    end = df.loc[os.path.split(videoPath)[-1], 'end']

    # Shorten processed videos to 1h
    if end - start > 3600:
        end = 3600 + start

    if shape == 'circle':
        circle = literal_eval(df.loc[os.path.split(videoPath)[-1], 'circle'])
    # elif shape == 'polygon':
    #     vertices = np.array(literal_eval(df.loc[os.path.split(videoPath)[-1], 'vertices']))
    crop = literal_eval(df.loc[os.path.split(videoPath)[-1], 'crop'])

    video = pims.ImageIOReader(videoPath)
    fps = video.frame_rate

    # pipelines
    # masking pipeline
    masking_pipeline = pims.pipeline(maskFrame_circle)
    masking_kwargs = dict(circle=circle)
    masked_video = masking_pipeline(video, **masking_kwargs)
    # cropping pipeline
    cropping_pipeline = pims.pipeline(crop_frame)
    cropping_kwargs = dict(crop=crop)
    cropped_video = cropping_pipeline(masked_video, **cropping_kwargs)

    # writing to disk
    try:
        os.mkdir(outputFolder)
    except OSError:
        print(
            outputFolder,
            "already exists! Continuing with the process without creating it.")

    start_frame = int(start * fps)
    end_frame = int(end * fps)
    imageio.mimwrite(outputFilename,
                     cropped_video[start_frame:end_frame],
                     fps=fps)
    print('Completed: processVideo with parameters: ', videoPath, outputFolder)
Exemplo n.º 3
0
def process_chunk(filename,
                  start,
                  stop,
                  reference,
                  save_name,
                  ds_factor=4,
                  correct_motion=True,
                  thresh=1.8,
                  cutoff=0.05,
                  clean_pixels=False,
                  pixel_thresh=1.1,
                  format='tiff'):
    '''
    Process one chunk of a video read in from pims and save as .tiff

    Input:
        - filename: video path
        - start: start frame
        - stop: stop frame
        - reference: reference frame
        - ds_factor: int, downsample factor, default=4
        - correct_motion: bool, correct motion, default=True
        - thresh: flt, threshold for motion correction, default=1.0
        - cutoff: flt, cutoff for motion correction, default=0.05
    Output:
        - None, saves processed chunk as .tiff or .avi
    '''
    chunk = stop / (stop - start)
    video = pims.ImageIOReader(filename)
    frame_rate = video.frame_rate
    video_chunk = video[start:stop]
    print("Processing frames {} to {} of {}".format(start, stop, len(video)))

    video_chunk_ds = downsample(video_chunk, ds_factor)
    #in order to have 01, 02 for file sorting and concatenation of chunks
    if chunk < 10:
        chunk = '0' + str(chunk)

    if clean_pixels:
        remove_dead_pixels(video_chunk_ds, pixel_thresh)

    if correct_motion:
        video_chunk_ds = align_video(video_chunk_ds, reference, thresh, cutoff)

    if format == 'tiff':
        skimage.io.imsave(save_name + '_temp_{}.tiff'.format(chunk),
                          img_as_uint(video_chunk_ds / 2**16))
    elif format == 'avi':
        save_to_avi(video_chunk_ds,
                    fps=frame_rate / ds_factor,
                    filename=save_name + '_temp_{}.avi'.format(chunk))
Exemplo n.º 4
0
def trimVideo(videoPath, outputFolder, use_imageio=True):

    print('trimVideo called with: ', videoPath, outputFolder)
    outputFilename = os.path.join(
        outputFolder,
        os.path.split(videoPath)[-1].strip(".mp4") + "_trimmed.mp4")
    if os.path.exists(outputFilename):
        print(outputFilename, "already exists. Terminating!")
        return None

    # get the required data from the process.csv file
    df = pd.read_csv(os.path.join(os.path.split(videoPath)[0], "trims.csv"),
                     index_col=0)
    start = df.loc[os.path.split(videoPath)[-1], 'start']
    end = df.loc[os.path.split(videoPath)[-1], 'end']

    # Shorten processed videos to 1h
    if end - start > 3600:
        end = 3600 + start

    if use_imageio:
        video = pims.ImageIOReader(videoPath)
    else:
        video = pims.Video(videoPath)
    fps = video.frame_rate

    # writing to disk
    try:
        os.mkdir(outputFolder)
    except OSError:
        print(
            outputFolder,
            "already exists! Continuing with the process without creating it.")

    start_frame = int(start * fps)
    end_frame = int(end * fps)
    imageio.mimwrite(outputFilename, video[start_frame:end_frame], fps=fps)
    print('Completed: processVideo with parameters: ', videoPath, outputFolder)
Exemplo n.º 5
0
if __name__ == '__main__':
    args = get_args()
    if args.merge:
        assert args.output != None, "Please provide an output filename if using --merge"
        directory = path.dirname(args.input[0])
        args.output = directory + '/' + args.output
        files = ' + '.join(args.input)

        print(files)
        system("mkvmerge -o {} {}".format(args.output, files))
        filename = args.output

        print("Processing {}".format(filename))
        directory = path.dirname(filename)
        vid = pims.ImageIOReader(filename)
        reference = np.round(np.mean(np.array(vid[args.target_frame:args.downsample])[:,:,:,0], axis=0))
        save_name = filename.replace('.mkv', '_proc')

        if len(vid) % args.chunk_size < 10:
            args.chunk_size += 5

        starts = np.arange(0,len(vid),args.chunk_size)
        stops = starts+args.chunk_size
        frames = list(zip(starts, stops))

        Parallel(n_jobs=args.cores)(delayed(process_chunk)(filename=filename, start=start, stop=stop, reference=reference, save_name=save_name, format=args.format, ds_factor=args.downsample, correct_motion=args.correct_motion, thresh=args.threshold) for start, stop in frames)
        if args.format == 'tiff':
            if args.bigtiff:
                system("tiffcp -8 {}/*_temp_* {}.tiff".format(directory, save_name))
            else: