Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/DOTA_devkit/SplitOnlyImage_multi_process.py
Views: 475
import os1import numpy as np2import cv23import copy4import dota_utils as util5from multiprocessing import Pool6from functools import partial789def split_single_warp(name, split_base, rate, extent):10split_base.SplitSingle(name, rate, extent)111213class splitbase():14def __init__(self,15srcpath,16dstpath,17gap=100,18subsize=1024,19ext='.png',20padding=True,21num_process=32):22self.srcpath = srcpath23self.outpath = dstpath24self.gap = gap25self.subsize = subsize26self.slide = self.subsize - self.gap27self.srcpath = srcpath28self.dstpath = dstpath29self.ext = ext30self.padding = padding31self.pool = Pool(num_process)3233if not os.path.isdir(self.outpath):34os.mkdir(self.outpath)3536def saveimagepatches(self, img, subimgname, left, up, ext='.png'):37subimg = copy.deepcopy(38img[up: (up + self.subsize), left: (left + self.subsize)])39outdir = os.path.join(self.dstpath, subimgname + ext)40h, w, c = np.shape(subimg)41if (self.padding):42outimg = np.zeros((self.subsize, self.subsize, 3))43outimg[0:h, 0:w, :] = subimg44cv2.imwrite(outdir, outimg)45else:46cv2.imwrite(outdir, subimg)4748def SplitSingle(self, name, rate, extent):49img = cv2.imread(os.path.join(self.srcpath, name + extent))50assert np.shape(img) != ()5152if (rate != 1):53resizeimg = cv2.resize(54img, None, fx=rate, fy=rate, interpolation=cv2.INTER_CUBIC)55else:56resizeimg = img57outbasename = name + '__' + str(rate) + '__'5859weight = np.shape(resizeimg)[1]60height = np.shape(resizeimg)[0]6162# if (max(weight, height) < self.subsize/2):63# return6465left, up = 0, 066while (left < weight):67if (left + self.subsize >= weight):68left = max(weight - self.subsize, 0)69up = 070while (up < height):71if (up + self.subsize >= height):72up = max(height - self.subsize, 0)73subimgname = outbasename + str(left) + '___' + str(up)74self.saveimagepatches(resizeimg, subimgname, left, up)75if (up + self.subsize >= height):76break77else:78up = up + self.slide79if (left + self.subsize >= weight):80break81else:82left = left + self.slide8384def splitdata(self, rate):8586imagelist = util.GetFileFromThisRootDir(self.srcpath)87imagenames = [util.custombasename(x) for x in imagelist if (88util.custombasename(x) != 'Thumbs')]8990# worker = partial(self.SplitSingle, rate=rate, extent=self.ext)91worker = partial(split_single_warp, split_base=self,92rate=rate, extent=self.ext)93self.pool.map(worker, imagenames)94#95# for name in imagenames:96# self.SplitSingle(name, rate, self.ext)9798def __getstate__(self):99self_dict = self.__dict__.copy()100del self_dict['pool']101return self_dict102103def __setstate__(self, state):104self.__dict__.update(state)105106107if __name__ == '__main__':108split = splitbase(r'/media/test/4d846cae-2315-4928-8d1b-ca6d3a61a3c6/DOTA/DOTAv1.5/test/images',109r'/media/test/4d846cae-2315-4928-8d1b-ca6d3a61a3c6/DOTA/DOTAv2.0/test-dev_split/images',110gap=200, subsize=1024, num_process=8)111split.splitdata(1)112# split.splitdata(0.5)113# split.splitdata(1.5)114print("Split Done!")115116117118