一、环境准备

开始使用_飞桨-源于产业实践的开源深度学习平台

快速安装:

 检查是否安装成功:

pip install paddlehub -i https://pypi.tuna.tsinghua.edu.cn/simple
#该Module依赖于第三方库shapely、pyclipper,使用该Module之前,请先安装shapely、pyclipper
pip install shapely -i https://pypi.tuna.tsinghua.edu.cn/simple 
pip install pyclipper -i https://pypi.tuna.tsinghua.edu.cn/simple 

 PaddleHub一键OCR中文识别(超轻量8.1M模型,火爆) - 飞桨AI Studio


import matplotlib.pyplot as plt 
import matplotlib.image as mpimg 

# 待预测图片
test_img_path = ["./advertisement.jpg", "./pics.jpg", "./identity_card.jpg", "./express.jpg", "./railway_ticket.jpg"]

# 展示其中广告信息图片
img1 = mpimg.imread(test_img_path[0]) 
plt.figure(figsize=(10,10))
plt.imshow(img1) 
plt.axis('off') 
plt.show()

 

二、加载预训练模型

import paddlehub as hub

# 加载移动端预训练模型
# ocr = hub.Module(name="chinese_ocr_db_crnn_mobile")
# 服务端可以加载大模型,效果更好 
ocr = hub.Module(name="chinese_ocr_db_crnn_server") 

三、预测

 

import cv2

# 读取测试文件夹test.txt中的照片路径
np_images =[cv2.imread(path+image_path) for image_path in test_img_path] 
results = ocr.recognize_text(
                    images=np_images,         # 图片数据,ndarray.shape 为 [H, W, C],BGR格式;
                    use_gpu=False,            # 是否使用 GPU;若使用GPU,请先设置CUDA_VISIBLE_DEVICES环境变量
                    output_dir='ocr_result',  # 图片的保存路径,默认设为 ocr_result;
                    visualization=True,       # 是否将识别结果保存为图片文件;
                    box_thresh=0.5,           # 检测文本框置信度的阈值;
                    text_thresh=0.5)          # 识别中文文本置信度的阈值;

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'])

recognize_text()接口返回结果results说明:

  • results (list[dict]): 识别结果的列表,列表中每一个元素为 dict,各字段为:
    • data (list[dict]): 识别文本结果,列表中每一个元素为 dict,各字段为:
      • text(str): 识别得到的文本
      • confidence(float): 识别文本结果置信度
      • text_box_position(list): 文本框在原图中的像素坐标,4*2的矩阵,依次表示文本框左下、右下、右上、左上顶点的坐标 如果无识别结果则data为[]
    • save_path (str, optional): 识别结果的保存路径,如不保存图片则save_path为''

四、服务部署

第一步:启动PaddleHub Serving

 

第二步:发送预测请求

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("D:/OCR/MMOCR/mmocr/demo/highway.jpg"))]}
headers = {"Content-type": "application/json"}
url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_server"
r = requests.post(url=url, headers=headers, data=json.dumps(data))

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

 

 参考:飞桨PaddlePaddle-源于产业实践的开源深度学习平台

GitHub 加速计划 / pa / PaddleOCR
41.51 K
7.59 K
下载
Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)
最近提交(Master分支:1 个月前 )
269e5b8f * Add formula recognition in ppstructure,Convert PDF to markdown file * Fix bug in converting to doc in formula recognition * modify time * Correct spelling errors in args_formula 2 天前
362103bd 3 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐