1.安装3.8以上python

我安装的是 python-3.9.6-amd64.exe


2.更新pip

pip3 install --upgrade pip -i https://mirror.baidu.com/pypi/simple

3.安装paddle

pip3 install paddlepaddle==2.1.2 -i https://mirror.baidu.com/pypi/simple


4.安装依赖

pip3 install  shapely                              -i https://mirror.baidu.com/pypi/simple
pip3 install  scikit-image                         -i https://mirror.baidu.com/pypi/simple
pip3 install  imgaug                               -i https://mirror.baidu.com/pypi/simple
pip3 install  pyclipper                            -i https://mirror.baidu.com/pypi/simple
pip3 install  lmdb                                 -i https://mirror.baidu.com/pypi/simple
pip3 install  tqdm                                 -i https://mirror.baidu.com/pypi/simple
pip3 install  numpy                                -i https://mirror.baidu.com/pypi/simple
pip3 install  visualdl                             -i https://mirror.baidu.com/pypi/simple

(安装失败,跳过,不影响使用) pip3 install  python-Levenshtein                   -i https://mirror.baidu.com/pypi/simple

pip3 install  opencv-contrib-python      -i https://mirror.baidu.com/pypi/simple
pip3 install  lxml                                 -i https://mirror.baidu.com/pypi/simple
pip3 install  premailer                            -i https://mirror.baidu.com/pypi/simple
pip3 install  openpyxl                             -i https://mirror.baidu.com/pypi/simple


5.安装paddlehub

pip3 install paddlehub==2.1.0 --upgrade -i https://pypi.tuna.tsinghua.edu.cn/simple

6.安装模块

拉取源码:https://gitee.com/paddlepaddle/PaddleOCR.git

按照源码提示安装推理模型,并放在源码目录内 如:PaddleOCR/inference

解压的时候注意要用7z打开,看到文件夹之后再拖出来

https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_infer.tar

https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar

https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_rec_infer.tar

# 进入源码根目录

# 安装检测服务模块:  
hub install deploy/hubserving/ocr_det/

# 或,安装分类服务模块:  
hub install deploy/hubserving/ocr_cls/

# 或,安装识别服务模块:  
hub install deploy/hubserving/ocr_rec/

# 或,安装检测+识别串联服务模块:  
hub install deploy/hubserving/ocr_system/


7. 启动服务

hub serving start -m ocr_system -p 8866


8. 调用接口

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

# 打印预测结果
print(r.json()["results"])
@Test
public void imgtest(){
    String imgBase64 = Base64.encode(new File("D:\\桌面\\测试资源\\ocrtest\\1.png"));
    HttpRequest request = HttpUtil.createPost("http://10.2.14.193:8867/predict/ocr_system");
    request.header("Content-type","application/json");
    
    Map<String, Object> map = new HashMap<>();
    map.put("images", new String[]{imgBase64});
    request.body(JSONUtil.toJsonStr(map));
    
    HttpResponse response = request.execute();
    String result = response.body();
    
    System.out.println(result);

}

返回结果:
15:50:27.524 [main] INFO com.cqliving.boot.cms.sso.ppocr.PPOCRTest - 耗时:[3859ms]
{"msg":"","results":[[{"confidence":0.9983974695205688,"text":"姓名徐乐","text_region":[[91,82],[267,81],[267,118],[92,119]]},{"confidence":0.9991212487220764,"text":"性别男","text_region":[[92,145],[209,148],[208,179],[91,175]]},{"confidence":0.9986091256141663,"text":"民族汉","text_region":[[258,148],[356,150],[355,177],[258,174]]},{"confidence":0.9952556490898132,"text":"1966年11月2日","text_region":[[177,207],[418,207],[418,233],[177,233]]},{"confidence":0.9881536960601807,"text":"住址","text_region":[[95,266],[160,266],[160,290],[95,290]]},{"confidence":0.9321985244750977,"text":"安徽省宿州市通桥区朱仙","text_region":[[179,268],[483,272],[483,298],[179,294]]},{"confidence":0.9909898042678833,"text":"庄镇","text_region":[[181,307],[235,307],[235,336],[181,336]]},{"confidence":0.9994156956672668,"text":"公民身份号码","text_region":[[95,418],[259,420],[259,444],[94,442]]},{"confidence":0.9982438683509827,"text":"652901196611026716","text_region":[[292,420],[700,424],[700,450],[291,446]]}]],"status":"000"}

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐