示例#1
0
def test_output_video_duration(video, wav, frames_dir, output):
    with temp_dir(frames_dir):
        extract_to_pngs(video, frames_dir)
        ops.merge_frames(frames_dir, wav, output)
        assert abs(
            ops.get_video_duration(output) - ops.get_video_duration(video)) < 1
        os.remove(output)
示例#2
0
def test_merge_frames_with_existing_output(video, wav, frames_dir, output):
    with temp_dir(frames_dir):
        extract_to_pngs(video, frames_dir)
        Path(output).touch()
        assert os.path.exists(output)
        ops.merge_frames(frames_dir, wav, output)
        assert os.path.exists(output)
        os.remove(output)
示例#3
0
def test(input_file, model, output_file, data_dir, tempdir, threshold,
         workers):
    segment_dir = os.path.join(
        tempdir,
        os.path.splitext(os.path.basename(output_file))[0])
    os.makedirs(segment_dir, exist_ok=True)
    segment_id = os.path.basename(segment_dir)
    segment = Segment(os.path.dirname(segment_dir), segment_id, -1, -1, [])
    filename, ext = os.path.splitext(input_file)
    shutil.copyfile(input_file, os.path.join(segment_dir, segment_id + ext))

    outputs_dir = os.path.join(segment_dir, 'outputs')
    os.makedirs(outputs_dir, exist_ok=True)

    data_output_dir = os.path.join(segment_dir, 'localization_outputs')
    os.makedirs(data_output_dir, exist_ok=True)

    model = keras_models.load_model(model)
    ops.extract_all_frames(segment.raw, segment.frames_dir)

    color_heatmaps = generate_color_heatmaps()
    failed_frames = list()

    def thread_function(thread_id, start_frame, end_frame):
        localization_output = K.function([model.input],
                                         [model.get_layer('sigmoid').output])
        for fi in range(start_frame, end_frame):
            outframe = os.path.join(outputs_dir, '{}.png'.format(fi))
            if not os.path.exists(outframe):
                try:
                    frame = segment.load_frame(fi)
                    spectrogram = segment.load_spectrogram(fi)
                except:
                    failed_frames.append(fi)
                    continue

                frame = cv2.resize(frame, (224, 224))
                spectrogram = np.expand_dims(
                    cv2.resize(spectrogram, (200, 257)), -1)

                frame = np.expand_dims(frame / 255., 0)
                spectrogram = np.expand_dims(spectrogram, 0)
                outputs = localization_output([frame, spectrogram])

                output = outputs[0][0]
                np.savez_compressed(os.path.join(data_output_dir,
                                                 '{}.npz'.format(fi)),
                                    out=output)
                max_val = np.max(output)

                if max_val > 0.5:
                    print('[Thread {}]'.format(thread_id), 'Processing frame',
                          fi, 'of', end_frame, '({})'.format(max_val),
                          'correspond')
                else:
                    print('[Thread {}]'.format(thread_id), 'Processing frame',
                          fi, 'of', end_frame, '({})'.format(max_val))

                heatmap = np.zeros((224, 224, 4))

                for i in range(14):
                    for j in range(14):
                        # value = color_heatmaps[int(output[i, j] * 100)]
                        value = output[i, j]
                        value = 0 if value < threshold else value
                        ii = i * 16
                        jj = j * 16
                        heatmap[ii:ii + 16, jj:jj + 16, 0] = 0.5
                        heatmap[ii:ii + 16, jj:jj + 16, 1] = 1
                        heatmap[ii:ii + 16, jj:jj + 16, 2] = 1
                        heatmap[ii:ii + 16, jj:jj + 16, 3] = value

                heatmap = Image.fromarray((heatmap * 255).astype(np.uint8))
                frame = Image.fromarray((frame[0] * 255.).astype(np.uint8))

                frame.paste(heatmap, (0, 0), heatmap)
                frame.save(outframe)
                # frame.save(os.path.join(data_output_dir, '{}.png'.format(fi)))

    threads = list()

    for idx in range(workers):
        thread_size = math.ceil(len(segment) / workers)
        thread_start = idx * thread_size
        thread_args = idx, thread_start, thread_start + thread_size
        thread = threading.Thread(target=thread_function, args=thread_args)
        threads.append(thread)
        thread.start()

    for idx, thread in enumerate(threads):
        thread.join()

    if len(failed_frames) > 0:
        print('The following frames cannot be processed:', failed_frames)
    else:
        ops.merge_frames(outputs_dir, segment.wav, output_file)
        print('Output saved to', output_file)
示例#4
0
def test_merge_frames_with_non_existing_wav(video, non_existing_wav,
                                            frames_dir, output):
    with temp_dir(frames_dir):
        with pytest.raises(FileNotFoundError):
            extract_to_pngs(video, frames_dir)
            ops.merge_frames(frames_dir, non_existing_wav, output)
示例#5
0
def test_merge_frames_with_non_dir_frames_dir(video, wav, output):
    with pytest.raises(NotADirectoryError):
        ops.merge_frames(video, wav, output)
示例#6
0
def test_merge_frames_with_non_existing_frames_dir(wav,
                                                   non_existing_frames_dir,
                                                   output):
    with pytest.raises(FileNotFoundError):
        ops.merge_frames(non_existing_frames_dir, wav, output)