def fake_char_boxes(self, src, word_box, word_length):
        img, src_points, crop_points = crop_image(src, word_box, dst_height=64.)
        h, w = img.shape[:2]
        if min(h, w) == 0:
            confidence = 0.5
            region_boxes = divide_region(word_box, word_length)
            region_boxes = [reorder_points(region_box) for region_box in region_boxes]
            return region_boxes, confidence
        img = img_normalize(img)
        region_score, _ = self.base_model.predict(np.array([img]))
        heat_map = region_score[0] * 255.
        heat_map = heat_map.astype(np.uint8)
        marker_map = watershed(heat_map)
        region_boxes = find_box(marker_map)
        confidence = cal_confidence(region_boxes, word_length)
        if confidence <= 0.5:
            confidence = 0.5
            region_boxes = divide_region(word_box, word_length)
            region_boxes = [reorder_points(region_box) for region_box in region_boxes]
        else:
            region_boxes = np.array(region_boxes) * 2
            region_boxes = enlarge_char_boxes(region_boxes, crop_points)
            region_boxes = [un_warping(region_box, src_points, crop_points) for region_box in region_boxes]

        return region_boxes, confidence
    def fake_char_boxes(self, src, word_box, word_length):
        img, src_points, crop_points = crop_image(src,
                                                  word_box,
                                                  dst_height=64.)
        h, w = img.shape[:2]
        if min(h, w) == 0:
            confidence = 0.5
            region_boxes = divide_region(word_box, word_length)
            region_boxes = [
                reorder_points(region_box) for region_box in region_boxes
            ]
            return region_boxes, confidence

        img = img_normalize(img)
        region_score, affinity_score = self.test_net(self.craft, img,
                                                     self.cuda)
        heat_map = region_score * 255.
        heat_map = heat_map.astype(np.uint8)
        marker_map = watershed(heat_map)
        region_boxes = find_box(marker_map)
        confidence = cal_confidence(region_boxes, word_length)
        if confidence <= 0.5:
            confidence = 0.5
            region_boxes = divide_region(word_box, word_length)
            region_boxes = [
                reorder_points(region_box) for region_box in region_boxes
            ]
        else:
            region_boxes = divide_region(word_box, word_length)
            region_boxes = [
                reorder_points(region_box) for region_box in region_boxes
            ]

        return region_boxes, confidence
Ejemplo n.º 3
0
def create_pseudo_gt(word_boxes, word_lengths, region, affinity, confidence,
                     pred_region):
    """
    Generate character boxes from each word-level annotation in a weakly-supervised manner.
    In order to reflect the reliability of the interim model’s prediction,
    the value of the confidence map over each word box is computed proportional to the number of
    the detected characters divided by the number of the ground truth characters,
    which is used for the learning weight during training.
    :param word_boxes: (word boxes, word_length).
    :param word_lengths: region map.
    :param region: region map.
    :param affinity: affinity map.
    :param confidence: confidence map.
    :param pred_region: region map.
    :return: region map, affinity map, confidence map.
    """
    gaussian_generator = GaussianGenerator()
    for word_box, word_length in zip(word_boxes, word_lengths):
        if word_length > 0:
            try:
                word_box = reorder_points(word_box)
                heat_map, src_points, crop_points = crop_image(
                    pred_region * 255., word_box)
                heat_map = heat_map.astype(np.uint8)
                marker_map = watershed(heat_map)
                region_boxes = find_box(marker_map)

                confidence_value = cal_confidence(region_boxes, word_length)
                if confidence_value <= 0.5:
                    confidence_value = 0.5
                    region_boxes = divide_region(word_box, word_length)
                    region_boxes = [
                        reorder_points(region_box)
                        for region_box in region_boxes
                    ]
                else:
                    region_boxes = enlarge_char_boxes(region_boxes,
                                                      crop_points)
                    region_boxes = [
                        un_warping(region_box, src_points, crop_points)
                        for region_box in region_boxes
                    ]

                tmp_confidence_mask = np.zeros(confidence.shape[:2],
                                               dtype=np.uint8)
                cv2.fillPoly(tmp_confidence_mask, [np.int32(word_box)], 1)
                tmp_confidence = tmp_confidence_mask.astype(
                    np.float32) * confidence_value
                confidence = (
                    1 - tmp_confidence_mask) * confidence + tmp_confidence

                tmp_region = np.float32(
                    gaussian_generator.gen(region.shape[:2], region_boxes))
                region = np.where(tmp_region > 0, tmp_region, region)

                affinity_boxes = cal_affinity_boxes(region_boxes)
                tmp_affinity = np.float32(
                    gaussian_generator.gen(affinity.shape[:2], affinity_boxes))
                affinity = np.where(tmp_affinity > 0, tmp_affinity, affinity)
            except Exception as e:
                print(e)
                traceback.print_exc()
        else:
            break

    return region, affinity, confidence