Ejemplo n.º 1
0
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
Ejemplo n.º 3
0
def test_net(net, image, text_threshold, link_threshold, low_text, poly,
             refine_net):
    # t0 = time.time()

    # resize
    img_resized, target_ratio, size_heatmap = resize_aspect_ratio(
        image,
        args.canvas_size,
        interpolation=cv2.INTER_LINEAR,
        mag_ratio=args.mag_ratio)
    ratio_h = ratio_w = 1 / target_ratio

    # preprocessing
    x = img_normalize(img_resized)
    x = np.expand_dims(x, axis=0)  # [h, w, c] to [b, h, w, c]

    # forward pass
    y, feature = net(x)

    # make score and link map
    score_text = y[0, :, :, 0]
    score_link = y[0, :, :, 1]

    # refine link
    if refine_net is not None:
        # TODO
        pass

    # t0 = time.time() - t0
    # t1 = time.time()

    # Post-processing
    boxes, polys = getDetBoxes(score_text, score_link, text_threshold,
                               link_threshold, low_text, poly)

    # coordinate adjustment
    boxes = adjustResultCoordinates(boxes, ratio_w, ratio_h)
    polys = adjustResultCoordinates(polys, ratio_w, ratio_h)
    for k in range(len(polys)):
        if polys[k] is None:
            polys[k] = boxes[k]

    # t1 = time.time() - t1

    # render results (optional)
    render_img = score_text.numpy()
    render_img = np.hstack((render_img, np.ones(
        (np.shape(render_img)[0], 5)), score_link))
    ret_score_text = score_to_heat_map(render_img)

    # if args.show_time : print("\ninfer/postproc time : {:.3f}/{:.3f}".format(t0, t1))

    return boxes, polys, ret_score_text
Ejemplo n.º 4
0
def predict(model, image, text_threshold, link_threshold, low_text):
    t0 = time.time()

    # resize
    h, w = image.shape[:2]
    mag_ratio = 600 / max(h, w)
    # img_resized, target_ratio = img_resize(image, FLAGS.mag_ratio, FLAGS.canvas_size, interpolation=cv2.INTER_LINEAR)
    img_resized, target_ratio = img_resize(image,
                                           mag_ratio,
                                           FLAGS.canvas_size,
                                           interpolation=cv2.INTER_LINEAR)
    ratio_h = ratio_w = 1 / target_ratio

    # preprocessing
    x = img_normalize(img_resized)

    # make score and link map
    score_text, score_link = model.predict(np.array([x]))
    score_text = score_text[0]
    score_link = score_link[0]

    t0 = time.time() - t0
    t1 = time.time()

    # Post-processing
    boxes = getDetBoxes(score_text, score_link, text_threshold, link_threshold,
                        low_text)
    boxes = adjustResultCoordinates(boxes, ratio_w, ratio_h)

    t1 = time.time() - t1

    # render results (optional)
    render_img = score_text.copy()
    white_img = np.ones((render_img.shape[0], 10, 3), dtype=np.uint8) * 255
    ret_score_text = np.hstack(
        (to_heat_map(render_img), white_img, to_heat_map(score_link)))
    # ret_score_text = to_heat_map(render_img)

    if FLAGS.show_time:
        print("\ninfer/postproc time : {:.3f}/{:.3f}".format(t0, t1))

    return boxes, ret_score_text