如何在Windows上一键部署PaddleOCR的WebAPI服务

PaddleOCR旨在打造一套丰富、领先、且实用的OCR工具库,助力开发者训练出更好的模型,并应用落地。

官方​开源项目地址:PaddlePaddle/PaddleOCR: 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) (github.com)

一定会有小伙伴们看完不知道如何部署与应用,怎么才能融入到自己的产品或项目中​去。PaddleOCR是开源、免费、可离线部署的OC​R。

本文将介绍如何在windows平台上,自动化部署webAPI,并给出示例如何使用​。

   第一步,要安装​Python环境。​下载地址:Download Python | Python.org

建议选择安装3.6~3.9版本,然后为了pip下载速度,把下面的文本内容,保存为pip.ini文件,存放到你的python安装目录下,​例如:C:\Users\username\AppData\Local\Programs\Python\Python39


[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
[install]
trusted-host=mirrors.aliyun.com
第二步,把下面的批处理文件文本内容,保存为PaddleOCRWebAPI.bat文件,存放到你的电脑上任意目录下,然后双击运行,不出意外,你的PaddleOCR的WebAPI就部署​成功啦。
@echo off
COLOR 2F
title 公众号:明月心技术学堂
echo 本脚本演示如何在window境下一键部署PaddleOCR的WEBAPI功能。
echo 待脚本执行完后,将自动开一个OCR服务。url=127.0.0.1:5000/PaddleOCR/DetectText
echo 检测PYTHON安装
python -V
python -m pip install --upgrade pip

pip3 install opencv-python==4.5.5.64
echo  开始安装PaddleOCR
pip3 install  paddleocr

echo 安装PaddleOCR的依赖预测库
pip3 install paddlepaddle
echo 安装Flask
pip3 install Flask
echo 安装DateTime
pip3 install DateTime

set port=5000
set filename=PaddleOCRAPI.py
@echo off
del %filename%
echo import io >> %filename%
echo from pickle import DICT >> %filename%
echo import paddleocr >> %filename%
echo import json >> %filename%
echo import base64 >> %filename%
echo import DateTime >> %filename%
echo from flask import Flask, request,jsonify   >> %filename%
echo import numpy as np >> %filename%
echo from PIL import Image >> %filename%
echo app=Flask(__name__) >> %filename%
echo app.config['JSON_AS_ASCII']=False >> %filename%
echo @app.route("/PaddleOCR/DetectText",methods=["POST"]) >> %filename%
echo def PaddleOCR(): >> %filename%
echo     if(request.data==""): >> %filename%
echo         return APIResult.Error("request data is null") >> %filename%
echo     data=json.loads(request.data) >> %filename%
echo     imgbyte=base64.b64decode(data) >> %filename%
echo     image=io.BytesIO(imgbyte) >> %filename%
echo     temp= Image.open(image) >> %filename%
echo     img=np.array(temp)[:,:,:3] >> %filename%
echo     info= ppocr.ocr(img) >> %filename%
echo     result={"TextBlocks":[]} >> %filename%
echo     for textblocks in info: >> %filename%
echo         textBlock={"Points":[],"Text":""} >> %filename%
echo         for tk in textblocks[0]: >> %filename%
echo               point={"x":str(tk[0]),"y":str(tk[1])} >> %filename%
echo               textBlock["Points"].append(point)  >> %filename%
echo         textBlock["Text"]=textblocks[1][0] >> %filename%
echo         result["TextBlocks"].append(textBlock) >> %filename%
echo     print(result) >> %filename%
echo     return jsonify(result) >> %filename%
echo     
echo def main(): >> %filename%
echo     global ppocr; >> %filename%
echo     ppocr=paddleocr.PaddleOCR(use_gpu=False); >> %filename%
echo     app.run(debug=True,host="0.0.0.0",port=%port%) >> %filename%
echo if __name__=="__main__": >> %filename%
echo     main(); >> %filename%
call %filename%

  在批处理执行如果遇到提示 core_avx,找不到指定的模块,这是应为vc++2017运行环境没有安装,你可以加入后面的QQ群,获取补丁文件,把【vc2017库文件】文件内的所有文件复制到Python安装目录下的【\Lib\site-packages\paddle\libs】的文件夹既可​。

 

 

上述OCR的服务启动以后,小伙伴是不是非常想试试看看OCR识别的结果呢​?

接下来介绍如何使用API,把下的文本内容另存为一个ClientPython.py文件,并把你想识别文字的图片,放在image文件夹下,双击运行保存的ClientPython.py文件,就可以看到识别的结果啦​!

import requests
import os
import base64
def main():
   imagepath=os.path.abspath('.')+"\\image\\"
   imagefiles=os.listdir(imagepath)
   for image in imagefiles:
       imagefile=imagepath+image
       with  open(imagefile,"rb") as fs:
          imagebase64=str(base64.b64encode(fs.read()),"utf-8")
          headers = { 'Content-Type': 'application/json'}
          response = requests.post('http://127.0.0.1:5000/PaddleOCR/DetectText', headers=headers, json=imagebase64)
          print(response.content) 
if __name__=="__main__":
    main();

  

​小伙伴们学会了吗?快来动手试试吧!

这里,再介绍一下C#如何调用WebAPI,看看C#如何使用,创建控制台程序,分别复制一下代码,同样在运行目录下准备好你的image文件夹​。

 

public class WebAPIClient
    {
        static string url = "http://localhost:5000/PaddleOCR/DetectText";
        public static   string DetectText(string base64)
        {
            RestClient client = new RestClient(url);
            RestRequest req = new RestRequest() { Method = Method.Post };
            req.AddBody(base64);
            req.AddHeader("Content-Type", "application/json");
            var resp = client.ExecuteAsync(req);
​
            if (resp.Result.StatusCode != HttpStatusCode.OK)
            {
                throw new Exception("HttpError:" + resp.Result.StatusDescription);
            }
            return resp.Result.Content;
        }
​
    }

  

string[] extensions = new string[] { ".jpg", ".bmp", ".jpeg", ".png", ".tif", ".tiff" };
string imageroot = Environment.CurrentDirectory + "//image";
if (!Directory.Exists(imageroot)) Directory.CreateDirectory(imageroot);
DirectoryInfo directoryInfo = new DirectoryInfo(imageroot);
var files = directoryInfo.GetFiles("*.*");
​
DateTime dt1;
DateTime dt2;
foreach (var file in files)
{
       
    if (!extensions.Contains(file.Extension.ToLower())) continue;
      byte[] imagebyte = File.ReadAllBytes(file.FullName);
     string base64 = Convert.ToBase64String(imagebyte);
    dt1 = DateTime.Now;
    var s = WebAPIClient.DetectText(base64);
     
    dt2 = DateTime.Now;
    Console.WriteLine((dt2-dt1).TotalMilliseconds);
  
   Console.WriteLine(s);

欢迎关注公众号了解更多​内容。

更多代码前往开源项目地址了解:PaddleOCRWebAPI: PaddleOCRWebAPI是 PaddleOCRSharp的web应用。 项目使用VS2022及以上版本编译,使用net6.0框架。模型默认带有一个轻量版中英文V3模型库。 (gitee.com)

 

posted on 2022-09-24 10:25  饶玉田  阅读(2527)  评论(1编辑  收藏  举报