def initForeground(self, numid): # 用于初始化前景对象 board_size = self.opts['board_size'] froe_minpos = self.opts['foreground_minIniPos'] froe_maxpos = self.opts['foreground_maxIniPos'] foreground_iniPos = self.opts['foreground_iniPosMethod'] assert foreground_iniPos in ['auto', 'define'] foreground_iniPosStandard = self.opts['foreground_iniPosStandard'] assert foreground_iniPosStandard in ['central', 'topleft'] foreground_size = self.opts['foreground_size'] # 读入前景 img, imgmask = self.foregroundGenerator.RandomGet() froesize = np.array(img.size) # (x,y) if (froesize > foreground_size).any(): b = np.max(froesize / foreground_size) b = 1 if b < 1 else b # 以防万一 froesize = np.array(froesize / (1 + (b - 1) * np.random.uniform(0, 1)), dtype=np.int) img = img.resize(froesize) imgmask = imgmask.resize(froesize) im = np.array(img) immask = np.array(imgmask) > 0 if foreground_iniPos == 'auto': pos = func2.RandomPoint((0, 0), board_size) elif foreground_iniPos == 'define': pos = func2.RandomPoint(froe_minpos, froe_maxpos) if foreground_iniPosStandard == 'central': # print(pos) pos = pos - froesize / 2 elif foreground_iniPosStandard == 'topleft': pass froe_rect = Rect(pos, froesize) # print(froe_rect) froe_data = RectArray(froe_rect, 3) froe_data.SetRectData(im) froe_datamask = RectArray(froe_rect, 1, dtype=np.bool) froe_datamask.SetValue(immask) obj_froe = Obj(froe_data, froe_datamask) self.objDict[numid] = obj_froe self.transOptsDict[numid] = TransOptsManager() # 私人变换数据 self.transOptsDict_customize[numid] = False
# 初始化 NameManager2 num = 20 name_opts = GetNameOpts() name_opts['target'] = '../data/ds_v2/TEST4_2_1' name_opts['sdir'] = ['show', 'show', 'show', 'show', 'show'] name_opts['suffix'] = ['A', 'B', 'gtAB_viz', 'C', 'gtBC_viz'] name_opts['ext'] = ['jpg', 'jpg', 'jpg', 'jpg', 'jpg'] nm = NameManager2(num, name_opts) for i, name in enumerate(nm): # 背景 pos0 = Point(0, 0) size0 = (640, 480) back_rect = Rect(pos0, size0) back_data = RectArray(back_rect, 3) back_data.SetRectData(back) back_datamask = RectArray(back_rect, 1, dtype=np.bool) back_datamask.SetValue(True) obj_back = Obj(back_data, back_datamask) trans_back = Trans(obj_back) trans_back.QuickTrans('M') ##################################### # 1111111111111111111111111111111 # 随机obj1的初始位置及大小 pos1 = func2.RandomPoint([50, 50], [300, 300]) size1 = im.shape[0:2] # 初始化obj1 obj1_rect = Rect(pos1, size1) obj1_data = RectArray(obj1_rect, 3) obj1_data.SetRectData(im)
im = Image.fromarray(imArray) im.show() img = Image.open('../data/ds_v1/timg.jpg') img = img.resize((256, 256)) im = np.array(img) pos = func.RandomPoint([0, 0], [200, 200]) # size = func.RandomSize([10, 10], [50, 50]) #pos = Point(100,100) size = im.shape[0:2] obj1_rect = Rect(pos, size) obj1_data = RectArray(obj1_rect, 3) obj1_data.SetRectData(im) immask = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) < 240 #Image.fromarray(np.uint8(immask*255)).show() obj1_datamask = RectArray(obj1_rect, 1, dtype=np.bool) obj1_datamask.SetValue(immask) # #obj1_mask_rect = Rect(Point(75, 75), size2) #obj1_true_datamask = RectArray(obj1_mask_rect, 1, dtype=np.bool) #obj1_true_datamask.SetValue(False) # #obj1_datamask.AddRectArray(obj1_true_datamask, CSYS='local') # # obj1 = Obj(obj1_data, obj1_datamask)
'png', 'flo', 'jpg'] nm = NameManager2(iter_num, name_opts) for i, name_dict in enumerate(nm): name = list(zip(*name_dict))[1] # =============== 初始化区域 ================== # 背景 # 初始化位置,大小 pos0 = func2.RandomPoint(background_minIniPos, background_maxIniPos) back_rect = Rect(pos0, background_size) back_data = RectArray(back_rect, 3) # 设置背景对象 backgroud = backgroundGenerator.RandomGet() backgroud = backgroud.resize(background_size) back_data.SetRectData(np.array(backgroud)) back_datamask = RectArray(back_rect, 1, dtype=np.bool) back_datamask.SetValue(True) obj_back = Obj(back_data, back_datamask) # 生成背景变换 trans_back = Trans(obj_back) trans_back_opts = GetTransOpts() trans_back.QuickTrans(['M'], [trans_back_opts]) # 创建列表储存初始化的obj obj_list = [trans_back.obj_imB] # 初始化obj列表 # initboard 用于保存第一帧 initboard = Board(board_size) initboard.addTrans(trans_back) # 初始化循环生成 obj for _ in range(obj_num): # 随机obj的初始位置及大小
def initBackground(self, numid=0): # 用于初始化背景对象 board_size = self.opts['board_size'] background_iniMethod = self.opts['background_iniMethod'] assert background_iniMethod in [ 'slice_central', 'slice_random', 'zoom', 'define' ] # 设置背景的图片 及 图片的位置和大小 while (True): background = self.backgroundGenerator.RandomGet() # 读取背景图 assert background is not None if background_iniMethod == 'define': # <define 通过直接缩放后确定左上角位置,不进行切片,有待改进> back_minpos = self.opts['background_minIniPos'] back_maxpos = self.opts['background_maxIniPos'] back_size = self.opts['background_size'] pos0 = func2.RandomPoint(back_minpos, back_maxpos) background = background.resize(back_size) break if background_iniMethod == 'slice_central': # 检查尺寸,进行缩放 qx, raw_size, tag_size = self.initBackground_check(background) if qx == 'f': continue elif qx == 's': background = Zoom(background, raw_size, tag_size) new_size = np.array(background.size) if not (new_size >= tag_size).all(): continue # 以防万一 cen_pos = new_size - tag_size box = (cen_pos[0], cen_pos[1], cen_pos[0] + tag_size[0], cen_pos[1] + tag_size[1]) background = background.crop(box) back_size = tag_size pos0 = Point((np.array(board_size) - tag_size) / 2) break elif background_iniMethod == 'slice_random': # 几乎和slice_central一模一样 qx, raw_size, tag_size = self.initBackground_check(background) if qx == 'f': continue elif qx == 's': background = Zoom(background, raw_size, tag_size) new_size = np.array(background.size) if not (new_size >= tag_size).all(): continue # 以防万一 ran_pos = func.RandomSize((0, 0), new_size - tag_size) box = (ran_pos[0], ran_pos[1], ran_pos[0] + tag_size[0], ran_pos[1] + tag_size[1]) background = background.crop(box) back_size = tag_size pos0 = Point((np.array(board_size) - tag_size) / 2) break elif background_iniMethod == 'zoom': qx, raw_size, tag_size = \ self.initBackground_check(background, 2) if qx == 'f': continue # 几乎和slice_central一模一样,区别在于必定缩放 background = Zoom(background, raw_size, tag_size) new_size = np.array(background.size) if not (new_size >= tag_size).all(): continue # 以防万一 box = (ran_pos[0], ran_pos[1], ran_pos[0] + tag_size[0], ran_pos[1] + tag_size[1]) box = (ran_pos[0], ran_pos[1], tag_size[0], tag_size[1]) background = background.crop(box) back_size = tag_size pos0 = Point((np.array(board_size) - tag_size) / 2) break # 创建背景 Rect 和 RectArray back_rect = Rect(pos0, back_size) back_data = RectArray(back_rect, 3) # 创建背景obj back_data.SetRectData(np.array(background)) back_datamask = RectArray(back_rect, 1, dtype=np.bool) back_datamask.SetValue(True) obj_back = Obj(back_data, back_datamask) self.objDict[numid] = obj_back self.transOptsDict[numid] = TransOptsManager() # 私人变换数据 self.transOptsDict_customize[numid] = False
num = 20 name_opts = GetNameOpts() name_opts['target'] = '../data/ds_v2/TEST4' name_opts['sdir'] = ['show', 'show', 'show', 'show', 'show'] name_opts['suffix'] = ['A', 'B', 'gtAB_viz', 'C', 'gtBC_viz'] name_opts['ext'] = ['jpg', 'jpg', 'jpg', 'jpg', 'jpg'] nm = NameManager2(num, name_opts) for i, name in enumerate(nm): # 随机obj1的初始位置及大小 pos1 = func.RandomPoint([50, 50], [300, 300]) size1 = im.shape[0:2] #!!!!!!!!!!!!!!!!这句的xy和ij反了 # 初始化obj1 obj1_rect = Rect(pos1, size1) obj1_data = RectArray(obj1_rect, 3) obj1_data.SetRectData(im) obj1_datamask = RectArray(obj1_rect, 1, dtype=np.bool) obj1_datamask.SetValue(immask) obj1 = Obj(obj1_data, obj1_datamask) # 初始化trans trans1_0 = Trans(obj1) trans_opts = GetTransOpts() trans_opts['xz_theta'] = func.RandomAngle(-np.pi / 2, np.pi / 2) trans_opts['py'] = func.RandomDis((-100, -100), (100, 100)) trans1_0.QuickTrans(['py', 'xz'], trans_opts) # 另一种写法: # trans.GenTrans('py',trans_opts) # trans.GenTrans('xz',trans_opts) # trans.ImposeTrans() trans1_1 = Trans(trans1_0.obj_imB)