日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PaddleOCR加载chinese_ocr_db_crnn_modile模型进行中英文混合预测(Http服务)实践

發布時間:2025/5/22 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PaddleOCR加载chinese_ocr_db_crnn_modile模型进行中英文混合预测(Http服务)实践 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1. 環境搭建

參考:《PaddleOCR加載chinese_ocr_db_crnn_server模型進行中英文混合預測(命令行)實踐》

2. 服務端部署
hub serving start -m chinese_ocr_db_crnn_mobile -p 8866
3. 客戶端訪問
# coding: utf8 import requests import json import cv2 import base64def 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_ocr_db_crnn_mobile" r = requests.post(url=url, headers=headers, data=json.dumps(data))# 打印預測結果 print(r.json()["results"])
4. 將圖像識別服務部署到Flask上

先參考《Flask開發實踐》或《Flask入門》
在此基礎上進行了以下修改:

文件routes.py增加的內容

import base64 import json import os import cv2 import requests from werkzeug.utils import secure_filename# OCR測試 @webapp.route('/ocr', methods=['POST', 'GET']) def ocr():basepath = os.path.dirname(__file__) # 當前文件所在路徑print(request.method)if request.method == 'POST':f = request.files['file']upload_path = os.path.join(basepath, 'static/uploads', secure_filename(f.filename)) # 注意:沒有的文件夾要先創建f.save(upload_path)return redirect(url_for('ocr_result', pic_name=f.filename))else:return render_template('ocr.html')# OCR測試結果 @webapp.route('/ocr/<pic_name>', methods=['POST', 'GET']) def ocr_result(pic_name):basepath = os.path.dirname(__file__) # 當前文件所在路徑print(request.method)if pic_name != "":upload_path = os.path.join(basepath, 'static/uploads', secure_filename(pic_name))data = {'images': [cv2_to_base64(cv2.imread(upload_path))]}headers = {"Content-type": "application/json"}url = "http://127.0.0.1:8866/predict/chinese_ocr_db_crnn_mobile"# 圖像識別服務: hub.exe serving start -m chinese_ocr_db_crnn_mobile -p 8866r = requests.post(url=url, headers=headers, data=json.dumps(data))# 打印預測結果results = r.json()["results"][0]["data"]return render_template('ocr.html', results=results, file=upload_path.replace(basepath, ''))else:return render_template('ocr.html')def cv2_to_base64(image):data = cv2.imencode('.jpg', image)[1]return base64.b64encode(data.tostring()).decode('utf8')

新建模板文件ocr.html

<!DOCTYPE html> <html > <head><meta charset="UTF-8"><title>圖像識別測試頁</title> </head> <body><h1>圖像識別測試頁</h1><form action="/ocr" enctype="multipart/form-data" method="post"><input type="file" name="file"><input type="submit" value="提交"></form><h1>識別結果</h1><img src="{{ file }}">{% for result in results %}<table><tr valign="top"><td>[置信度] {{ (result.confidence*100) | round(2) }}%, [文字內容] {{ result.text }}</td></tr></table>{% endfor %}{{ result }} </body> </html>
5. 運行效果

先上傳圖片

提交后顯示圖片內容和識別結果

【參考文檔】
《PaddleHub一鍵OCR中文識別》

總結

以上是生活随笔為你收集整理的PaddleOCR加载chinese_ocr_db_crnn_modile模型进行中英文混合预测(Http服务)实践的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。