[Comparison of the speed of reading pictures between opencv and PIL]

0. Application scenarios

In the Pytorch code, when we build the Dataset class, we need to read data in the ' getitem ' method, and during the model training and prediction process, data reading will take part of the time. What's more, there are tens of thousands of images in the data set, so the speed of reading will affect the efficiency of model training and prediction.

There are two common image data reading methods in the code: opencv and PIL image reading. The data read by opencv is directly of np.ndarray type, while the data read by PIL is the class of Image, which requires np.asarray() conversion.

Let's detect the difference in reading speed between the two:

1. Code detection

Read jpg and png images of the same size (500x442) 100 times respectively, as follows:
insert image description here
insert image description here

import cv2
from PIL import Image
import os
import time
import numpy as np

if __name__ == '__main__':
    # 比较PIL和opencv读取jpg图片到numpy.ndarray的速度
    jpg_dir = os.path.join('..', 'data', '2008_000008.jpg')
    png_dir = os.path.join('..', 'data', '2008_000008.png')

    t_jpg_cv = time.time()
    for i in range(100):
        img = cv2.imread(jpg_dir)  # 默认np.dtype=uint8
    print(f"opencv读取jpg图片100次用时:{
      
      time.time()-t_jpg_cv:.5f}s")

    t_jpg_pil = time.time()
    for i in range(100):
        img = np.asarray(Image.open(jpg_dir), dtype=np.uint8)  # Image读取的图片不是np.ndarray,需要转换
    print(f"PIL读取jpg图片100次用时:{
      
      time.time() - t_jpg_pil:.5f}s")

    t_png_cv = time.time()
    for i in range(100):
        img = cv2.imread(png_dir)
    print(f"opencv读取png图片100次用时:{
      
      time.time() - t_png_cv:.5f}s")

    t_png_pil = time.time()
    for i in range(100):
        img = np.asarray(Image.open(png_dir))  # Image读取的图片不是np.ndarray,需要转换
    print(f"PIL读取png图片100次用时:{
      
      time.time() - t_png_pil:.5f}s")

Results:
insert image description here
Therefore, the conclusion is that PIL is more efficient than opencv in terms of speed for reading images (whether in jpg or png format) into memory with np.ndarray(). So I will use PIL to read pictures in the future.

In addition, I saw in some blogs that some people said that PIL reading is not as efficient as opencv, but my experiments have proved that this does not seem to be the case. But it may also be that I have not thought carefully in some places, and I hope to enlighten me.

Guess you like

Origin blog.csdn.net/qq_44166630/article/details/127153615