骚操作之,opencv读取图像太慢了,图像的加载与解码都直接用cuda
一、安装
conda activate my_base # 先CD到自己的anconda下
git clone https://github.com/itsliupeng/torchnvjpeg.git
cd torchnvjpeg/
python3 setup.py bdist_wheel # 编译生成whl文件
cd dist
python -m pip install torchnvjpeg-0.1.0-cp37-cp37m-win_amd64.whl
二、使用
注意,要先import torch
否则会报
ImportError: DLL load failed: 找不到指定的模块。
或者
ImportError: libc10.so: cannot open shared object file: No such file or directory
通过先import torch,就可以找到这个文件了
import glob, os
import time
import torch
import torchnvjpeg
import cv2
if __name__ == "__main__":
path = "./images/"
img_paths = glob.glob(os.path.join(path, "*.jpg"))
img_size = len(img_paths)
decoder = torchnvjpeg.Decoder()
a = 0
for img_path in img_paths:
start = time.time()
# img = cv2.imread(img_path)
image_data = open(img_path, 'rb').read()
image_tensor = decoder.decode(image_data)
np = image_tensor.data.cpu().numpy()
end = time.time()
a += end - start
print("cost time = {}".format(a / img_size))