Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
@@ -1,4 +1,6 @@
|
|
1 |
import os
|
|
|
|
|
2 |
from fastapi import FastAPI, File, Request, UploadFile, Body, Depends, HTTPException
|
3 |
from fastapi.security.api_key import APIKeyHeader
|
4 |
from typing import Optional, Annotated
|
@@ -8,6 +10,7 @@ from io import BytesIO
|
|
8 |
import pytesseract
|
9 |
from nltk.tokenize import sent_tokenize
|
10 |
from transformers import MarianMTModel, MarianTokenizer
|
|
|
11 |
|
12 |
API_KEY = os.environ.get("API_KEY")
|
13 |
|
@@ -19,19 +22,44 @@ def get_api_key(api_key: Optional[str] = Depends(api_key_header)):
|
|
19 |
raise HTTPException(status_code=401, detail="Unauthorized access")
|
20 |
return api_key
|
21 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
@app.post("/api/ocr", response_model=dict)
|
23 |
async def ocr(
|
24 |
api_key: str = Depends(get_api_key),
|
25 |
image: UploadFile = File(...),
|
26 |
# languages: list = Body(["eng"])
|
27 |
):
|
|
|
28 |
try:
|
29 |
content = await image.read()
|
30 |
image = Image.open(BytesIO(content))
|
31 |
print("[image]",image)
|
32 |
if hasattr(pytesseract, "image_to_string"):
|
33 |
print("Image to string function is available")
|
34 |
-
print(pytesseract.image_to_string(image, lang = 'eng'))
|
|
|
35 |
else:
|
36 |
print("Image to string function is not available")
|
37 |
# text = pytesseract.image_to_string(image, lang="+".join(languages))
|
|
|
1 |
import os
|
2 |
+
os.system("sudo apt-get install xclip")
|
3 |
+
import nltk
|
4 |
from fastapi import FastAPI, File, Request, UploadFile, Body, Depends, HTTPException
|
5 |
from fastapi.security.api_key import APIKeyHeader
|
6 |
from typing import Optional, Annotated
|
|
|
10 |
import pytesseract
|
11 |
from nltk.tokenize import sent_tokenize
|
12 |
from transformers import MarianMTModel, MarianTokenizer
|
13 |
+
nltk.download('punkt')
|
14 |
|
15 |
API_KEY = os.environ.get("API_KEY")
|
16 |
|
|
|
22 |
raise HTTPException(status_code=401, detail="Unauthorized access")
|
23 |
return api_key
|
24 |
|
25 |
+
# Image path
|
26 |
+
img_dir = "./data"
|
27 |
+
# Get tesseract language list
|
28 |
+
choices = os.popen('tesseract --list-langs').read().split('\n')[1:-1]
|
29 |
+
# Convert tesseract language list to pytesseract language
|
30 |
+
def ocr_lang(lang_list):
|
31 |
+
lang_str = ""
|
32 |
+
lang_len = len(lang_list)
|
33 |
+
if lang_len == 1:
|
34 |
+
return lang_list[0]
|
35 |
+
else:
|
36 |
+
for i in range(lang_len):
|
37 |
+
lang_list.insert(lang_len - i, "+")
|
38 |
+
|
39 |
+
lang_str = "".join(lang_list[:-1])
|
40 |
+
return lang_str
|
41 |
+
# ocr tesseract
|
42 |
+
def ocr_tesseract(img, languages):
|
43 |
+
print("[img]", img)
|
44 |
+
print("[languages]", languages)
|
45 |
+
ocr_str = pytesseract.image_to_string(img, lang=ocr_lang(languages))
|
46 |
+
return ocr_str
|
47 |
+
|
48 |
@app.post("/api/ocr", response_model=dict)
|
49 |
async def ocr(
|
50 |
api_key: str = Depends(get_api_key),
|
51 |
image: UploadFile = File(...),
|
52 |
# languages: list = Body(["eng"])
|
53 |
):
|
54 |
+
|
55 |
try:
|
56 |
content = await image.read()
|
57 |
image = Image.open(BytesIO(content))
|
58 |
print("[image]",image)
|
59 |
if hasattr(pytesseract, "image_to_string"):
|
60 |
print("Image to string function is available")
|
61 |
+
# print(pytesseract.image_to_string(image, lang = 'eng'))
|
62 |
+
text = ocr_tesseract(image, ['eng'])
|
63 |
else:
|
64 |
print("Image to string function is not available")
|
65 |
# text = pytesseract.image_to_string(image, lang="+".join(languages))
|