python 线程池


import argparse
import os
import imageio
import cv2
import numpy as np
import time
from concurrent.futures import ThreadPoolExecutor,wait,ALL_COMPLETED,FIRST_COMPLETED, as_completed


'''
python mask2rgbaThread.py --image_path /home/***/color/%04d.jpg \
    --mask_path /home/***/mask/%04d.png \
    --n 38 \
    --save_path /home/***/rgbaThread/%04d.png

'''

def parse_args():
	parser = argparse.ArgumentParser(description="Run neural graphics primitives testbed with additional configuration & output options")

	# parser.add_argument("--camera_intrinsics", default="data/camera-intrinsics.txt", help="camera-intrinsics")
	parser.add_argument("--n", default="10", type=int,help="Set render mode.")
	# parser.add_argument("--depth_path", default="data/frame-%06d.depth.png", help="depth_path")
	parser.add_argument("--image_path", default="data/frame-%06d.color.jpg", help="image_path")
	parser.add_argument("--mask_path", default="mydata/mask/%04d.png", help="image_path")
	parser.add_argument("--debug_path", default="mydata/mask/%04d.png", help="image_path")
	parser.add_argument("--save_path", default="data/frame-%06d.png", help="Load this snapshot before training. recommended extension: .msgpack")

	args = parser.parse_args()
	return args

def rgb2rgbd(imgPath, maskPath, savePath, index):
    mask_im1 = cv2.imread(maskPath%(index))
    _, mask_im = cv2.threshold(mask_im1, 1, 255, cv2.THRESH_BINARY)


    color_im = cv2.cvtColor(cv2.imread(imgPath%(index)), cv2.COLOR_RGB2BGR)

    img_result = np.zeros((color_im.shape[0], color_im.shape[1],4), dtype=np.uint8)
    img_result[:,:, 0:3] = cv2.bitwise_and(color_im, mask_im)
    img_result[:,:,3]=mask_im[:,:,0]
    imageio.imwrite(savePath%(index), img_result)
    # cv2.namedWindow("result",cv2.WINDOW_NORMAL)
    # cv2.imshow("result",img_result)
    # # cv2.waitKey(0)


if __name__ == "__main__":
    args = parse_args()
    n_imgs = args.n
    start_time = time.time()    # 程序开始时间

    savePath = args.save_path[0:args.save_path.rfind('/')]
    # print(savePath)
    if not os.path.exists(savePath):
        os.makedirs(savePath)
    
    # 创建一个最大容纳数量为2的线程池
    # max_workers=2
    with ThreadPoolExecutor() as pool:
        print("pool._max_workers:",pool._max_workers)

        all_task=[pool.submit(rgb2rgbd, args.image_path, args.mask_path,args.save_path, i) for i in range(args.n)]

        # 主线程等待所有子线程完成
        wait(all_task, return_when=ALL_COMPLETED)
        print("----complete-----")

    end_time = time.time()    # 程序结束时间
    run_time = end_time - start_time    # 程序的运行时间,单位为秒
    print("all time:", run_time, " s")


posted @ 2023-02-28 17:14  小小灰迪  阅读(25)  评论(0编辑  收藏  举报