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 68 69 70 71 72 73 74 75
| import cv2 import numpy as np import random from funcs import * import os import multiprocessing
times = 1 photos = os.listdir(r'photo') backs = os.listdir(r'back')
def data_maker(img, back, img_copy): img = random_brightness(img) back = random_brightness(back) img = random_blur(img) r = random.randint(5, 20) / 10 img = cv2.resize(img, (0, 0), fx=r, fy=r, interpolation=cv2.INTER_NEAREST) img_copy = cv2.resize(img_copy, (0, 0), fx=r, fy=r, interpolation=cv2.INTER_NEAREST) img, points = random_horizontally_affine(img) xc, yc, wc, hc = points_affine(img_copy, points) back, x, y = overlay(img, back) xmin, ymin = (x + xc, y + yc) xmax, ymax = (x + xc + wc, y + yc + hc) return back, xmin, ymin, xmax, ymax
def kind(datas, backs, times, i): m = 0 n = 0 for j in datas: for k in backs: for l in range(0, times): m += 1 n += 1 img = cv2.imread('photo\\' + i + '\\' + j) back = cv2.imread('back\\' + k) rbn = backs[random.randint(0, len(backs) - 1)] back[0:539, 960:1919] = cv2.imread('back\\' + rbn)[0:539, 960:1919] rbn = backs[random.randint(0, len(backs) - 1)] back[540:1079, 0:969] = cv2.imread('back\\' + rbn)[540:1079, 0:969] rbn = backs[random.randint(0, len(backs) - 1)] back[540:1079, 960:1919] = cv2.imread('back\\' + rbn)[540:1079, 960:1919] img_copy = cv2.imread('photo_copy\\' + i + '\\' + j) save, xmin, ymin, xmax, ymax = data_maker(img, back, img_copy) cv2.imwrite("F:\\out\\photo_output\\" + i + '\\' + str(m) + '.jpg', save) cv2.rectangle(save, (xmin, ymin), (xmax, ymax), (0, 255, 0), 2) cv2.imwrite("F:\\out\\photo_marked\\" + i + '\\' + str(m) + '.jpg', save) txt = xml_save(str(m) + '.jpg', str(xmin), str(ymin), str(xmax), str(ymax)) path = 'F:\\out\\outputs\\' + i + '\\' + str(m) + '.xml' fw = open(path, 'w') fw.write(txt) fw.close() xx = xmax - xmin yy = ymax - ymin if not 0.2 < xx / yy < 2: m -= 1 print(i + '进度:' + str(n) + '/' + str(len(datas) * len(backs) * times))
if __name__ == "__main__": pool = multiprocessing.Pool(processes=16) for i in photos: datas = os.listdir('photo\\' + i) pool.apply_async(kind, (datas, backs, times, i)) pool.close() pool.join() print("Sub-process(es) done.")
|