-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support
ocr
(base on [PaddleOCR-API](https://github.com/cgcel…
- Loading branch information
1 parent
8e10891
commit 5779cbf
Showing
4 changed files
with
60 additions
and
3 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
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,36 @@ | ||
from fastapi import UploadFile, File | ||
import requests | ||
from config import OCR_ENDPOINT | ||
import time | ||
|
||
|
||
def get_ocr_source(data: any) -> list: | ||
if type(data) is str: | ||
return [data] | ||
elif type(data) is list: | ||
# recursive call and merge the results | ||
return sum([get_ocr_source(item) for item in data], []) | ||
|
||
return [] | ||
|
||
|
||
def ocr_image(file: UploadFile = File(...)) -> str: | ||
start = time.time() | ||
|
||
response = requests.post( | ||
OCR_ENDPOINT + "/ocr/predict-by-file", | ||
files={"file": (file.filename, file.file, file.content_type)}, | ||
) | ||
response.raise_for_status() | ||
data = response.json() | ||
|
||
code = data.get("resultcode", -1) | ||
message = data.get("message", "") | ||
result = data.get("data", []) | ||
|
||
if code != 200: | ||
raise ValueError(f"OCR API error: {message} (code: {code})") | ||
|
||
print(f"[orc] time taken: {time.time() - start:.2f}s (file: {file.filename})") | ||
|
||
return " ".join(get_ocr_source(result)) |