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.
- Loading branch information
Showing
11 changed files
with
90 additions
and
62 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
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,15 +1,13 @@ | ||
from typing import Optional | ||
|
||
import os | ||
from fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
import uvicorn | ||
import os | ||
|
||
from route import router | ||
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") | ||
app.include_router(router) | ||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=8000) | ||
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 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,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
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