diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..4d2b306 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM python:3.8-buster + +RUN mkdir /app + +COPY ./*.txt ./*.py ./*.sh ./*.onnx /app/ + + +RUN cd /app \ + && python3 -m pip install --upgrade pip -i https://pypi.douban.com/simple/\ + && pip3 install --no-cache-dir -r requirements.txt --extra-index-url https://pypi.douban.com/simple/ \ + && rm -rf /tmp/* && rm -rf /root/.cache/* \ + && sed -i 's#http://deb.debian.org#http://mirrors.aliyun.com/#g' /etc/apt/sources.list\ + && apt-get --allow-releaseinfo-change update && apt install libgl1-mesa-glx -y + +WORKDIR /app + +ENTRYPOINT ["sh", "entrypoint.sh"] \ No newline at end of file diff --git a/README.md b/README.md index 6376bcb..93b0a51 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,72 @@ # ocr_api_server 使用ddddocr的最简api搭建项目,支持docker + +**建议python版本3.7-3.9 64位** + +再有不好好看文档的我就不管了啊!!! + +# 运行方式 + +## 最简单运行方式 + +```shell +# 安装依赖 +pip install -r requirements.txt -i https://pypi.douban.com/simple + +# 运行 可选参数如下 +# --port 9898 指定端口,默认为9898 +# --ocr 开启ocr模块 默认开启 +# --old 只有ocr模块开启的情况下生效 默认不开启 +# --det 开启目标检测模式 + +# 最简单运行方式,只开启ocr模块并以新模型计算 +python ocr_server.py --port 9898 --ocr + +# 开启ocr模块并使用旧模型计算 +python ocr_server.py --port 9898 --ocr -old + +# 只开启目标检测模块 +python ocr_server.py --port 9898 --det + +# 同时开启ocr模块以及目标检测模块 +python ocr_server.py --port 9898 --ocr --det + +# 同时开启ocr模块并使用旧模型计算以及目标检测模块 +python ocr_server.py --port 9898 --ocr --old --det + +``` + +## docker运行方式(目测只能在Linux下部署) + +```shell +git clone https://github.com/sml2h3/ocr_api_server.git +# docker怎么安装?百度吧 + +cd ocr_api_server + +# 修改entrypoint.sh中的参数,具体参数往上翻,默认9898端口,同时开启ocr模块以及目标检测模块 + +# 编译镜像 +docker build -t ocr_server:v1 . + +# 运行镜像 +docker run -p 9898:9898 -d ocr_server:v1 + +``` + +# 接口 + +**具体请看test_api.py文件** + +```python +# 1、测试是否启动成功,可以通过直接GET访问http://{host}:{port}/ping来测试,如果返回pong则启动成功 + +# 2、OCR请求 + +# resp = requests.post("http://{host}:{port}/ocr", files={'image': image_bytes}) + +# 3、目标检测请求 + +# resp = requests.post("http://{host}:{port}/det", files={'image': image_bytes}) + +``` diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..8167127 --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +python3 ocr_server.py --port 9898 --ocr --det diff --git a/ocr_server.py b/ocr_server.py new file mode 100644 index 0000000..4bebfbb --- /dev/null +++ b/ocr_server.py @@ -0,0 +1,84 @@ +# encoding=utf-8 +import argparse +import json + +import ddddocr +from flask import Flask, request + +parser = argparse.ArgumentParser(description="使用ddddocr搭建的最简api服务") +parser.add_argument("-p", "--port", type=int, default=9898) +parser.add_argument("--ocr", action="store_true", help="开启ocr识别") +parser.add_argument("--old", action="store_true", help="OCR是否启动旧模型") +parser.add_argument("--det", action="store_true", help="开启目标检测") + +args = parser.parse_args() + +app = Flask(__name__) + + +class Server(object): + def __init__(self, ocr=True, det=False, old=False): + self.ocr_option = ocr + self.det_option = det + self.old_option = old + self.ocr = None + self.det = None + if self.ocr_option: + print("ocr模块开启") + if self.old_option: + print("使用OCR旧模型启动") + self.ocr = ddddocr.DdddOcr(old=True) + else: + print("使用OCR新模型启动,如需要使用旧模型,请额外添加参数 --old开启") + self.ocr = ddddocr.DdddOcr() + else: + print("ocr模块未开启,如需要使用,请使用参数 --ocr开启") + if self.det_option: + print("目标检测模块开启") + self.det = ddddocr.DdddOcr(det=True) + else: + print("目标检测模块未开启,如需要使用,请使用参数 --det开启") + + + def classification(self, img: bytes): + if self.ocr_option: + return self.ocr.classification(img) + else: + raise Exception("ocr模块未开启") + + def detection(self, img: bytes): + if self.det_option: + return self.det.detection(img) + else: + raise Exception("目标检测模块模块未开启") + + +server = Server(ocr=args.ocr, det=args.det, old=args.old) + + +@app.route('/ocr', methods=['POST']) +def ocr(): + try: + img = request.files.get('image').read() + r = server.classification(img) + return json.dumps({"status": "200", "result": str(r), "msg": ""}) + except Exception as e: + return json.dumps({"status": "500", "result": "", "msg": str(e)}) + +@app.route('/det', methods=['POST']) +def det(): + try: + img = request.files.get('image').read() + r = server.detection(img) + return json.dumps({"status": "200", "result": r, "msg": ""}) + except Exception as e: + return json.dumps({"status": "500", "result": "", "msg": str(e)}) + + +@app.route('/ping', methods=['GET']) +def ping(): + return "pong" + + +if __name__ == '__main__': + app.run(host="0.0.0.0", port=args.port) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..a8661fa --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +ddddocr>=1.3.1 +flask \ No newline at end of file diff --git a/test.jpg b/test.jpg new file mode 100644 index 0000000..d5894d2 Binary files /dev/null and b/test.jpg differ diff --git a/test_api.py b/test_api.py new file mode 100644 index 0000000..bc8279d --- /dev/null +++ b/test_api.py @@ -0,0 +1,40 @@ +#!/usr/bin/python3.6 +# -*- coding: utf-8 -*- +# +# Copyright (C) 2021 # +# @Time : 2022/1/6 23:28 +# @Author : sml2h3 +# @Email : sml2h3@gmail.com +# @File : test_api.py +# @Software: PyCharm +import requests + +# ******************OCR识别部分开始****************** +# 目标检测就把ocr改成det,其他相同 +api_url = "http://10.0.20.198:9898/ocr" + +# 方式一 +# file = open(r'test.jpg', 'rb').read() +# +# resp = requests.post(api_url, files={'image': file}) +# print(resp.text) + +# 方式二 + +# 获取验证码图片 +# headers = { +# "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", +# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4195.1 Safari/537.36" +# } +# resp = requests.get('https://data.gdcic.net/Dop/CheckCode.aspx?codemark=408.15173910730016', headers=headers, verify=False) +# captcha_img = resp.content +# +# 识别 +# resp = requests.post(api_url, files={'image': captcha_img}) +# print('验证码结果', resp.text) +# +# # 保存验证码图片以供验证 +# with open('captcha.jpg', 'wb') as f: +# f.write(captcha_img) + +# ******************OCR识别部分开始******************