Skip to content

Commit

Permalink
Merge pull request DayBreak-u#219 from Pad0y/master
Browse files Browse the repository at this point in the history
fixed dbnet OOM and rewrite Dockerfile
  • Loading branch information
DayBreak-u authored Jul 24, 2020
2 parents ffe4570 + 43aa154 commit d71afa2
Show file tree
Hide file tree
Showing 12 changed files with 258 additions and 112 deletions.
131 changes: 131 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
# Created by .ignore support plugin (hsz.mobi)
### Python template
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# PEP 582; used by e.g. github.com/David-OConnor/pyflow
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/
43 changes: 32 additions & 11 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,15 +1,36 @@
FROM python:3.6
ENV LANG C.UTF-8
FROM centos:7.2.1511

COPY . /chineseocr_lite
LABEL Author="Pad0y<github.com/Pad0y>"

WORKDIR /chineseocr_lite
ENV LANG C.UTF-8 LC_ALL=C.UTF-8

RUN PIP_INSTALL="pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/" && \
$PIP_INSTALL \
torch==1.2.0 \
torchvision==0.4.0 \
&& \
$PIP_INSTALL -r requirements.txt
COPY . /data/project/
WORKDIR /data/project/

CMD python app.py 8080
RUN yum -y update \
&& yum -y install gcc gcc-c++ wget make git libSM-1.2.2-2.el7.x86_64 libXrender libXext\
&& yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel libffi-devel \
&& yum -y install python3-devel centos-release-scl scl-utils-build \
&& yum -y install devtoolset-7-gcc* \
&& echo 'source /opt/rh/devtoolset-7/enable' >> ~/.bash_profile \
&& source ~/.bash_profile \
&& scl enable devtoolset-7 bash


RUN pip3 install --user -U pip -i https://pypi.tuna.tsinghua.edu.cn/simple/ \
&& pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/


RUN source ~/.bash_profile && pip3 install -r requirements.txt \
&& pip3 install ./resource/torch-1.2.0+cpu-cp36-cp36m-manylinux1_x86_64.whl \
&& pip3 install ./resource/torchvision-0.4.0+cpu-cp36-cp36m-manylinux1_x86_64.whl

RUN source ~/.bash_profile && cd ./psenet/pse && make clean && make
RUN yum clean all && \
rm -rf /tmp/* && \
rm -rf /data/project/resource

EXPOSE 5000
EXPOSE 8000

CMD python3 flask_app.py
19 changes: 11 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
- windows环境运行需要手动编译psenet/pse目录下的pse.cpp为DLL,为了方便新增支持python3.5和python3.6的pyd文件,在windows平台运行
的时候需要根据自己环境的python版本更改对应的pyd文件为pse.pyd即可

- Docker 环境
## Docker 环境
- 重写Dockerfile,资源占用更小,可在1C 1G的学生服务器编译成功
- 容器环境是python3.6 + pytorch-cpu1.2
- 编译方式:在项目根目录下运行 `docker build -t chineseocr:v1 .`
- Flask-RESTful API运行方式: `docker run --name ocr_api -p5000:5000 -d <Image ID>`
请求方式详见下方
- web app运行方式:`docker run --name ocr_ui -p8000:8000 -d <Image ID> python3 app.py 8000`

1. 可以直接在项目根目录下面运行`docker build -t my/chineseocr .` 构建运行环境的镜像,也可以

使用其他已经构建好的镜像`docker run -dit -p 8080:8080 -v /mnt/d/data/:/data --name chineseocr vitzy/chineseocr_lite`

2. 可通过`docker attach --sig-proxy=false <container id>` 或者`docker exec -it <your container name or id> /bin/bash`进入容器,然后`git clone https://github.com/ouyanghuiyu/chineseocr_lite`拉取本项目代码到`/data`
3. cd 到`chineseocr_lite`下进行安装:`pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple -r requirements.txt`
4. 启动 web `python3 app.py 8080`, 在浏览器中打开` http://127.0.0.1:8080/ocr`
## PSENET 编译
``` Bash
cd psenet/pse
Expand Down Expand Up @@ -74,7 +74,7 @@ http://127.0.0.1:8080/ocr
- 请求url: http://ip:port/api/v1/ocr
- 请求方式:POST
- 请求参数
- imgString:图片转base64后的字符串, str
- ImgString:图片转base64后的字符串, str
- 返回实例
{
"code": "SUCCESS",
Expand All @@ -94,6 +94,9 @@ http://127.0.0.1:8080/ocr
<img width="500" height="300" src="https://github.com/ouyanghuiyu/chineseocr_lite/blob/master/ncnn_project/ocr/res_imgs/res_3.jpg"/>
<img width="500" height="300" src="https://github.com/ouyanghuiyu/chineseocr_lite/blob/master/ncnn_project/ocr/res_imgs/res_2.jpg"/>

## flask-restful api 内存测试(dbnet)
200张图片测试稳定在1-1.5G左右的内存
![](test_imgs/flask-api.png)


## 参考
Expand Down
2 changes: 1 addition & 1 deletion angle_class/angle_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from config import angle_type


class AangleClassHandle():
class AangleClassHandle:
def __init__(self, model_path, net, gpu_id=None):
"""
初始化pytorch模型
Expand Down
32 changes: 17 additions & 15 deletions dbnet/dbnet_infer.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import onnxruntime as rt
import numpy as np
import numpy as np
import time
import cv2
from .decode import SegDetectorRepresenter

from .decode import SegDetectorRepresenter
from psenet.PSENET import SingletonType

mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
Expand All @@ -16,45 +16,47 @@ def draw_bbox(img_path, result, color=(255, 0, 0), thickness=2):
img_path = img_path.copy()
for point in result:
point = point.astype(int)

cv2.polylines(img_path,[point], True, color, thickness)
cv2.polylines(img_path, [point], True, color, thickness)
return img_path

class DBNET(object):
def __init__(self,MODEL_PATH,short_size = 640):

class DBNET(metaclass=SingletonType):
def __init__(self, MODEL_PATH, short_size=640):
self.sess = rt.InferenceSession(MODEL_PATH)
self.short_size = short_size
self.decode_handel = SegDetectorRepresenter()
def process(self,img):


def process(self, img):

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
h, w = img.shape[:2]

if h < w:
scale_h = self.short_size / h
tar_w = w * scale_h
tar_w = tar_w - tar_w % 32
tar_w = max(32, tar_w)
scale_w = tar_w / w

else:
scale_w = self.short_size / w
tar_h = h * scale_w
tar_h = tar_h - tar_h % 32
tar_h = max(32, tar_h)
scale_h = tar_h / h

img = cv2.resize(img, None, fx=scale_w, fy=scale_h)

img = img.astype(np.float32)

img /= 255.0
img -= mean
img /= std
img = img.transpose(2, 0, 1)
transformed_image = np.expand_dims(img, axis=0)
out = self.sess.run(["out1"], {"input0": transformed_image.astype(np.float32)})
box_list, score_list = self.decode_handel(out[0][0],h, w)
box_list, score_list = self.decode_handel(out[0][0], h, w)
if len(box_list) > 0:
idx = box_list.reshape(box_list.shape[0], -1).sum(axis=1) > 0 # 去掉全为0的框
box_list, score_list = box_list[idx], score_list[idx]
Expand Down
Loading

0 comments on commit d71afa2

Please sign in to comment.