Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slow video reading of imageio.v3 compared to opencv #1086

Open
zzhuolun opened this issue May 23, 2024 · 0 comments
Open

Slow video reading of imageio.v3 compared to opencv #1086

zzhuolun opened this issue May 23, 2024 · 0 comments

Comments

@zzhuolun
Copy link

zzhuolun commented May 23, 2024

Test the video reading speed

I benchmarked the reading speed of .MP4 video between Imageio.v3 and opencv::VideoCapture. This is the python code to compare the speed:

import cv2
import time
import imageio.v3 as iio
import argparse
from tqdm import tqdm


def cv2_time(video_path: str, frame_ids: list) -> float:
    # opencv
    video = cv2.VideoCapture(video_path)
    start_time = time.time()
    for frame_id in tqdm(frame_ids, desc='OpenCV seq'):
        video.read()
    return time.time() - start_time


def imiter_time(video_path: str, frame_ids: list) -> float:
    # imiter
    imiter = iio.imiter(video_path, plugin='pyav')
    start_time = time.time()
    for frame_id in tqdm(frame_ids, desc='imiter'):
        next(imiter)
    return time.time() - start_time


def imopen_read_time(video_path: str, frame_ids: list) -> float:
    # imopen read
    imopen = iio.imopen(video_path, 'r', plugin="pyav")
    start_time = time.time()
    for frame_id in tqdm(frame_ids, desc='imopen read'):
        imopen.read(index=frame_id)
    return time.time() - start_time


def imopen_iter_time(video_path: str, frame_ids: list) -> float:
    # imopen iter
    imopen = iio.imopen(video_path, 'r', plugin="pyav")
    iter = imopen.iter()
    start_time = time.time()
    for frame_id in tqdm(frame_ids, desc='imopen iter'):
        next(iter)
    return time.time() - start_time


def compare_time(video_path: str):
    frame_ids = range(500)
    secs_cv2 = cv2_time(video_path, frame_ids)
    secs_imiter = imiter_time(video_path, frame_ids)
    secs_imopen_read = imopen_read_time(video_path, frame_ids)
    secs_imopen_iter = imopen_iter_time(video_path, frame_ids)

    print(f'opencv time {secs_cv2:.3f}s')
    print(f'iio.iter time {secs_imiter:.3f}s')
    print(f'iio.open read time {secs_imopen_read:.3f}s')
    print(f'iio.open ter time {secs_imopen_iter:.3f}s')


if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('video', help='Path to video', type=str)
    opt = parser.parse_args()
    compare_time(opt.video)

Result

Testing the above script on a DJI MP4 video gives the following result:

OpenCV seq: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:04<00:00, 115.35it/s]
imiter: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:26<00:00, 18.53it/s]
imopen read: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:26<00:00, 18.66it/s]
imopen iter: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 500/500 [00:26<00:00, 18.76it/s]
opencv time 4.339s
iio.iter time 26.980s
iio.open read time 26.798s
iio.open ter time 26.648s

Imageio.v3 is significantly slower than opencv in video reading.

My environment:

opencv-python                  4.9.0.80
imageio                        2.34.1
imageio-ffmpeg                 0.4.9
av                             12.0.0
Python 3.10.14
OS Ubuntu 22.04.4 LTS

Related issue

I found a similar issue reported: #1050
However, the previous issue is on imageio.v2. The current one is about imageio.v3. Also, seems that the slow reads problem in v2 is also still not solved.

@zzhuolun zzhuolun changed the title Video reading speed significantly slower than opencv::VideoCapture Slow video reading of imageio.v3 compared to opencv May 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant