1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
""" 采用多进程加快处理。添加了在读取图片时捕获异常,OpenCV对大分辨率或者tif格式图片支持不好 处理数据集 和 标签数据集的代码:(主要是对原始数据集裁剪) 处理方式:分别处理 注意修改 输入 输出目录 和 生成的文件名 output_dir = "./label_temp" input_dir = "./label" """ import multiprocessing import cv2 import os import time
def get_img(input_dir): img_paths = [] for (path,dirname,filenames) in os.walk(input_dir): for filename in filenames: img_paths.append(path+'/'+filename) print("img_paths:",img_paths) return img_paths
def cut_img(img_paths,output_dir): imread_failed = [] try: img = cv2.imread(img_paths) height, weight = img.shape[:2] if (1.0 * height / weight) < 1.3: cropImg = img[30:150, 50:600] cv2.imwrite(output_dir + '/1' + img_paths.split('/')[-1], cropImg) else: cropImg_01 = img[30:150, 30:60] cv2.imwrite(output_dir + '/' + img_paths.split('/')[-1], cropImg_01) except Exception as r: print("error:",r) imread_failed.append(img_paths) return imread_failed
def main(input_dir,output_dir): img_paths = get_img(input_dir) scale = len(img_paths)
results = [] pool = multiprocessing.Pool(processes = 4) for i,img_path in enumerate(img_paths): a = "#"* int(i/10) b = "."*(int(scale/10)-int(i/10)) c = (i/scale)*100 results.append(pool.apply_async(cut_img, (img_path,output_dir ))) print('{:^3.3f}%[{}>>{}]'.format(c, a, b)) pool.close() pool.join() for result in results: print('image read failed!:', result.get()) print ("All done.")
if __name__ == "__main__": output_dir = "src/python-opencv/003-cut/dist_img" input_dir = "src/python-opencv/003-cut/dist_img" main(input_dir, output_dir)
|