Example #1
0
def general_ocr_port(img,det_ip_port,rec_ip_port,MyList):

    det_client = Client()
    det_client.load_client_config("./general_ocr_config/det_infer_client/serving_client_conf.prototxt")
    det_client.connect(det_ip_port)

    #start rec Client
    rec_client = Client()
    rec_client.load_client_config("./general_ocr_config/rec_infer_client/serving_client_conf.prototxt")
    rec_client.connect(rec_ip_port)
    #前处理
    feed, fetch, tmp_args = det_preprocess(img)
    #推理
    fetch_map = det_client.predict(feed, fetch)
    outputs = [fetch_map[x] for x in fetch]

    #后处理
    dt_boxes = det_postprocess(outputs, tmp_args)
    # print(dt_boxes.shape)
    #裁剪出框
    img_crop_list = []
    dt_boxes = sorted_boxes(dt_boxes)
    for bno in range(len(dt_boxes)):
        tmp_box = copy.deepcopy(dt_boxes[bno])
        img_crop = get_rotate_crop_image(img, tmp_box)
        img_crop_list.append(img_crop)

    #以batch为30开始识别
    batch_size = 8
    batch_num = len(img_crop_list) // batch_size + 1
    batch_num = len(img_crop_list)
    text_list = []
    score_list =[]
    for i in range(batch_num):
        if i == (batch_num-1):
            img_batch = img_crop_list[i*batch_size:]
        else :
            img_batch = img_crop_list[i*batch_size:(i+1)*batch_size]
        if(len(img_batch)==0):
            continue
        feed, fetch, tmp_args = rec_preprocess(img_batch)
        #推理
        fetch_map = rec_client.predict(feed, fetch)
        # print(fetch_map)
        outputs = [fetch_map[x] for x in fetch]
        for x in fetch_map.keys():
            if ".lod" in x:
                # print(x),fetch_map[x]
                tmp_args[x] = fetch_map[x]
        #后处理
        rec_res = rec_postprocess(outputs, tmp_args)
        for x in rec_res:
            text_list.append(x[0])
            score_list.append(x[1])
    MyList.append(text_list)    
    det_client.release()
    rec_client.release()
    return 
Example #2
0
def general_ocr_port(img, det_ip_port, rec_ip_port):
    '''
    General_ocr_rec function
    Args:
        img: Input image,opencv BGR888
        det_ip_port: The det serving port to listen
        rec_ip_port: The rec serving port to listen
    return:
        dt_boxes:detection boxes list
        text_list: Boxes recognition result
        score_list: The confidence of boxes recognition result 
    '''
    #开始检测
    #前处理
    #初始化检测和识别
    det_client = Client()
    det_client.load_client_config(
        "./general_ocr_config/det_infer_client/serving_client_conf.prototxt")
    det_client.connect(det_ip_port)

    feed, fetch, tmp_args = det_preprocess(img)
    #推理
    fetch_map = det_client.predict(feed, fetch)
    outputs = [fetch_map[x] for x in fetch]

    #后处理
    dt_boxes = det_postprocess(outputs, tmp_args)
    # print(dt_boxes.shape)
    #裁剪出框
    img_crop_list = []
    dt_boxes = sorted_boxes(dt_boxes)
    for bno in range(len(dt_boxes)):
        tmp_box = copy.deepcopy(dt_boxes[bno])
        img_crop = get_rotate_crop_image(img, tmp_box)
        img_crop_list.append(img_crop)

    #以batch为30开始识别
    batch_size = 8
    batch_num = len(img_crop_list) // batch_size + 1
    text_list = []
    score_list = []
    if len(img_crop_list) == 0:
        return text_list, score_list
    #start rec Client
    rec_client = Client()
    rec_client.load_client_config(
        "./general_ocr_config/rec_infer_client/serving_client_conf.prototxt")
    rec_client.connect(rec_ip_port)
    feed, fetch, tmp_args = det_preprocess(img)

    for i in range(batch_num):
        if i == (batch_num - 1):
            img_batch = img_crop_list[i * batch_size:]
        else:
            img_batch = img_crop_list[i * batch_size:(i + 1) * batch_size]
        if (len(img_batch) == 0):
            continue
        feed, fetch, tmp_args = rec_preprocess(img_batch)

        #推理
        fetch_map = rec_client.predict(feed, fetch)
        # print(fetch_map)
        outputs = [fetch_map[x] for x in fetch]
        for x in fetch_map.keys():
            if ".lod" in x:
                # print(x),fetch_map[x]
                tmp_args[x] = fetch_map[x]
        #后处理
        rec_res = rec_postprocess(outputs, tmp_args)
        for x in rec_res:
            text_list.append(x[0])
            score_list.append(x[1])
    rec_client.release()
    det_client.release()
    return dt_boxes, text_list, score_list