Skip to content

Commit

Permalink
feat: api config
Browse files Browse the repository at this point in the history
  • Loading branch information
zxjlm committed May 14, 2024
1 parent ebcc2c7 commit 06ce31e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 15 deletions.
6 changes: 4 additions & 2 deletions backend/app/app/api/api.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from fastapi import APIRouter

from app.api.api_v1.endpoints.digit import digit_api_router
from app.api.api_v1.endpoints.font import font_api_router

api_router = APIRouter()
api_router.include_router(font_api_router, prefix="/api", tags=["utils"])
api_router = APIRouter(prefix="/api")
api_router.include_router(font_api_router, prefix="/font", tags=["utils"])
api_router.include_router(digit_api_router, prefix="/digit", tags=["utils"])
# api_router.include_router(items.router, prefix="/items", tags=["items"])
30 changes: 17 additions & 13 deletions backend/app/app/api/api_v1/endpoints/font.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,30 @@
import time

from fastapi import APIRouter, UploadFile
from starlette.requests import Request

from backend.app.app import config
from backend.app.app.utils.ocrs import check_file, ocr_processor
from app import config
from app.utils.ocrs import check_file, ocr_processor, ocr_func

font_api_router = APIRouter()


@font_api_router.post('/font_file_cracker/')
def font_file_cracker(file: UploadFile, type_=str):
async def font_file_cracker(file: UploadFile, type_=str):
filename = re.sub('[(()) ]', '', file.filename)
if not os.path.exists('./font_collection'):
os.mkdir('./font_collection')

file.save('./font_collection/' + filename)
base_path = './font_collection'
if not os.path.exists(base_path):
os.mkdir(base_path)

if config.is_online and not check_file('./font_collection/' + filename):
return {'code': 300, 'msg': 'Please use example file(*^_^*)'}
font_path = os.path.join(base_path, filename)
with open(font_path, "wb") as f:
f.write(await file.read())

res = ocr_processor('./font_collection/' + filename)
if config.is_online and not check_file(font_path):
return {'code': 400, 'msg': 'Please use example file(*^_^*)'}

res = ocr_processor(font_path)

# TBD
if type_ == 'html':
Expand All @@ -35,19 +40,18 @@ def font_file_cracker(file: UploadFile, type_=str):


@font_api_router.post('/img_cracker_via_local_ocr/')
def local_cracker(img_b64: str):
def local_cracker(img_b64: str, request: Request):
"""
接受单个图片,进行本地的ocr,返回图片破解结果
:return:
"""
if config.is_online:
return {'code': 300, 'msg': 'online mode can`t use image cracker'}
return {'code': 400, 'msg': 'online mode can`t use image cracker'}
# img_b64 = request.form['img'].replace('data:image/png;base64,', '')

start_time = time.time()
res = ocr_func(img_b64, 'single_image', request.remote_addr)
res = ocr_func(img_b64, 'single_image', request.client.host)
return {'code': 200, 'msg': '成功',
'data': {'raw_out': res,
'speed_time':
round(time.time() - start_time, 2)}}

0 comments on commit 06ce31e

Please sign in to comment.