def __init__(self, id_=None): self.id = id_ # 注意TransOptsManager只管理单个对象,但是可以被多个对象共用,且如果使用随机函数则 # 每个对象的变换参数也会不同,但是他们来自于同一个形式的随机函数 # 唯一的操作号:操作过程(可能包含函数,用于产生<操作结果>) self.operates_dict = {} self.operates_data = {} # 唯一的操作号:操作结果(固定值,用于导入Trans) self.operates_mode = {} self.c = count() self.mode = 'cover' # self.TRANS_TYPE = GetTransInfo()[0] self.TRANS_OPTSLIST = GetTransOpts().keys()
def SetRandom(self, item, fun, parameter, mark=None): # 设置随机函数 self.operates_mode[mark] = self.mode # 给每个变换加单独的mode值 if item in self.TRANS_OPTSLIST: # 添加变换 if mark in self.operates_dict: self.operates_dict[mark][item] = ['func', fun, parameter] else: assert item in self.TRANS_TYPE self.operates_data[mark] = [item, GetTransOpts()] self.operates_dict[mark] = {item: ['func', fun, parameter]} else: print(f'WARRING: TransOptsManager.SetRandom:' + f'{item} is not in the TRANS_TYPE or TransOpts')
def SetValue(self, item, value=None, mark=None): # 设置固定值 if item in self.TRANS_OPTSLIST: # 添加变换 self.operates_mode[mark] = self.mode # 给每个变换加单独的mode值 if mark in self.operates_dict: self.operates_dict[mark][item] = ['const', value] else: assert item in self.TRANS_TYPE # 保证创建新操作的时候item的合法性 self.operates_data[mark] = [item, GetTransOpts()] self.operates_dict[mark] = {item: ['const', value]} else: print(f'WARRING: TransOptsManager.SetValue:' + f'{item} is not in the TRANS_TYPE or TransOpts')
def Get(self): # 获取参数,随机函数在此时生效 operates_dict = self.operates_dict # 获取值产生器 operates_data = self.operates_data operates_mode = self.operates_mode operates = [] operates_opts = [] for mark in operates_dict: # 对于对象的每个操作 # 获得操作类型 和 参数 mode = operates_mode[mark] operate, trans_opts = operates_data[mark] if(mode == 'cover'): # 如果是覆盖模式,则重新生成新的 参数 trans_opts = GetTransOpts() # 对于每一个变换操作做循环,大部分变换由于只有一个参数其实都只循环了一次 for item in operates_dict[mark]: sub_opt = operates_dict[mark][item] types = sub_opt[0] # 值产生器类型,固定值或函数 if types is 'const': value = sub_opt[1] elif types is 'func': fun = sub_opt[1] parameter = sub_opt[2] value = fun(*parameter) if value is not None: # 根据覆盖、调整、不变对原来的 参数 进行修改 if(mode == 'cover'): trans_opts[item] = value operates_mode[mark] = 'const' # 下次就不变了 elif(mode == 'modify1'): trans_opts[item] += value operates_mode[mark] = 'const' # 下次就不变了 elif(mode == 'modifyn'): trans_opts[item] += value else: pass operates.append(operate) operates_opts.append(trans_opts) operates_data[mark][1] = trans_opts # 引用赋值,改变了变量 # print(operates_mode) return (operates, operates_opts)
##################################### # 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) 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) trans1_1 = Trans(trans1_0.obj_imB) trans_opts = GetTransOpts() trans_opts['xz_theta'] = func.RandomAngle(-np.pi/36, np.pi/36) trans_opts['py'] = func.RandomDis((-40, -40), (40, 40)) trans1_1.QuickTrans(['py', 'xz'], trans_opts) trans1_2 = Trans(trans1_1.obj_imB) trans_opts['xz_theta'] += func.NormalAngle(0, 1, 'd') trans_opts['py'] += func.NormalDis(0, 2) trans1_2.QuickTrans(['py', 'xz'], trans_opts)
# =============== 初始化区域 ================== # 背景 # 初始化位置,大小 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的初始位置及大小 img,imgmask = materialGenerator.RandomGet() im = np.array(img) immask = np.array(imgmask) > 0 pos = func2.RandomPoint(obj_minIniPos, obj_maxIniPos) size = (im.shape[1],im.shape[0]) # 初始化obj