Spaces:
Running
Running
Susus
commited on
Upload 12 files
Browse files- Dockerfile +16 -0
- LICENSE +201 -0
- README.md +85 -9
- compare_bg.jpg +0 -0
- compare_target.jpg +0 -0
- match_bg.png +0 -0
- match_target.png +0 -0
- ocr_server.py +126 -0
- requirements.txt +2 -0
- test.jpg +0 -0
- test_api.py +111 -0
- test_calc.png +0 -0
Dockerfile
ADDED
@@ -0,0 +1,16 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
FROM python:3.8-slim-buster
|
2 |
+
|
3 |
+
RUN mkdir /app
|
4 |
+
|
5 |
+
COPY ./*.txt ./*.py ./*.sh ./*.onnx /app/
|
6 |
+
|
7 |
+
|
8 |
+
RUN cd /app \
|
9 |
+
&& python3 -m pip install --upgrade pip\
|
10 |
+
&& pip3 install --no-cache-dir -r requirements.txt\
|
11 |
+
&& rm -rf /tmp/* && rm -rf /root/.cache/* \
|
12 |
+
&& apt-get --allow-releaseinfo-change update && apt install libgl1-mesa-glx libglib2.0-0 -y
|
13 |
+
|
14 |
+
WORKDIR /app
|
15 |
+
|
16 |
+
CMD ["python3", "ocr_server.py", "--port", "9898", "--ocr", "--det"]
|
LICENSE
ADDED
@@ -0,0 +1,201 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
Apache License
|
2 |
+
Version 2.0, January 2004
|
3 |
+
http://www.apache.org/licenses/
|
4 |
+
|
5 |
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
6 |
+
|
7 |
+
1. Definitions.
|
8 |
+
|
9 |
+
"License" shall mean the terms and conditions for use, reproduction,
|
10 |
+
and distribution as defined by Sections 1 through 9 of this document.
|
11 |
+
|
12 |
+
"Licensor" shall mean the copyright owner or entity authorized by
|
13 |
+
the copyright owner that is granting the License.
|
14 |
+
|
15 |
+
"Legal Entity" shall mean the union of the acting entity and all
|
16 |
+
other entities that control, are controlled by, or are under common
|
17 |
+
control with that entity. For the purposes of this definition,
|
18 |
+
"control" means (i) the power, direct or indirect, to cause the
|
19 |
+
direction or management of such entity, whether by contract or
|
20 |
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
21 |
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
22 |
+
|
23 |
+
"You" (or "Your") shall mean an individual or Legal Entity
|
24 |
+
exercising permissions granted by this License.
|
25 |
+
|
26 |
+
"Source" form shall mean the preferred form for making modifications,
|
27 |
+
including but not limited to software source code, documentation
|
28 |
+
source, and configuration files.
|
29 |
+
|
30 |
+
"Object" form shall mean any form resulting from mechanical
|
31 |
+
transformation or translation of a Source form, including but
|
32 |
+
not limited to compiled object code, generated documentation,
|
33 |
+
and conversions to other media types.
|
34 |
+
|
35 |
+
"Work" shall mean the work of authorship, whether in Source or
|
36 |
+
Object form, made available under the License, as indicated by a
|
37 |
+
copyright notice that is included in or attached to the work
|
38 |
+
(an example is provided in the Appendix below).
|
39 |
+
|
40 |
+
"Derivative Works" shall mean any work, whether in Source or Object
|
41 |
+
form, that is based on (or derived from) the Work and for which the
|
42 |
+
editorial revisions, annotations, elaborations, or other modifications
|
43 |
+
represent, as a whole, an original work of authorship. For the purposes
|
44 |
+
of this License, Derivative Works shall not include works that remain
|
45 |
+
separable from, or merely link (or bind by name) to the interfaces of,
|
46 |
+
the Work and Derivative Works thereof.
|
47 |
+
|
48 |
+
"Contribution" shall mean any work of authorship, including
|
49 |
+
the original version of the Work and any modifications or additions
|
50 |
+
to that Work or Derivative Works thereof, that is intentionally
|
51 |
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
52 |
+
or by an individual or Legal Entity authorized to submit on behalf of
|
53 |
+
the copyright owner. For the purposes of this definition, "submitted"
|
54 |
+
means any form of electronic, verbal, or written communication sent
|
55 |
+
to the Licensor or its representatives, including but not limited to
|
56 |
+
communication on electronic mailing lists, source code control systems,
|
57 |
+
and issue tracking systems that are managed by, or on behalf of, the
|
58 |
+
Licensor for the purpose of discussing and improving the Work, but
|
59 |
+
excluding communication that is conspicuously marked or otherwise
|
60 |
+
designated in writing by the copyright owner as "Not a Contribution."
|
61 |
+
|
62 |
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
63 |
+
on behalf of whom a Contribution has been received by Licensor and
|
64 |
+
subsequently incorporated within the Work.
|
65 |
+
|
66 |
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
67 |
+
this License, each Contributor hereby grants to You a perpetual,
|
68 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
69 |
+
copyright license to reproduce, prepare Derivative Works of,
|
70 |
+
publicly display, publicly perform, sublicense, and distribute the
|
71 |
+
Work and such Derivative Works in Source or Object form.
|
72 |
+
|
73 |
+
3. Grant of Patent License. Subject to the terms and conditions of
|
74 |
+
this License, each Contributor hereby grants to You a perpetual,
|
75 |
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
76 |
+
(except as stated in this section) patent license to make, have made,
|
77 |
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
78 |
+
where such license applies only to those patent claims licensable
|
79 |
+
by such Contributor that are necessarily infringed by their
|
80 |
+
Contribution(s) alone or by combination of their Contribution(s)
|
81 |
+
with the Work to which such Contribution(s) was submitted. If You
|
82 |
+
institute patent litigation against any entity (including a
|
83 |
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
84 |
+
or a Contribution incorporated within the Work constitutes direct
|
85 |
+
or contributory patent infringement, then any patent licenses
|
86 |
+
granted to You under this License for that Work shall terminate
|
87 |
+
as of the date such litigation is filed.
|
88 |
+
|
89 |
+
4. Redistribution. You may reproduce and distribute copies of the
|
90 |
+
Work or Derivative Works thereof in any medium, with or without
|
91 |
+
modifications, and in Source or Object form, provided that You
|
92 |
+
meet the following conditions:
|
93 |
+
|
94 |
+
(a) You must give any other recipients of the Work or
|
95 |
+
Derivative Works a copy of this License; and
|
96 |
+
|
97 |
+
(b) You must cause any modified files to carry prominent notices
|
98 |
+
stating that You changed the files; and
|
99 |
+
|
100 |
+
(c) You must retain, in the Source form of any Derivative Works
|
101 |
+
that You distribute, all copyright, patent, trademark, and
|
102 |
+
attribution notices from the Source form of the Work,
|
103 |
+
excluding those notices that do not pertain to any part of
|
104 |
+
the Derivative Works; and
|
105 |
+
|
106 |
+
(d) If the Work includes a "NOTICE" text file as part of its
|
107 |
+
distribution, then any Derivative Works that You distribute must
|
108 |
+
include a readable copy of the attribution notices contained
|
109 |
+
within such NOTICE file, excluding those notices that do not
|
110 |
+
pertain to any part of the Derivative Works, in at least one
|
111 |
+
of the following places: within a NOTICE text file distributed
|
112 |
+
as part of the Derivative Works; within the Source form or
|
113 |
+
documentation, if provided along with the Derivative Works; or,
|
114 |
+
within a display generated by the Derivative Works, if and
|
115 |
+
wherever such third-party notices normally appear. The contents
|
116 |
+
of the NOTICE file are for informational purposes only and
|
117 |
+
do not modify the License. You may add Your own attribution
|
118 |
+
notices within Derivative Works that You distribute, alongside
|
119 |
+
or as an addendum to the NOTICE text from the Work, provided
|
120 |
+
that such additional attribution notices cannot be construed
|
121 |
+
as modifying the License.
|
122 |
+
|
123 |
+
You may add Your own copyright statement to Your modifications and
|
124 |
+
may provide additional or different license terms and conditions
|
125 |
+
for use, reproduction, or distribution of Your modifications, or
|
126 |
+
for any such Derivative Works as a whole, provided Your use,
|
127 |
+
reproduction, and distribution of the Work otherwise complies with
|
128 |
+
the conditions stated in this License.
|
129 |
+
|
130 |
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
131 |
+
any Contribution intentionally submitted for inclusion in the Work
|
132 |
+
by You to the Licensor shall be under the terms and conditions of
|
133 |
+
this License, without any additional terms or conditions.
|
134 |
+
Notwithstanding the above, nothing herein shall supersede or modify
|
135 |
+
the terms of any separate license agreement you may have executed
|
136 |
+
with Licensor regarding such Contributions.
|
137 |
+
|
138 |
+
6. Trademarks. This License does not grant permission to use the trade
|
139 |
+
names, trademarks, service marks, or product names of the Licensor,
|
140 |
+
except as required for reasonable and customary use in describing the
|
141 |
+
origin of the Work and reproducing the content of the NOTICE file.
|
142 |
+
|
143 |
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
144 |
+
agreed to in writing, Licensor provides the Work (and each
|
145 |
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
146 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
147 |
+
implied, including, without limitation, any warranties or conditions
|
148 |
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
149 |
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
150 |
+
appropriateness of using or redistributing the Work and assume any
|
151 |
+
risks associated with Your exercise of permissions under this License.
|
152 |
+
|
153 |
+
8. Limitation of Liability. In no event and under no legal theory,
|
154 |
+
whether in tort (including negligence), contract, or otherwise,
|
155 |
+
unless required by applicable law (such as deliberate and grossly
|
156 |
+
negligent acts) or agreed to in writing, shall any Contributor be
|
157 |
+
liable to You for damages, including any direct, indirect, special,
|
158 |
+
incidental, or consequential damages of any character arising as a
|
159 |
+
result of this License or out of the use or inability to use the
|
160 |
+
Work (including but not limited to damages for loss of goodwill,
|
161 |
+
work stoppage, computer failure or malfunction, or any and all
|
162 |
+
other commercial damages or losses), even if such Contributor
|
163 |
+
has been advised of the possibility of such damages.
|
164 |
+
|
165 |
+
9. Accepting Warranty or Additional Liability. While redistributing
|
166 |
+
the Work or Derivative Works thereof, You may choose to offer,
|
167 |
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
168 |
+
or other liability obligations and/or rights consistent with this
|
169 |
+
License. However, in accepting such obligations, You may act only
|
170 |
+
on Your own behalf and on Your sole responsibility, not on behalf
|
171 |
+
of any other Contributor, and only if You agree to indemnify,
|
172 |
+
defend, and hold each Contributor harmless for any liability
|
173 |
+
incurred by, or claims asserted against, such Contributor by reason
|
174 |
+
of your accepting any such warranty or additional liability.
|
175 |
+
|
176 |
+
END OF TERMS AND CONDITIONS
|
177 |
+
|
178 |
+
APPENDIX: How to apply the Apache License to your work.
|
179 |
+
|
180 |
+
To apply the Apache License to your work, attach the following
|
181 |
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
182 |
+
replaced with your own identifying information. (Don't include
|
183 |
+
the brackets!) The text should be enclosed in the appropriate
|
184 |
+
comment syntax for the file format. We also recommend that a
|
185 |
+
file or class name and description of purpose be included on the
|
186 |
+
same "printed page" as the copyright notice for easier
|
187 |
+
identification within third-party archives.
|
188 |
+
|
189 |
+
Copyright [yyyy] [name of copyright owner]
|
190 |
+
|
191 |
+
Licensed under the Apache License, Version 2.0 (the "License");
|
192 |
+
you may not use this file except in compliance with the License.
|
193 |
+
You may obtain a copy of the License at
|
194 |
+
|
195 |
+
http://www.apache.org/licenses/LICENSE-2.0
|
196 |
+
|
197 |
+
Unless required by applicable law or agreed to in writing, software
|
198 |
+
distributed under the License is distributed on an "AS IS" BASIS,
|
199 |
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
200 |
+
See the License for the specific language governing permissions and
|
201 |
+
limitations under the License.
|
README.md
CHANGED
@@ -1,9 +1,85 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# ocr_api_server
|
2 |
+
使用ddddocr的最简api搭建项目,支持docker
|
3 |
+
|
4 |
+
**建议python版本3.7-3.9 64位**
|
5 |
+
|
6 |
+
再有不好好看文档的我就不管了啊!!!
|
7 |
+
|
8 |
+
# 运行方式
|
9 |
+
|
10 |
+
## 最简单运行方式
|
11 |
+
|
12 |
+
```shell
|
13 |
+
# 安装依赖
|
14 |
+
pip install -r requirements.txt -i https://pypi.douban.com/simple
|
15 |
+
|
16 |
+
# 运行 可选参数如下
|
17 |
+
# --port 9898 指定端口,默认为9898
|
18 |
+
# --ocr 开启ocr模块 默认开启
|
19 |
+
# --old 只有ocr模块开启的情况下生效 默认不开启
|
20 |
+
# --det 开启目标检测模式
|
21 |
+
|
22 |
+
# 最简单运行方式,只开启ocr模块并以新模型计算
|
23 |
+
python ocr_server.py --port 9898 --ocr
|
24 |
+
|
25 |
+
# 开启ocr模块并使用旧模型计算
|
26 |
+
python ocr_server.py --port 9898 --ocr --old
|
27 |
+
|
28 |
+
# 只开启目标检测模块
|
29 |
+
python ocr_server.py --port 9898 --det
|
30 |
+
|
31 |
+
# 同时开启ocr模块以及目标检测模块
|
32 |
+
python ocr_server.py --port 9898 --ocr --det
|
33 |
+
|
34 |
+
# 同时开启ocr模块并使用旧模型计算以及目标检测模块
|
35 |
+
python ocr_server.py --port 9898 --ocr --old --det
|
36 |
+
|
37 |
+
```
|
38 |
+
|
39 |
+
## docker运行方式(目测只能在Linux下部署)
|
40 |
+
|
41 |
+
```shell
|
42 |
+
git clone https://github.com/sml2h3/ocr_api_server.git
|
43 |
+
# docker怎么安装?百度吧
|
44 |
+
|
45 |
+
cd ocr_api_server
|
46 |
+
|
47 |
+
# 修改entrypoint.sh中的参数,具体参数往上翻,默认9898端口,同时开启ocr模块以及目标检测模块
|
48 |
+
|
49 |
+
# 编译镜像
|
50 |
+
docker build -t ocr_server:v1 .
|
51 |
+
|
52 |
+
# 运行镜像
|
53 |
+
docker run -p 9898:9898 -d ocr_server:v1
|
54 |
+
|
55 |
+
```
|
56 |
+
|
57 |
+
# 接口
|
58 |
+
|
59 |
+
**具体请看test_api.py文件**
|
60 |
+
|
61 |
+
```python
|
62 |
+
# 1、测试是否启动成功,可以通过直接GET访问http://{host}:{port}/ping来测试,如果返回pong则启动成功
|
63 |
+
|
64 |
+
# 2、OCR/目标检测请求接口格式:
|
65 |
+
|
66 |
+
# http://{host}:{port}/{opt}/{img_type}/{ret_type}
|
67 |
+
# opt:操作类型 ocr=OCR det=目标检测 slide=滑块(match和compare两种算法,默认为compare)
|
68 |
+
# img_type: 数据类型 file=文件上传方式 b64=base64(imgbyte)方式 默认为file方式
|
69 |
+
# ret_type: 返回类型 json=返回json(识别出错会在msg里返回错误信息) text=返回文本格式(识别出错时回直接返回空文本)
|
70 |
+
|
71 |
+
# 例子:
|
72 |
+
|
73 |
+
# OCR请求
|
74 |
+
# resp = requests.post("http://{host}:{port}/ocr/file", files={'image': image_bytes})
|
75 |
+
# resp = requests.post("http://{host}:{port}/ocr/b64/text", data=base64.b64encode(file).decode())
|
76 |
+
|
77 |
+
# 目标检测请求
|
78 |
+
# resp = requests.post("http://{host}:{port}/det/file", files={'image': image_bytes})
|
79 |
+
# resp = requests.post("http://{host}:{port}/det/b64/json", data=base64.b64encode(file).decode())
|
80 |
+
|
81 |
+
# 滑块识别请求
|
82 |
+
# resp = requests.post("http://{host}:{port}/slide/match/file", files={'target_img': target_bytes, 'bg_img': bg_bytes})
|
83 |
+
# jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str})
|
84 |
+
# resp = requests.post("http://{host}:{port}/slide/compare/b64", files=base64.b64encode(jsonstr.encode()).decode())
|
85 |
+
```
|
compare_bg.jpg
ADDED
![]() |
compare_target.jpg
ADDED
![]() |
match_bg.png
ADDED
![]() |
match_target.png
ADDED
![]() |
ocr_server.py
ADDED
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# encoding=utf-8
|
2 |
+
import argparse
|
3 |
+
import base64
|
4 |
+
import json
|
5 |
+
|
6 |
+
import ddddocr
|
7 |
+
from flask import Flask, request
|
8 |
+
|
9 |
+
parser = argparse.ArgumentParser(description="使用ddddocr搭建的最简api服务")
|
10 |
+
parser.add_argument("-p", "--port", type=int, default=9898)
|
11 |
+
parser.add_argument("--ocr", action="store_true", help="开启ocr识别")
|
12 |
+
parser.add_argument("--old", action="store_true", help="OCR是否启动旧模型")
|
13 |
+
parser.add_argument("--det", action="store_true", help="开启目标检测")
|
14 |
+
|
15 |
+
args = parser.parse_args()
|
16 |
+
|
17 |
+
app = Flask(__name__)
|
18 |
+
|
19 |
+
|
20 |
+
class Server(object):
|
21 |
+
def __init__(self, ocr=True, det=False, old=False):
|
22 |
+
self.ocr_option = ocr
|
23 |
+
self.det_option = det
|
24 |
+
self.old_option = old
|
25 |
+
self.ocr = None
|
26 |
+
self.det = None
|
27 |
+
if self.ocr_option:
|
28 |
+
print("ocr模块开启")
|
29 |
+
if self.old_option:
|
30 |
+
print("使用OCR旧模型启动")
|
31 |
+
self.ocr = ddddocr.DdddOcr(old=True)
|
32 |
+
else:
|
33 |
+
print("使用OCR新模型启动,如需要使用旧模型,请额外添加参数 --old开启")
|
34 |
+
self.ocr = ddddocr.DdddOcr()
|
35 |
+
else:
|
36 |
+
print("ocr模块未开启,如需要使用,请使用参数 --ocr开启")
|
37 |
+
if self.det_option:
|
38 |
+
print("目标检测模块开启")
|
39 |
+
self.det = ddddocr.DdddOcr(det=True)
|
40 |
+
else:
|
41 |
+
print("目标检测模块未开启,如需要使用,请使用参数 --det开启")
|
42 |
+
|
43 |
+
def classification(self, img: bytes):
|
44 |
+
if self.ocr_option:
|
45 |
+
return self.ocr.classification(img)
|
46 |
+
else:
|
47 |
+
raise Exception("ocr模块未开启")
|
48 |
+
|
49 |
+
def detection(self, img: bytes):
|
50 |
+
if self.det_option:
|
51 |
+
return self.det.detection(img)
|
52 |
+
else:
|
53 |
+
raise Exception("目标检测模块模块未开启")
|
54 |
+
|
55 |
+
def slide(self, target_img: bytes, bg_img: bytes, algo_type: str):
|
56 |
+
dddd = self.ocr or self.det or ddddocr.DdddOcr(ocr=False)
|
57 |
+
if algo_type == 'match':
|
58 |
+
return dddd.slide_match(target_img, bg_img)
|
59 |
+
elif algo_type == 'compare':
|
60 |
+
return dddd.slide_comparison(target_img, bg_img)
|
61 |
+
else:
|
62 |
+
raise Exception(f"不支持的滑块算法类型: {algo_type}")
|
63 |
+
|
64 |
+
server = Server(ocr=args.ocr, det=args.det, old=args.old)
|
65 |
+
|
66 |
+
|
67 |
+
def get_img(request, img_type='file', img_name='image'):
|
68 |
+
if img_type == 'b64':
|
69 |
+
img = base64.b64decode(request.get_data()) #
|
70 |
+
try: # json str of multiple images
|
71 |
+
dic = json.loads(img)
|
72 |
+
img = base64.b64decode(dic.get(img_name).encode())
|
73 |
+
except Exception as e: # just base64 of single image
|
74 |
+
pass
|
75 |
+
else:
|
76 |
+
img = request.files.get(img_name).read()
|
77 |
+
return img
|
78 |
+
|
79 |
+
|
80 |
+
def set_ret(result, ret_type='text'):
|
81 |
+
if ret_type == 'json':
|
82 |
+
if isinstance(result, Exception):
|
83 |
+
return json.dumps({"status": 200, "result": "", "msg": str(result)})
|
84 |
+
else:
|
85 |
+
return json.dumps({"status": 200, "result": result, "msg": ""})
|
86 |
+
# return json.dumps({"succ": isinstance(result, str), "result": str(result)})
|
87 |
+
else:
|
88 |
+
if isinstance(result, Exception):
|
89 |
+
return ''
|
90 |
+
else:
|
91 |
+
return str(result).strip()
|
92 |
+
|
93 |
+
|
94 |
+
@app.route('/<opt>/<img_type>', methods=['POST'])
|
95 |
+
@app.route('/<opt>/<img_type>/<ret_type>', methods=['POST'])
|
96 |
+
def ocr(opt, img_type='file', ret_type='text'):
|
97 |
+
try:
|
98 |
+
img = get_img(request, img_type)
|
99 |
+
if opt == 'ocr':
|
100 |
+
result = server.classification(img)
|
101 |
+
elif opt == 'det':
|
102 |
+
result = server.detection(img)
|
103 |
+
else:
|
104 |
+
raise f"<opt={opt}> is invalid"
|
105 |
+
return set_ret(result, ret_type)
|
106 |
+
except Exception as e:
|
107 |
+
return set_ret(e, ret_type)
|
108 |
+
|
109 |
+
@app.route('/slide/<algo_type>/<img_type>', methods=['POST'])
|
110 |
+
@app.route('/slide/<algo_type>/<img_type>/<ret_type>', methods=['POST'])
|
111 |
+
def slide(algo_type='compare', img_type='file', ret_type='text'):
|
112 |
+
try:
|
113 |
+
target_img = get_img(request, img_type, 'target_img')
|
114 |
+
bg_img = get_img(request, img_type, 'bg_img')
|
115 |
+
result = server.slide(target_img, bg_img, algo_type)
|
116 |
+
return set_ret(result, ret_type)
|
117 |
+
except Exception as e:
|
118 |
+
return set_ret(e, ret_type)
|
119 |
+
|
120 |
+
@app.route('/ping', methods=['GET'])
|
121 |
+
def ping():
|
122 |
+
return "pong"
|
123 |
+
|
124 |
+
|
125 |
+
if __name__ == '__main__':
|
126 |
+
app.run(host="0.0.0.0", port=args.port)
|
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
ddddocr>=1.4.7
|
2 |
+
flask
|
test.jpg
ADDED
![]() |
test_api.py
ADDED
@@ -0,0 +1,111 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/python3.6
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
#
|
4 |
+
# Copyright (C) 2021 #
|
5 |
+
# @Time : 2022/1/6 23:28
|
6 |
+
# @Author : sml2h3
|
7 |
+
# @Email : sml2h3@gmail.com
|
8 |
+
# @File : test_api.py
|
9 |
+
# @Software: PyCharm
|
10 |
+
import base64
|
11 |
+
import json
|
12 |
+
import requests
|
13 |
+
|
14 |
+
print(' ')
|
15 |
+
# ******************OCR识别部分开始******************
|
16 |
+
host = "http://127.0.0.1:9898"
|
17 |
+
# 目标检测就把ocr改成det,其他相同
|
18 |
+
# 方式一
|
19 |
+
file = open(r'test.jpg', 'rb').read()
|
20 |
+
# file = open(r'test_calc.png', 'rb').read()
|
21 |
+
|
22 |
+
|
23 |
+
api_url = f"{host}/ocr/file"
|
24 |
+
resp = requests.post(api_url, files={'image': file})
|
25 |
+
print(f"{api_url=}, {resp.text=}")
|
26 |
+
|
27 |
+
api_url = f"{host}/ocr/file/json"
|
28 |
+
resp = requests.post(api_url, files={'image': file})
|
29 |
+
print(f"{api_url=}, {resp.text=}")
|
30 |
+
|
31 |
+
api_url = f"{host}/ocr/b64"
|
32 |
+
resp = requests.post(api_url, data=base64.b64encode(file).decode())
|
33 |
+
print(f"{api_url=}, {resp.text=}")
|
34 |
+
|
35 |
+
api_url = f"{host}/ocr/b64/json"
|
36 |
+
resp = requests.post(api_url, data=base64.b64encode(file).decode())
|
37 |
+
print(f"{api_url=}, {resp.text=}")
|
38 |
+
|
39 |
+
api_url = f"{host}/det/file"
|
40 |
+
resp = requests.post(api_url, files={'image': file})
|
41 |
+
print(f"{api_url=}, {resp.text=}")
|
42 |
+
|
43 |
+
api_url = f"{host}/det/file/json"
|
44 |
+
resp = requests.post(api_url, files={'image': file})
|
45 |
+
print(f"{api_url=}, {resp.text=}")
|
46 |
+
|
47 |
+
# 滑块识别
|
48 |
+
|
49 |
+
target_file = open(r'match_target.png', 'rb').read()
|
50 |
+
bg_file = open(r'match_bg.png', 'rb').read()
|
51 |
+
|
52 |
+
api_url = f"{host}/slide/match/file"
|
53 |
+
resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file})
|
54 |
+
print(f"{api_url=}, {resp.text=}")
|
55 |
+
|
56 |
+
api_url = f"{host}/slide/match/file/json"
|
57 |
+
resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file})
|
58 |
+
print(f"{api_url=}, {resp.text=}")
|
59 |
+
|
60 |
+
api_url = f"{host}/slide/match/b64"
|
61 |
+
target_b64str = base64.b64encode(target_file).decode()
|
62 |
+
bg_b64str = base64.b64encode(bg_file).decode()
|
63 |
+
jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str})
|
64 |
+
resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode())
|
65 |
+
print(f"{api_url=}, {resp.text=}")
|
66 |
+
|
67 |
+
api_url = f"{host}/slide/match/b64/json"
|
68 |
+
resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode())
|
69 |
+
print(f"{api_url=}, {resp.text=}")
|
70 |
+
|
71 |
+
target_file = open(r'compare_target.jpg', 'rb').read()
|
72 |
+
bg_file = open(r'compare_bg.jpg', 'rb').read()
|
73 |
+
|
74 |
+
api_url = f"{host}/slide/compare/file"
|
75 |
+
resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file})
|
76 |
+
print(f"{api_url=}, {resp.text=}")
|
77 |
+
|
78 |
+
api_url = f"{host}/slide/compare/file/json"
|
79 |
+
resp = requests.post(api_url, files={'target_img': target_file, 'bg_img': bg_file})
|
80 |
+
print(f"{api_url=}, {resp.text=}")
|
81 |
+
|
82 |
+
api_url = f"{host}/slide/compare/b64"
|
83 |
+
target_b64str = base64.b64encode(target_file).decode()
|
84 |
+
bg_b64str = base64.b64encode(bg_file).decode()
|
85 |
+
jsonstr = json.dumps({'target_img': target_b64str, 'bg_img': bg_b64str})
|
86 |
+
resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode())
|
87 |
+
print(f"{api_url=}, {resp.text=}")
|
88 |
+
|
89 |
+
api_url = f"{host}/slide/compare/b64/json"
|
90 |
+
resp = requests.post(api_url, data=base64.b64encode(jsonstr.encode()).decode())
|
91 |
+
print(f"{api_url=}, {resp.text=}")
|
92 |
+
|
93 |
+
# 方式二
|
94 |
+
|
95 |
+
# 获取验证码图片
|
96 |
+
# headers = {
|
97 |
+
# "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
|
98 |
+
# "User-Agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4195.1 Safari/537.36"
|
99 |
+
# }
|
100 |
+
# resp = requests.get('https://data.gdcic.net/Dop/CheckCode.aspx?codemark=408.15173910730016', headers=headers, verify=False)
|
101 |
+
# captcha_img = resp.content
|
102 |
+
#
|
103 |
+
# 识别
|
104 |
+
# resp = requests.post(api_url, files={'image': captcha_img})
|
105 |
+
# print('验证码结果', resp.text)
|
106 |
+
#
|
107 |
+
# # 保存验证码图片以供验证
|
108 |
+
# with open('captcha.jpg', 'wb') as f:
|
109 |
+
# f.write(captcha_img)
|
110 |
+
|
111 |
+
# ******************OCR识别部分开始******************
|
test_calc.png
ADDED
![]() |