예제 #1
0
 def __init__(self,
              name,
              health,
              level,
              experience,
              strength,
              skill,
              speed,
              luck,
              defence,
              resistance,
              movement,
              constitution,
              aid,
              affinity,
              wrank,
              health_max=None):
     self.name = str(name)  # name of the Unit
     self.health = int(health)  # current health
     self.health_max = health_max if health_max else self.health  # maximum health
     self.health_prev = health  # HP before an attack
     self.level = int(level)  # level
     self.level_prev = self.level
     self.experience = int(experience)  # experience
     self.exp_prev = experience
     self.strength = int(
         strength)  # strength determines the damage inflicted to the enemy
     self.skill = int(skill)  # skill chance of hitting the enemy
     self.speed = int(speed)  # speed chance to avoid enemy's attack
     self.luck = int(luck)  # luck influences many things
     self.defence = int(defence)  # defence reduces phisical damages
     self.resistance = int(resistance)  # resistance reduces magical damages
     self.movement = int(
         movement
     )  # movement determines how far the unit can move in a turn
     self.constitution = int(
         constitution)  # constitution, or phisical size. affects rescues.
     self.aid = int(
         aid
     )  # max rescuing constitution. units with lower con can be rescued.
     self.affinity = affinity  # elemental affinity. determines compatibility with other units.
     self.condition = NormalHealthCondition(self)
     self.wrank = wrank  # weapons' levels.
     self.items = Items()  # list of items
     self.played = False  # wether unit was used or not in a turn
     self.team = None  # team
     self.coord = None
     self.modified = True
     try:
         self.image = resources.load_sprite(self.name).convert_alpha()
         new_size = utils.resize_keep_ratio(self.image.get_size(),
                                            (200, 200))
         self.image = pygame.transform.smoothscale(self.image, new_size)
     except pygame.error:
         logging.warning("Couldn't load %s! Loading default image",
                         resources.sprite_path(self.name))
         self.image = resources.load_sprite('no_image.png').convert_alpha()
예제 #2
0
def data_generator(folder_name, name2idx, prior_bboxes):
    img_files = glob.glob(os.path.join(config.ROOT_PATH, folder_name, "JPEGImages/*.jpg"))
    while True:
        fn = np.random.choice(img_files)
        img = cv2.imread(fn, cv2.IMREAD_COLOR)
        ratio, img, bbox_offset = utils.resize_keep_ratio(img, config.IMG_SIZE)
        xml = os.path.join(config.ROOT_PATH, folder_name, "Annotations/%s.xml" % fn.split('\\' if os.name == 'nt' else '/')[-1].split('.')[0])
        cls_name, bbox_resized = utils.get_class_and_bbox(xml, ratio)
        bbox_resized[:2] += bbox_offset
        bbox_resized[2:] += bbox_offset

        # random horizontal flip
        # TODO: add random crop
        if np.random.rand() < 0.5:
            img = img[:, ::-1, :]
            bbox_resized[::2] = config.IMG_SIZE - bbox_resized[::2] - 1
            bbox_resized[::2] = bbox_resized[2::-2]
        cls_idx = name2idx[cls_name]
        bbox_size_normalized = np.array([(bbox_resized[2] - bbox_resized[0]) / config.IMG_SIZE, (bbox_resized[3] - bbox_resized[1]) / config.IMG_SIZE])
        bbox_center_normalized = np.array([(bbox_resized[0] + bbox_resized[2]) / 2 / config.IMG_SIZE, (bbox_resized[1] + bbox_resized[3]) / 2 / config.IMG_SIZE])

        # generate bbox offset target
        scales = [32, 16, 8]
        targets = []
        for i in range(3):
            N = config.IMG_SIZE // scales[i]
            base_offset = int(bbox_center_normalized[0] * N), int(bbox_center_normalized[1] * N)
            target = np.zeros([N, N, 3, (4 + 1 + config.NUM_CLASSES)])
            target[base_offset[0], base_offset[1], :, 4] = 1

            pos_delta = bbox_center_normalized * N - base_offset
            size_delta = np.log(bbox_size_normalized / (prior_bboxes[i*3:i*3+3] / config.IMG_SIZE))

            # position delta comes first
            target[base_offset[0], base_offset[1], :, :4] = np.concatenate([np.tile(pos_delta[np.newaxis, :], (3, 1)), size_delta], axis=1)
            target[base_offset[0], base_offset[1], :, 5:] = np.eye(config.NUM_CLASSES)[cls_idx]
            targets.append(target)

        if config.DEBUG:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
            plt.imshow(img_rgb)
            # coco.showAnns(anns)
            ax.add_patch(
                patches.Rectangle(
                    (bbox_resized[0], bbox_resized[1]),
                    bbox_resized[2] - bbox_resized[0],
                    bbox_resized[3] - bbox_resized[1],
                    edgecolor="red",
                    fill=False  # remove background
                )
            )
            plt.show()
        assert img.shape[0] == 256 and img.shape[1] == 256
        yield img, targets[0], targets[1], targets[2]
예제 #3
0
    def __init__(self, txt_file: str, root_dir: str, image_size=(640, 480), crop_size=(96, 96), transform=None):
        super().__init__()
        self.transform = transform
        self.root_dir = root_dir
        self.image_info = open(txt_file, 'r').readlines()
        self.image_size = image_size
        self.crop_size = crop_size

        self.data = []
        self.labels = []
        for i in range(len(self.image_info)):
            img_path = os.path.join(self.root_dir, self.image_info[i].split(' ')[0].strip())
            img = cv2.imread(img_path)

            points_seven = self.image_info[i].strip().replace(',', '').split(' ')[1:]
            assert len(points_seven)//2 == 7, 'the ground truth should contain 7 points information'
            points = []
            for j in range(len(points_seven)//2):
                point = (int(points_seven[2 * j]), int(points_seven[2 * j + 1]))
                # np_point = np.array([int(point_str[0]) / img.shape[1], int(point_str[1]) / img.shape[0]]) # scale to [0,1]
                points.append(point)

            # resize image to 640x480 keep ratio
            h, w, _ = img.shape
            img, trans = resize_keep_ratio(img, self.image_size)
            for i in range(len(points)):
                resize_ratio = trans[0]
                pad_l = trans[1]
                pad_t = trans[2]
                points[i] = (int(points[i][0] * resize_ratio + pad_l), int(points[i][1] * resize_ratio + pad_t))

            # resize image to 640x480
            # h, w, _ = img.shape
            # img = cv2.resize(img, self.image_size, interpolation=cv2.INTER_CUBIC)
            # ratio_w = self.image_size[0] / w
            # ratio_h = self.image_size[1] / h
            # for i in range(len(points)):
            #     points[i] = (int(points[i][0] * ratio_w), int(points[i][1] * ratio_h))

            # visualize
            # visualize_gt(img, points)

            np_points = np.array(points)
            self.data.append(np.array(img))
            self.labels.append(np_points)
예제 #4
0
 def bg_image_resized(self) -> None:
     """
     Resize self.bg_image so that it can fit self.surface respecting the type of resize specified by self.bg_size.
     """
     if self.bg_size == 'contain':
         new_size = utils.resize_keep_ratio(self.bg_image.get_size(),
                                            self.rect.size)
     elif self.bg_size == 'cover':
         new_size = utils.resize_cover(self.bg_image.get_size(),
                                       self.rect.size)
     else:
         new_size = (int(self.bg_size[0] / 100 * self.rect.w),
                     int(self.bg_size[1] / 100 * self.rect.h))
     if new_size == self._bg_image_size:
         return self._bg_image_resized
     self._bg_image_resized = pygame.transform.smoothscale(
         self.bg_image, new_size)
     self._bg_image_size = self.rect.size
     return self._bg_image_resized
예제 #5
0
    def update(self):
        if self.zoom != self.tilemap.zoom:
            self.zoom_changed()
        elif not self.unit.was_modified():
            return

        logging.debug("Sprite update: %s" % self.unit.name)

        w, h = self.rect.size
        mw, mh = img_max_size = (w, h - 5)
        mw2, mh2 = mw // 2, mh // 2

        self.image.fill((0, 0, 0, 0))
        pygame.draw.circle(self.image, self.unit.team.color, (mw2, mh2), mh2,
                           3)

        src_img = self.unit.image
        if src_img is None:
            self.image.blit(
                src_img, utils.center(self.image.get_rect(),
                                      src_img.get_rect()))
        else:
            image_size = utils.resize_keep_ratio(src_img.get_size(),
                                                 img_max_size)
            resized_image = pygame.transform.smoothscale(
                src_img, image_size).convert_alpha()
            self.image.blit(
                resized_image,
                utils.center(self.image.get_rect(), resized_image.get_rect()))

        hp_bar_length = int(self.unit.health / self.unit.health_max *
                            self.rect.w)
        hp_bar = pygame.Surface((hp_bar_length, 5))
        hp_bar.fill((0, 255, 0))
        self.image.blit(hp_bar, (0, self.rect.h - 5))

        if self.team.is_boss(self.unit):
            icon = pygame.Surface((3, 3))
            icon.fill(c.BLUE)
            self.image.blit(icon, (0, self.rect.h - 4))
예제 #6
0
def data_generator():
    ann_file = '{}/annotations/instances_{}.json'.format(
        config.DATASET_DIR, config.DATASET_TYPE)
    coco = COCO(ann_file)
    categories = coco.loadCats(coco.getCatIds())
    nms = [cat['name'] for cat in categories]
    print('COCO categories: \n{}\n'.format(' '.join(nms)))

    img_ids = coco.getImgIds()
    all_anchors = utils.generate_anchors()
    while True:
        rand = np.random.randint(0, len(img_ids))
        # rand = 3118
        # print(rand)

        img_info = coco.loadImgs(img_ids[rand])[0]
        img = scipy.ndimage.imread(config.DATASET_DIR + '\\' +
                                   config.DATASET_TYPE + '\\' +
                                   img_info['file_name'])
        img = img.astype(np.float32) / 255.
        ratio, img, offset = utils.resize_keep_ratio(img, (1024, 1024))

        ann_ids = coco.getAnnIds(imgIds=img_info['id'], iscrowd=0)
        anns = coco.loadAnns(ann_ids)
        bboxs = [ann['bbox'] for ann in anns]
        bboxs = np.vstack(bboxs)
        # OFFSET one for backgroound
        cls = np.array([ann['category_id'] + 1 for ann in anns])
        masks = np.array([
            utils.annToMask(ann, img_info['height'], img_info['width'])
            for ann in anns
        ])

        # resize masks to desired shape
        bboxs_ind = bboxs.astype(np.int)
        masks = np.array([
            cv2.resize(
                mask[bboxs_ind[i, 1]:bboxs_ind[i, 1] + bboxs_ind[i, 3],
                     bboxs_ind[i, 0]:bboxs_ind[i, 0] + bboxs_ind[i, 2]],
                (config.MASK_OUTPUT_SHAPE, config.MASK_OUTPUT_SHAPE))
            for i, mask in enumerate(masks)
        ])
        bboxs = bboxs * ratio
        bboxs[:, :2] += offset
        bboxs_rpn = bboxs

        valid_label_range = 0
        # we pad ot trim all labels to MAX_GT_TRAIN_INSTANCES to make it batched
        if bboxs.shape[0] > config.MAX_GT_TRAIN_INSTANCES:
            valid_label_range = config.MAX_GT_TRAIN_INSTANCES
            bboxs = bboxs[:config.MAX_GT_TRAIN_INSTANCES, :]
            cls = cls[:config.MAX_GT_TRAIN_INSTANCES]
            masks = masks[:config.MAX_GT_TRAIN_INSTANCES, :, :]
        else:
            valid_label_range = bboxs.shape[0]
            bboxs = np.pad(
                bboxs,
                ((0, config.MAX_GT_TRAIN_INSTANCES - bboxs.shape[0]), (0, 0)),
                mode='constant',
                constant_values=((0, 0), (0, 0)))
            cls = np.pad(cls,
                         (0, config.MAX_GT_TRAIN_INSTANCES - cls.shape[0]),
                         mode='constant',
                         constant_values=(0, 0))
            masks = np.pad(
                masks, ((0, config.MAX_GT_TRAIN_INSTANCES - masks.shape[0]),
                        (0, 0), (0, 0)),
                mode='constant',
                constant_values=((0, 0), (0, 0), (0, 0)))

        # pre compute rpn targets
        anchor_types, matches = utils.generate_anchor_types(
            all_anchors, bboxs_rpn)
        rpn_positive_mask, rpn_mask = utils.get_mask(anchor_types)
        rpn_labels = utils.generate_rpn_labels(anchor_types, rpn_mask)
        rpn_deltas = utils.generate_rpn_deltas(all_anchors, bboxs_rpn,
                                               rpn_positive_mask, matches)
        rpn_positive_range = rpn_deltas.shape[0]
        # do some padding
        rpn_deltas = np.pad(
            rpn_deltas,
            ((0, config.RPN_ANCHORS_TRAIN_PER_IMAGE - rpn_positive_range),
             (0, 0)), 'constant')
        rpn_positive_mask = np.pad(
            rpn_positive_mask,
            (0, config.RPN_ANCHORS_TRAIN_PER_IMAGE - rpn_positive_range),
            'constant',
            constant_values=-1)

        if config.DEBUG:
            fig = plt.figure()
            ax = fig.add_subplot(111)
            plt.imshow(img)
            # coco.showAnns(anns)
            for bbox in bboxs:
                ax.add_patch(
                    patches.Rectangle(
                        (bbox[0], bbox[1]),
                        bbox[2],
                        bbox[3],
                        edgecolor="red",
                        fill=False  # remove background
                    ))
            for m in matches:
                ax.add_patch(
                    patches.Rectangle(
                        (all_anchors[m][0], all_anchors[m][1]),
                        all_anchors[m][2],
                        all_anchors[m][3],
                        edgecolor="blue",
                        fill=False  # remove background
                    ))
            plt.show()
        # we feed precomputed rpn masks on multi-threaded cpu
        print()
        yield img, bboxs, rpn_labels, rpn_deltas, rpn_mask, rpn_positive_range, rpn_positive_mask, cls, masks, valid_label_range