在做深度学习的时候,经常要处理各种数据,多线程可以加速处理图像
import os
from PIL import Image
import numpy as np
import threadpool
names = os.listdir("./STDC/before/")
images = [img for img in names if img[-3] == "j"]
def ThreadFun_c1(images):
# 读取原文件夹
if images.endswith("jpg"):
path = os.path.join("./STDC/before", images)
img = Image.open(path)
img.save(os.path.join("./STDC/jpg", images).replace(".jpg", ".png"))
# 找到对应的png
path = "./STDC/output/" + images.split(".")[0] + "_json/label.png"
img = Image.open(path)
# 找到全局的类
class_txt = open("./STDC/before/class_name.txt", "r")
class_name = class_txt.read().splitlines()
# ["_background_","a","b"]
# 打开json文件里面存在的类,称其为局部类
with open("./STDC/output/" + images.split(".")[0] + "_json/label_names.txt", "r") as f:
names = f.read().splitlines()
# ["_background_","b"]
new = Image.new("RGB", [np.shape(img)[1], np.shape(img)[0]])
# print('new:',new)
for name in names:
index_json = names.index(name)
index_all = class_name.index(name)
# 将局部类转换成为全局类
new = new + np.expand_dims(index_all * (np.array(img) == index_json), -1)
new = Image.fromarray(np.uint8(new))
print('new:', new)
new.save(os.path.join("./STDC/png", images.replace("jpg", "png")))
print(np.max(new), np.min(new))
# 定义了一个线程池,最多创建8个线程
pool = threadpool.ThreadPool(8)
# 创建要开启多线程的函数,以及函数相关参数和回调函数,其中回调数可以不写,default是none
requests = threadpool.makeRequests(ThreadFun_c1, images)
# 将所有要运行多线程的请求扔进线程池
[pool.putRequest(req) for req in requests]
# 所有的线程完成工作后退出
pool.wait()