提交 b25eb5c7 编写于 作者: H hjdhnx

增加drpy的ocr类型

上级 e0e5c963
# 默认忽略的文件
/shelf/
/workspace.xml
# 基于编辑器的 HTTP 客户端请求
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredIdentifiers">
<list>
<option value="dict.*" />
<option value="custom_addons.common.wb_base.wizard.payment_wizard_mixin.PaymentWizardMixin.show_alert" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.8 (ocr_api_server)" project-jdk-type="Python SDK" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/ocr_api_server.iml" filepath="$PROJECT_DIR$/.idea/ocr_api_server.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="WEB_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/temp" />
<excludeFolder url="file://$MODULE_DIR$/.tmp" />
<excludeFolder url="file://$MODULE_DIR$/tmp" />
</content>
<orderEntry type="jdk" jdkName="Python 3.8 (ocr_api_server)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
......@@ -6,8 +6,8 @@ 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/ \
&& python3 -m pip install --upgrade pip -i https://mirrors.cloud.tencent.com/pypi/simple/\
&& pip3 install --no-cache-dir -r requirements.txt --extra-index-url https://mirrors.cloud.tencent.com/pypi/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 libglib2.0-0 -y
......
......@@ -11,7 +11,7 @@
```shell
# 安装依赖
pip install -r requirements.txt -i https://pypi.douban.com/simple
pip install -r requirements.txt -i https://mirrors.cloud.tencent.com/pypi/simple
# 运行 可选参数如下
# --port 9898 指定端口,默认为9898
......@@ -39,7 +39,7 @@ python ocr_server.py --port 9898 --ocr --old --det
## docker运行方式(目测只能在Linux下部署)
```shell
git clone https://github.com/sml2h3/ocr_api_server.git
git clone https://gitcode.net/qq_32394351/ocr_api_server.git
# docker怎么安装?百度吧
cd ocr_api_server
......@@ -65,7 +65,7 @@ docker run -p 9898:9898 -d ocr_server:v1
# http://{host}:{port}/{opt}/{img_type}/{ret_type}
# opt:操作类型 ocr=OCR det=目标检测 slide=滑块(match和compare两种算法,默认为compare)
# img_type: 数据类型 file=文件上传方式 b64=base64(imgbyte)方式 默认为file方式
# img_type: 数据类型 file=文件上传方式 b64=base64(imgbyte) drpy={'img':'base64'}方式 默认为file方式
# ret_type: 返回类型 json=返回json(识别出错会在msg里返回错误信息) text=返回文本格式(识别出错时回直接返回空文本)
# 例子:
......@@ -73,6 +73,7 @@ docker run -p 9898:9898 -d ocr_server:v1
# OCR请求
# resp = requests.post("http://{host}:{port}/ocr/file", files={'image': image_bytes})
# resp = requests.post("http://{host}:{port}/ocr/b64/text", data=base64.b64encode(file).decode())
# resp = requests.post("http://{host}:{port}/ocr/drpy/text", data={"img":base64.b64encode(img).decode()})
# 目标检测请求
# resp = requests.post("http://{host}:{port}/det/file", files={'image': image_bytes})
......
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : log.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Date : 2022/9/6
import os
import logging
from logging import handlers
import sys
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
LOG_ROOT = dirname
print(LOG_ROOT)
# logging.basicConfig(
# # level=logging.INFO, # 控制台打印的日志级别
# level=logging.DEBUG, # 控制台打印的日志级别
# filename='dr.log', # 将日志写入log_new.log文件中
# filemode='a', # 模式,有w和a,w就是写模式,每次都会重新写日志,覆盖之前的日志 a是追加模式,默认如果不写的话,就是追加模式
# # format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
# format="%(asctime)s:%(levelname)s:%(name)s -- %(message)s", datefmt="%Y/%m/%d %H:%M:%S" # 日志格式
# )
def get_logger(log_filename, level=logging.DEBUG, when='D', back_count=0):
"""
https://blog.csdn.net/qq_39147299/article/details/124455632
:brief 日志记录
:param log_filename: 日志名称
:param level: 日志等级 critical > error > warning > info > debug 当设置某个级别之后,把它低的不会被记录,例如级别设置为warning,则info和debug则会被丢弃
:param when: 间隔时间:
S:秒
M:分
H:小时
D:天
W:每星期(interval==0时代表星期一)
midnight: 每天凌晨
:param back_count: 备份文件的个数,若超过该值,就会自动删除
:return: logger
"""
# 创建一个日志器。提供了应用程序接口
logger = logging.getLogger(log_filename)
# 设置日志输出的最低等级,低于当前等级则会被忽略
logger.setLevel(level)
# 创建日志输出路径
log_path = os.path.join(LOG_ROOT, "logs")
# base_path = os.path.dirname(os.path.abspath(os.path.dirname(__file__))) # 上级目录
# log_path = os.path.join(base_path, f'logs')
if not os.path.exists(log_path):
os.mkdir(log_path)
log_file_path = os.path.join(log_path, log_filename)
# 创建格式器
# formatter = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d] - %(levelname)s: %(message)s',datefmt="%Y/%m/%d %H:%M:%S")
formatter = logging.Formatter('%(asctime)s - %(pathname)s[line:%(lineno)d]:%(levelname)s:%(name)s -- %(message)s', datefmt="%Y-%m-%d %H:%M:%S")
# 创建处理器:ch为控制台处理器,fh为文件处理器
ch = logging.StreamHandler()
ch.setLevel(level)
# 输出到文件
fh = logging.handlers.TimedRotatingFileHandler(
filename=log_file_path,
when=when,
backupCount=back_count,
encoding='utf-8')
fh.setLevel(level)
# 设置日志输出格式
fh.setFormatter(formatter)
ch.setFormatter(formatter)
# 将处理器,添加至日志器中
logger.addHandler(fh)
logger.addHandler(ch)
return logger
logger = get_logger('dr.log')
\ No newline at end of file
2023-03-28 13:56:45 - ocr_server.py[line:109]:INFO:dr.log -- drpy验证码图片:iVBORw0KGgoAAAANSUhEUgAAAIAAAAAoBAMAAADEX+97AAAAG1BMVEXz+/4cRovY5O+HoMRsibait9JRc6c2XJm9zeEA2a6oAAABdklEQVRIie1TPVPCQBDN5ANTuhKDZQBhKNEwSgkYsRUYZihNhgmWRkdiiVroz/Z27yATNUvh2OU1eXN7+2737UbTSpT4BzSDa6842pr7wZjNbwNAVVIjTddxlA+HIgxcvp7gDVnCAdLDfByPapwAJdU2xF+R3+XCNh65nMAZuIs3xYd4e0C0uVr38WuA488XnEACo96Vp8p1G51P4jY2FlGB0azP5YsSxbuPxC2ojpe3WWPoTB2Sbfh3GJA1WckouYF2TPZ5YAI8Xaop1ClrI2nUAnhAW9whO0bxqmcpgQk8i6wu0ja8iA0YaNoq3hjbKRcIuFgFcb2BY0eBD9h6IIvkW1iGWZMmtSCWy7mB2ghPOrMQHEbAoqdOkBpxmsqtQxk9QVahPT1iBOSu0+zkQI6pL1FHgqcm7MJFOBUX7r9piTTnQroRqoVi0PKnihkJuOdIdNgN1O4F08LUn2ioL+0RZ90+6Phbdf8goGnvPutciRIlBL4A1044MEcBs5MAAAAASUVORK5CYII=
2023-03-28 13:56:45 - ocr_server.py[line:141]:INFO:dr.log -- drpy类型的图片识别结果:6585
# encoding=utf-8
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# File : ocr_server.py
# Author: DaShenHan&道长-----先苦后甜,任凭晚风拂柳颜------
# Author's Blog: https://blog.csdn.net/qq_32394351
# Date : 2023/3/28
import argparse
import base64
import json
import ddddocr
from flask import Flask, request
from log import logger
parser = argparse.ArgumentParser(description="使用ddddocr搭建的最简api服务")
parser.add_argument("-p", "--port", type=int, default=9898)
......@@ -64,6 +71,31 @@ class Server(object):
server = Server(ocr=args.ocr, det=args.det, old=args.old)
def getParmas(key=None,value=''):
"""
获取链接参数
:param key:
:return:
"""
content_type = request.headers.get('Content-Type')
# print(content_type)
args = {}
if request.method == 'POST':
if 'application/x-www-form-urlencoded' in content_type or 'multipart/form-data' in content_type:
args = request.form
elif 'application/json' in content_type:
args = request.json
elif 'text/plain' in content_type:
args = request.data
else:
args = request.args
elif request.method == 'GET':
args = request.args
if key:
return args.get(key,value)
else:
return args
def get_img(request, img_type='file', img_name='image'):
if img_type == 'b64':
img = base64.b64decode(request.get_data()) #
......@@ -72,6 +104,10 @@ def get_img(request, img_type='file', img_name='image'):
img = base64.b64decode(dic.get(img_name).encode())
except Exception as e: # just base64 of single image
pass
elif img_type == 'drpy':
img_base64 = getParmas('img')
logger.info(f'drpy验证码图片:{img_base64}')
img = base64.b64decode(img_base64)
else:
img = request.files.get(img_name).read()
return img
......@@ -102,8 +138,10 @@ def ocr(opt, img_type='file', ret_type='text'):
result = server.detection(img)
else:
raise f"<opt={opt}> is invalid"
logger.info(f'{img_type}类型的图片识别结果:{result}')
return set_ret(result, ret_type)
except Exception as e:
logger.info(f'{e}')
return set_ret(e, ret_type)
@app.route('/slide/<algo_type>/<img_type>', methods=['POST'])
......
......@@ -36,6 +36,14 @@ api_url = f"{host}/ocr/b64/json"
resp = requests.post(api_url, data=base64.b64encode(file).decode())
print(f"{api_url=}, {resp.text=}")
api_url = f"{host}/ocr/drpy"
resp = requests.post(api_url, data={"img":base64.b64encode(file).decode()})
print(f"{api_url=}, {resp.text=}")
api_url = f"{host}/ocr/drpy/json"
resp = requests.post(api_url, data={"img":base64.b64encode(file).decode()})
print(f"{api_url=}, {resp.text=}")
api_url = f"{host}/det/file"
resp = requests.post(api_url, files={'image': file})
print(f"{api_url=}, {resp.text=}")
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册