Exemplo n.º 1
0
import tensorflow as tf
graph = tf.get_default_graph()##解决web.py 相关报错问题

anchors = [float(x) for x in keras_anchors.split(',')]
anchors = np.array(anchors).reshape(-1, 2)
num_anchors = len(anchors)

num_classes = len(class_names)
textModel = yolo_text(num_classes,anchors)
textModel.load_weights(kerasTextModel)


sess = K.get_session()
image_shape = K.placeholder(shape=(2, ))##图像原尺寸:h,w
input_shape = K.placeholder(shape=(2, ))##图像resize尺寸:h,w
box_score = box_layer([*textModel.output,image_shape,input_shape],anchors, num_classes)



def text_detect(img,prob = 0.05):
    im    = Image.fromarray(img)
    scale = IMGSIZE[0]
    w,h   = im.size
    w_,h_ = resize_im(w,h, scale=scale, max_scale=2048)##短边固定为608,长边max_scale<4000
    #boxed_image,f = letterbox_image(im, (w_,h_))
    boxed_image = im.resize((w_,h_), Image.BICUBIC)
    image_data = np.array(boxed_image, dtype='float32')
    image_data /= 255.
    image_data = np.expand_dims(image_data, 0)  # Add batch dimension.
    imgShape   = np.array([[h,w]])
    inputShape = np.array([[h_,w_]])
Exemplo n.º 2
0
def net_output_process(batch_preds,
                       batch_shape,
                       batch_shape_padded,
                       prob=0.05):
    """
    将主干网络的批输出转换为boxes,scores,这里方便兼容之前代码,
    暂且使用for循环,以后可替换为vectorize处理
    @params batch_preds(list of arrays):list长度代表n个采样尺度,其中每个\
        array形状为(batch_size,grid_size_w,grid_size_h,3*(4+1+num_classes))
    @params batch_shape(list of tuples):图片原始长宽
    @params prob(float):置信度小于prob的box将被忽略
    @returns batch_boxes(array): [字符区域数量,8]
    @returns batch_scores:[字符区域数量,]
    """
    batch_boxes = []
    batch_scores = []

    MAX_HORIZONTAL_GAP = 100
    MIN_V_OVERLAPS = 0.6
    MIN_SIZE_SIM = 0.6
    textdetector = TextDetector(MAX_HORIZONTAL_GAP, MIN_V_OVERLAPS,
                                MIN_SIZE_SIM)

    TEXT_PROPOSALS_MIN_SCORE = 0.1
    TEXT_PROPOSALS_NMS_THRESH = 0.3
    TEXT_LINE_NMS_THRESH = 0.99
    LINE_MIN_SCORE = 0.1
    leftAdjustAlph = 0.01
    rightAdjustAlph = 0.01

    # 首先初步对模型主干输出进行预处理
    for y1, y2, y3, image_shape, input_shape in zip(batch_preds[0],
                                                    batch_preds[1],
                                                    batch_preds[2],
                                                    batch_shape,
                                                    batch_shape_padded):
        outputs = [y1, y2, y3, image_shape, input_shape]
        box, scores = box_layer(outputs, anchors, num_classes)
        h, w = image_shape
        keep = np.where(scores > prob)
        # box[:, 0:4][box[:, 0:4]<0] = 0
        box = np.array(box)
        scores = np.array(scores)
        box[box < 0] = 0
        box[:, 0][box[:, 0] >= w] = w - 1
        box[:, 1][box[:, 1] >= h] = h - 1
        box[:, 2][box[:, 2] >= w] = w - 1
        box[:, 3][box[:, 3] >= h] = h - 1
        boxes = box[keep[0]]
        scores = scores[keep[0]]

        # 筛选出需要的box,并且进行nms,字符行组合
        boxes, scores = textdetector.detect(boxes, scores[:, np.newaxis],
                                            (h, w), TEXT_PROPOSALS_MIN_SCORE,
                                            TEXT_PROPOSALS_NMS_THRESH,
                                            TEXT_LINE_NMS_THRESH,
                                            LINE_MIN_SCORE)
        boxes = sort_box(boxes)
        batch_boxes.append(boxes)
        batch_scores.append(scores)
        # print('done')

    return batch_boxes, batch_scores