paddlehub 文本检测使用

发布时间:2024年01月04日

PaddleHub负责模型的管理、获取和预训练模型的使用。
参考:https://github.com/PaddlePaddle/PaddleHub/tree/develop/modules/image/text_recognition/chinese_text_detection_db_server
在这里插入图片描述

 

在这里插入图片描述

import paddlehub as hub
import cv2
import numpy as np


def cv_show(img):
    '''
    展示图片
    @param img:
    @param name:
    @return:
    '''
    cv2.namedWindow('name', cv2.WINDOW_KEEPRATIO)  # cv2.WINDOW_NORMAL | cv2.WINDOW_KEEPRATIO
    cv2.imshow('name', img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

# 输入图片路径
image_path = 'pic/img.jpg'
image = cv2.imread(image_path)


gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
gray_padding = cv2.cvtColor( np.pad(gray, ((100, 100), (0, 0)), 'constant', constant_values=(255)), cv2.COLOR_GRAY2BGR)


# 检测+识别
paddle_ocr = hub.Module(name="ch_pp-ocrv3")  #SVTR_LCNet     # mkldnn加速仅在CPU下有效 , enable_mkldnn=True
paddle_ocr.recognize_text(images=[gray_padding]   )

ocr = hub.Module(name="chinese_ocr_db_crnn_server")#CRNN
ocr.recognize_text(images=[gray_padding]   )
# def recognize_text(images=[],
#                    paths=[],
#                    use_gpu=False,
#                    output_dir='ocr_result',
#                    visualization=False,
#                    box_thresh=0.6,
#                    text_thresh=0.5,
#                    angle_classification_thresh=0.9,
#                    det_db_unclip_ratio=1.5,
#                    det_db_score_mode="fast"):
# print('text',[[''.join(y['text'] for y in x['data'])]  for x in results])

# 检测
text_detector_v3 = hub.Module(name="ch_pp-ocrv3_det")
text_detector_v3.detect_text(images=[gray_padding]   )

text_detector = hub.Module(name='chinese_text_detection_db_server')
result = text_detector.detect_text(images=[gray_padding]   )

# def detect_text(images=[],
#                 paths=[],
#                 use_gpu=False,
#                 output_dir='detection_result',
#                 visualization=False,
#                 box_thresh=0.6,
#                 det_db_unclip_ratio=1.5,
#                 det_db_score_mode="fast")

#可视框
for box in result[0]['data']:
    img = gray_padding[box[0][1]:box[2][1],box[0][0]:box[1][0]]
    cv_show(img)



# for result in results:
#     data = result['data']
#     save_path = result['save_path']
#     for infomation in data:
#         print('text: ', infomation['text'], '\nconfidence: ', infomation['confidence'], '\ntext_box_position: ', infomation['text_box_position'])



文章来源:https://blog.csdn.net/weixin_38235865/article/details/135381296
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。