利用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"])

更多模型

更多模型可在官网查看

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值