-
-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: add CLI command to pretty print OCR result
- Loading branch information
1 parent
a61eef4
commit 26f44a4
Showing
3 changed files
with
152 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import base64 | ||
from typing import List | ||
|
||
import orjson | ||
import requests | ||
|
||
from robotoff.utils import get_logger, http_session | ||
|
||
logger = get_logger(__name__) | ||
|
||
|
||
def run_ocr_on_image_batch(base64_images: List[str], api_key: str) -> requests.Response: | ||
url = f"https://vision.googleapis.com/v1/images:annotate?key={api_key}" | ||
return http_session.post( | ||
url, | ||
json={ | ||
"requests": [ | ||
{ | ||
"features": [{"type": "TEXT_DETECTION"}], | ||
"image": {"content": base64_image}, | ||
} | ||
for base64_image in base64_images | ||
] | ||
}, | ||
) | ||
|
||
|
||
def run_ocr_on_image(image_bytes: bytes, api_key: str): | ||
if not image_bytes: | ||
raise ValueError("empty image") | ||
|
||
content = base64.b64encode(image_bytes).decode("utf-8") | ||
r = run_ocr_on_image_batch([content], api_key) | ||
|
||
if not r.ok: | ||
logger.info("HTTP %s received", r.status_code) | ||
logger.info("Response: %s", r.text) | ||
return | ||
return orjson.loads(r.content) |
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