forked from PaddlePaddle/PaddleOCR
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request PaddlePaddle#1 from lewangdev/fastapi
Fastapi
- Loading branch information
Showing
15 changed files
with
109 additions
and
155 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,13 @@ | ||
import log | ||
import os | ||
import sys | ||
import logging | ||
from tornado.options import define, options | ||
import tornado.web | ||
import tornado.httpserver | ||
import tornado.ioloop | ||
from tornado.web import StaticFileHandler | ||
from webhandler import IndexHandler, OcrHandler | ||
from fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
import uvicorn | ||
|
||
BASE_PATH = os.path.dirname(os.path.abspath(__file__)) | ||
sys.path.append(BASE_PATH) | ||
|
||
|
||
logger = logging.getLogger(log.LOGGER_ROOT_NAME + '.' + __name__) | ||
|
||
current_path = os.path.dirname(__file__) | ||
settings = dict( | ||
static_path=os.path.join(current_path, "..", "webui", "dist") | ||
) | ||
|
||
|
||
def make_app(): | ||
return tornado.web.Application([ | ||
(r"/api/ocr", OcrHandler), | ||
(r"/", IndexHandler), | ||
(r"/(.*)", StaticFileHandler, | ||
{"path": os.path.join(current_path, "..", "webui", "dist"), "default_filename": "index.html"}), | ||
|
||
], **settings) | ||
from router import router | ||
|
||
app = FastAPI() | ||
app.include_router(router, prefix="/api") | ||
app.mount("/", StaticFiles(directory=os.path.join(".", "webui", "dist"), html=True), name="static") | ||
|
||
if __name__ == "__main__": | ||
define("addr", default='0.0.0.0', type=str, help='指定运行时绑定的地址') | ||
define("port", default=8080, type=int, help='指定运行时端口号') | ||
tornado.options.parse_command_line() | ||
port = options.port | ||
addr = options.addr | ||
app = make_app() | ||
app.listen(port, addr) | ||
logger.info("Application is running on: http://%s:%s/" % (addr, port)) | ||
tornado.ioloop.IOLoop.current().start() | ||
uvicorn.run(app, host="0.0.0.0", port=8080) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import time | ||
import json | ||
from fastapi import APIRouter, File, UploadFile, Form, Response, HTTPException | ||
from jsonencoder import NumpyEncoder | ||
from util import * | ||
from ocr import text_ocr | ||
|
||
router = APIRouter() | ||
|
||
|
||
@router.post('/ocr', tags=['ocr']) | ||
async def ocr(img_upload: UploadFile = File(None), | ||
img_b64: str = Form(None), | ||
compress_size: int = Form(None), | ||
ocr_model: str = Form(None)): | ||
start_time = time.time() | ||
|
||
if img_upload is not None: | ||
img = convert_bytes_to_image(img_upload.file.read()) | ||
elif img_b64 is not None: | ||
img = convert_b64_to_image(img_b64) | ||
else: | ||
return Response(media_type="application/json", status_code=400, | ||
content=json.dumps(dict(code=4001, msg='没有传入参数'))) | ||
|
||
img = rotate_image(img) | ||
img = img.convert("RGB") | ||
img = compress_image(img, compress_size) | ||
|
||
texts = text_ocr(img, ocr_model) | ||
img_drawed = draw_box_on_image(img.copy(), texts) | ||
img_drawed_b64 = convert_image_to_b64(img_drawed) | ||
|
||
data = {'code': 0, 'msg': '成功', | ||
'data': {'img_detected': 'data:image/jpeg;base64,' + img_drawed_b64, | ||
'raw_out': list(map(lambda x: [x[0], x[1][0], x[1][1]], texts)), | ||
'speed_time': round(time.time() - start_time, 2)}} | ||
json_str = json.dumps(data, cls=NumpyEncoder, ensure_ascii=False).encode('utf-8') | ||
return Response(media_type="application/json", content=json_str) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
black | ||
mypy | ||
flake8 | ||
requests | ||
pytest | ||
pytest-cov | ||
isort | ||
autoflake |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
paddlepaddle==2.2.0 | ||
paddleocr==2.3.0.2 | ||
tornado==6.0.2 | ||
fastapi | ||
python-multipart | ||
uvicorn |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import requests | ||
import base64 | ||
|
||
|
||
def img_to_base64(img_path): | ||
with open(img_path, 'rb')as read: | ||
b64 = base64.b64encode(read.read()) | ||
return b64 | ||
|
||
|
||
url = 'http://127.0.0.1:8000/api/ocr' | ||
img_b64 = img_to_base64('./img1.png') | ||
res = requests.post(url=url, data={'img_b64': img_b64}) | ||
|
||
print(res.content.decode('utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import requests | ||
url = 'http://127.0.0.1:8000/api/ocr' | ||
img1_file = { | ||
'img_upload': open('img1.png', 'rb') | ||
} | ||
res = requests.post(url=url, data={'compress': 0}, files=img1_file) | ||
|
||
print(res.content.decode('utf-8')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters