使用 python 读取图像数据,提高数据预处理速度

import cv2
import time
import glob

'''
首先需要从处理内容的文件列表开始
使用 for 循环逐个处理每个数据,然后在每个循环迭代上运行预处理
'''
# loop through all jpg file in the current folder
# resize each one to size 600*600
start_time = time.time()
for image_filename in glob.glob('./Airport/*.jpg'):
    img = cv2.imread(image_filename)

    # resize the image
    img = cv2.resize(img, (600, 600))
print('time:', time.time() - start_time)
# 2.667  360 jpgs

'''
将 jpg 文件列表分成4个小组
运行 python 解释器中的 4 个独立的实例
让 python 的每个实例处理 4 个数据小组中一个
结合 4 个处理过程得到的结构得出最终那那个结果列表
'''
import concurrent.futures

start_time1 = time.time()


def load_and_resize(image_filename):
    img = cv2.imread(image_filename)
    img = cv2.resize(img, (600, 600))


# create a pool of processes. By default,one is created for each cpu in your machine
with concurrent.futures.ProcessPoolExecutor() as executor:
    # get a list of files to process
    image_files = glob.glob('*.jpg')

    # executor.map() 将你想要运行的函数和列表作为输入,列表中的每个元素都是我们函数的单个输入,由于我们有6个核,我们将同时处理该列表中的6个项目
    executor.map(load_and_resize, image_files)

print('acceleration time:', time.time() - start_time1)
# 0.139

使用数据集一共 360 张图片,采用第一种方法,for 循环大概 2.667s,第二种方法,利用多核并行

with concurrent.futures.ProcessPoolExecutor() as excutor
    executor.map()     

大概是 0.139s,比第一种方法快一倍。

  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值