def load_data_generator(self, generator_json, config_json):
        new_data_generator = DataGenerator2D.DataGenerator2D()
        new_data_generator.from_json(generator_json)
        if self.check_if_config_exist(new_data_generator.name):
            if QMessageBox.Cancel == QMessageBox.question(
                    self, "Question",
                    "Config name exist, still load and rename? ",
                    QMessageBox.Yes | QMessageBox.Cancel):
                return
            else:
                new_data_generator.name = self.update_config_name(
                    new_data_generator.name)

        if config_json is None:
            self.generator_name_to_inputs[
                new_data_generator.name] = GenerateConfig()
        elif not 'generate_image_num' in config_json:
            self.generator_name_to_inputs[
                new_data_generator.name] = GenerateConfig(
                    generate_image_num=self.ui.generate_num.value())
        else:
            self.generator_name_to_inputs[
                new_data_generator.name] = GenerateConfig(config_json)

        self.data_generators.append(
            copy.deepcopy(new_data_generator))  # add generator

        self.create_image_attachment_gallery(new_data_generator.name)

        self.bind_widgets_to_generator(
            self.find_data_generator(new_data_generator.name)
        )  # bind object list and algo list widget
        self.ui.configList.addItem(new_data_generator.name)
        self.ui.configList.setCurrentRow(self.ui.configList.count() - 1)
def create_generator_config(random_settings):
    random_generate_image_num = np.random.randint(1, max_image_num)
    new_generate_config = GenerateConfig(random_settings)
    new_generate_config.generate_image_num = random_generate_image_num
    new_generate_config.stop_condition = stop_conditon
    new_generate_config.label_to_items = image_gallery.label_to_items
    new_generate_config.label_to_density_gennum = new_generate_config.update_label_to_density_gennum(
        label_to_items=image_gallery.label_to_items)
    for key in new_generate_config.label_to_density_gennum:
        new_generate_config.label_to_density_gennum[key][
            GenerateConfig.density_gennum_params[2]] = np.random.randint(
                0, MAX_DensityMax)
    return new_generate_config
def generate_executor(dest_path='',
                      prefix='2007_',
                      config_key='',
                      data_generator=DataGenerator2D(),
                      config=GenerateConfig()):
    # np.random.seed(np.int64(time.time() + int(config_key)))
    _config = copy.deepcopy(config)
    _image_num = 0
    _remain_num = 0
    if _config.stop_condition == 'By Image Num':
        print('process', config_key, 'required_image_num',
              _config.generate_image_num)
        _remain_num = _remain_generate_image_num(
            image_num=_image_num,
            required_image_num=_config.generate_image_num)
    elif _config.stop_condition == 'By Seed Num':
        _remain_num = _remain_generate_seed_num(
            label_to_density_gennum=_config.label_to_density_gennum)
    while _remain_num > 0:

        print('process', config_key, '_remain_num', _remain_num)
        img, _key_to_outputs = generate_one_image(
            data_generator=data_generator, config=_config)
        _write_color_img(img,
                         idx=_image_num,
                         dest_path=dest_path,
                         prefix=prefix)
        _write_json(items=_key_to_outputs['items'],
                    idx=_image_num,
                    dest_path=dest_path,
                    prefix=prefix,
                    generate_image_shape=list(img.shape))
        _image_num += 1

        if _config.stop_condition == 'By Image Num':
            _remain_num = _remain_generate_image_num(
                image_num=_image_num,
                required_image_num=_config.generate_image_num)
        elif _config.stop_condition == 'By Seed Num':
            _label_to_item_count = _key_to_outputs['_label_to_item_count']
            _config.label_to_density_gennum = update_seed_num(
                _label_to_item_count=_label_to_item_count,
                label_to_density_gennum=_config.label_to_density_gennum)
            _remain_num = _remain_generate_seed_num(
                label_to_density_gennum=_config.label_to_density_gennum)
    def create_data_generator(self, name):
        current_data_generator = self.get_current_generator()
        operator_types, operator_type_to_param = current_data_generator.get_operator_types_and_params(
        )
        new_data_generator = DataGenerator2D.DataGenerator2D(
            name=name,
            operator_types=operator_types,
            operator_type_to_params=operator_type_to_param)

        self.generator_name_to_inputs[
            new_data_generator.name] = GenerateConfig(
                generate_image_num=self.ui.generate_num.value())

        self.create_image_attachment_gallery(new_data_generator.name)
        image_gallery = self.image_galleries[new_data_generator.name]
        attachment_gallery = self.attachment_galleries[new_data_generator.name]

        # use global label to items and label to desity gennum
        self.generator_name_to_inputs[
            new_data_generator.
            name].generate_image_num = self.ui.generate_num.value()
        self.generator_name_to_inputs[
            new_data_generator.
            name].stop_condition = self.ui.stop_condition.currentText()
        self.generator_name_to_inputs[
            new_data_generator.
            name].label_to_items = image_gallery.label_to_items
        self.generator_name_to_inputs[
            new_data_generator.
            name].label_to_attachment = attachment_gallery.label_to_items

        self.generator_name_to_inputs[new_data_generator.name].label_to_density_gennum = \
            copy.deepcopy(self.generator_name_to_inputs[current_data_generator.name].label_to_density_gennum)
        self.data_generators.append(
            copy.deepcopy(new_data_generator))  # add generator

        self.bind_widgets_to_generator(
            self.find_data_generator(new_data_generator.name)
        )  # bind object list and algo list widget
        self.ui.configList.addItem(new_data_generator.name)
        self.ui.configList.setCurrentRow(self.ui.configList.count() - 1)
 def process(self, config=GenerateConfig()):
     try:
         keys = []
         for key in config.label_to_density_gennum.keys(
         ):  # exmple {'TULING': {'GenNum': 1.0, 'SeedNum': 22, 'DensityMax': 2, 'DensityMin': 0}}
             if config.label_to_density_gennum[key]["DensityMax"] == 0:
                 keys.append(key)
         for key in keys:
             config.label_to_density_gennum.pop(key, None)
         _key_to_inputs = config.__dict__
         _key_to_outputs = {}
         for operator in self.operators:  # all algorithm
             if operator.is_used:
                 logging.info(operator.type)
                 outputs = operator.process(_key_to_inputs)
                 print(operator.type, 'finished')
                 for output_key, output in zip(operator.outputs, outputs):
                     _key_to_outputs[output_key] = output
                 _key_to_inputs = _key_to_outputs
         return _key_to_outputs
     except Exception:
         traceback.print_exc()
def generate_one_image(data_generator=DataGenerator2D(),
                       config=GenerateConfig()):
    _key_to_outputs = data_generator.process(config)
    b, g, r, _ = cv2.split(_key_to_outputs['image'])
    img = cv2.merge([b, g, r])
    return img, _key_to_outputs
                image_num=_image_num,
                required_image_num=_config.generate_image_num)
        elif _config.stop_condition == 'By Seed Num':
            _label_to_item_count = _key_to_outputs['_label_to_item_count']
            _config.label_to_density_gennum = update_seed_num(
                _label_to_item_count=_label_to_item_count,
                label_to_density_gennum=_config.label_to_density_gennum)
            _remain_num = _remain_generate_seed_num(
                label_to_density_gennum=_config.label_to_density_gennum)


if __name__ == '__main__':
    src = 'D:/data/distort'
    test = ImageGallery()
    test.load_multi_items(src)
    config = GenerateConfig()
    config.label_to_items = test.label_to_items
    config.label_to_density_gennum = {
        'distort': {
            'DensityMin': 1,
            'DensityMax': 5,
            'GenNum': 30
        }
    }

    config.operator_names = [
        'SeedItemPicker', 'RandomHighLight', 'ItemBlender', 'color_bg',
        'ItemsToImage', 'UpdateGenerateNum'
    ]
    config.operator_to_params = {
        'SeedItemPicker': {