def fake_char_boxes(net, 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)
    # print(img.shape)
    region_score, _ = net.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
        ]
        # print(word_box, 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
Exemple #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
Exemple #4
0
                                 score_map)
        score_map = np.clip(score_map, 0, 1.)
        # score_map = score_map.astype(np.uint8)
        return score_map


if __name__ == "__main__":
    gaussian_generator = GaussianGenerator()

    # generate a empty image
    img = np.zeros((600, 600, 3), dtype=np.int)

    # regions to place heat-maps
    from utils.box_util import reorder_points
    point = np.asarray([[250, 200], [400, 400], [350, 200], [200, 400]])
    points = np.expand_dims(reorder_points(point), axis=0)
    points = [pts.reshape((-1, 1, 2)) for pts in points]

    # heat-map
    region = gaussian_generator.gen(img.shape[0:2], points)

    # draw result
    from utils.img_util import to_heat_map
    img = to_heat_map(region)
    cv2.polylines(img, points, True, (0, 255, 255))

    # save result
    import os
    if not os.path.exists("../result/"):
        os.makedirs("../result/")
    cv2.imwrite("../result/gaussian_test.jpg", img)