python - 使用CoLab在Google云端硬盘上OpenCV读取和写入速度太慢

标签 python image opencv google-colaboratory drive

我正在使用Google CoLab并运行一小段代码,该代码可重复处理170K-190K张图像,使用cv2.imread函数从类似 gdrive / My Drive / folder1 / img.jpg 的路径读取它们,然后使用cv2.resize函数调整它们的大小,然后使用cv2.imwrite将它们写在 gdrive / My Drive / folder2 / img.jpg 之类的路径中。
但这太慢了。我曾经在Linux Ubuntu LTS18.04 OS上使用Jupyter Notebook在设备上运行,并且迭代速度很快。现在,每次迭代需要将近30秒,这将估计代码将花费65天左右!
是什么原因导致CoLab上的速度如此之慢,我如何使其更快?
提前致谢。

最佳答案

这不是OpenCV的问题,而是Google云端硬盘的问题。即使您在Google Colaboratory中执行操作,从Google云端硬盘访问文件的速度也非常慢。

One better way is to copy the files into your Colab storage, then you will get faster performance which will be based on runtime (CPU/GPU/TPU) you are using.


要复制文件,可以使用shutil库(Shutil documentation):
import shutil

# To copy files
shutil.copyfile('source_file_location', 'destination_file_location')

# To copy multiple files of folder with some conditions
import os
for file in os.listdir('folder_path'):
    if file.endswith('.jpg'):
        shutil.copyfile('folder_path' + file, 'desination_folder_path' + file)

# To copy folders
shutil.copytree('source_path', 'destination_path')  
由于您在Google云端硬盘上的一个文件夹中有很多文件夹,因此有时可能无法访问所有文件。最好将文件批量保存在多个文件夹中(这将消除在通过Colab使用Google云端硬盘时出现的一些错误)。 See more about this one

关于python - 使用CoLab在Google云端硬盘上OpenCV读取和写入速度太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62894074/

相关文章:

python - Clipboard.Flush() 在 wxpython 应用程序中总是失败

python - PyInstaller 生成的 exe 不工作,项目使用 ReportLab

python - 运行 Docker 镜像时出错。显示没有名为 cv2、request、boto3 的模块

Android Jetpack Compose 闪烁图像克隆

arrays - 快速创建图像数组

python - 用于 Excel 文件的 DictReader

python - 是否可以从直方图中找到图像的平均颜色?

matlab - 使用 Roll-Pitch-Yaw 角度变换图像(图像校正)

c++ - 用opencv python填充轮廓

使用相机闪光灯获取摩尔斯电码的 Android 应用程序