利用paddle的识别模型识别图片内容(chinese_text_detection_db_server)

配置环境

conda create -n ocr python=3.8
pip install paddlepaddle -i https://mirror.baidu.com/pypi/simple
pip install paddlehub -i https://mirror.baidu.com/pypi/simple

两种使用方法

直接使用(1)

"""可选参数
    paths (list[str]): 图片的路径;
    images (list[numpy.ndarray]): 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
    use_gpu (bool): 是否使用 GPU;若使用GPU,请先设置CUDA_VISIBLE_DEVICES环境变量
    box_thresh (float): 检测文本框置信度的阈值;
    visualization (bool): 是否将识别结果保存为图片文件;
    output_dir (str): 图片的保存路径,默认设为 detection_result;
"""
""" return
    res (list[dict]): 识别结果的列表,列表中每一个元素为 dict,各字段为:
    data (list): 检测文本框结果,文本框在原图中的像素坐标,4*2的矩阵,依次表示文本框左上,右上,右下,左下顶点的坐标
    save_path (str): 识别结果的保存路径, 如不保存图片则save_path为''
"""
import paddlehub as hub
import cv2
image_path =""
text_detector = hub.Module(name="chinese_text_detection_db_server")
result = text_detector.detect_text(images=[cv2.imread(image_path)])
# or
# result =text_detector.detect_text(paths=[image_path])
print(result)

部署服务器(2)

# 终端输入
hub serving start -m chinese_text_detection_db_server
发送请求
import requests
import json
import cv2
import base64

def cv2_to_base64(image):
    data = cv2.imencode('.jpg', image)[1]
    return base64.b64encode(data.tostring()).decode('utf8')

# 发送HTTP请求
data = {'images':[cv2_to_base64(cv2.imread("/PATH/TO/IMAGE"))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/chinese_text_detection_db_server"
r = requests.post(url=url, headers=headers, data=json.dumps(data))

# 打印预测结果
print(r.json()["results"])

更多模型

更多模型可在官网查看

报错:[2025-07-02 23:12:53,128] [ WARNING] - The home directory contains Chinese characters which may cause unknown exceptions in the execution of some modules. Please set another path through the set HUB_HOME command. E0702 23:12:53.176828 26960 analysis_config.cc:169] Please use PaddlePaddle with GPU version. E0702 23:12:53.361079 26960 analysis_config.cc:169] Please use PaddlePaddle with GPU version. 识别中文文本结果: Download https://bj.bcebos.com/paddlehub/paddlehub_dev/chinese_text_detection_db_mobile_1.1.0.zip [##################################################] 100.00% Decompress C:\Users\田龙飞\.paddlehub\tmp\tmpqgas7s5_\chinese_text_detection_db_mobile_1.1.0.zip [##################################################] 100.00% [2025-07-02 23:13:01,965] [ INFO] - Successfully installed dependent packages. [2025-07-02 23:13:02,031] [ INFO] - Successfully installed chinese_text_detection_db_mobile-1.1.0 Traceback (most recent call last): File "E:\Project\Python\SJZ-GC\paddle_ocr.py", line 63, in <module> chinese_texts = recognize_chinese(image_path) File "E:\Project\Python\SJZ-GC\paddle_ocr.py", line 23, in recognize_chinese results = ocr.recognize_text(images=[img], use_gpu=True) File "D:\Exp\Python38\lib\site-packages\paddlehub\compat\paddle_utils.py", line 221, in runner return func(*args, **kwargs) File "E:\PaddleOCR\paddle_hub_models\chinese_ocr_db_crnn_mobile\module.py", line 234, in recognize_text detection_results = self.text_detector_module.detect_text(images=predicted_data, File "D:\Exp\Python38\lib\site-packages\paddlehub\module\module.py", line 95, in __getattribute__ _attr = object.__getattribute__(self, attr) File "E:\PaddleOCR\paddle_hub_models\chinese_ocr_db_crnn_mobile\module.py", line 108, in text_detector_module self._text_detector_module = hub.Module(name='chinese_text_detection_db_mobile', File "D:\Exp\Python38\lib\site-packages\paddlehub\module\module.py", line 393, in __new__ module = cls.init_with_name( File "D:\Exp\Python38\lib\site-packages\paddlehub\module\module.py", line 516, in init_with_name return user_module_cls(**kwargs) File "C:\Users\田龙飞\.paddlehub\modules\chinese_text_detection_db_mobile\module.py", line 61, in __init__ self._set_config() File "D:\Exp\Python38\lib\site-packages\paddlehub\compat\paddle_utils.py", line 221, in runner return func(*args, **kwargs) File "C:\Users\田龙飞\.paddlehub\modules\chinese_text_detection_db_mobile\module.py", line 78, in _set_config config = Config(model_file_path, params_file_path) RuntimeError: (NotFound) Cannot open file C:\Users\田龙飞\.paddlehub\modules\chinese_text_detection_db_mobile\inference_model\model.pdmodel, please confirm whether the file is normal. [Hint: Expected paddle::inference::IsFileExists(prog_file_) == true, but received paddle::inference::IsFileExists(prog_file_):0 != true:1.] (at ..\paddle\fluid\inference\api\analysis_config.cc:117)
07-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值