Spaces:
Sleeping
Sleeping
deepak191z
commited on
Commit
•
669df7a
1
Parent(s):
3a21f1e
Update app.py
Browse files
app.py
CHANGED
@@ -1,49 +1,67 @@
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
2 |
os.system('pip install paddlepaddle==2.4.2')
|
3 |
-
# os.system('pip install paddlepaddle==0.0.0 -f https://www.paddlepaddle.org.cn/whl/linux/cpu-mkl/develop.html')
|
4 |
os.system('pip install paddleocr')
|
|
|
5 |
from paddleocr import PaddleOCR, draw_ocr
|
6 |
from PIL import Image
|
7 |
import gradio as gr
|
8 |
import torch
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
|
11 |
|
12 |
-
|
13 |
-
|
14 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
15 |
result = ocr.ocr(img_path, cls=True)[0]
|
16 |
|
17 |
boxes = [line[0] for line in result]
|
18 |
txts = [line[1][0] for line in result]
|
19 |
scores = [line[1][1] for line in result]
|
20 |
-
|
21 |
image = Image.open(img_path).convert('RGB')
|
22 |
-
im_show = draw_ocr(image, boxes, txts=None, scores=None,
|
23 |
-
font_path='simfang.ttf')
|
24 |
im_show = Image.fromarray(im_show)
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
outputs=["image", "text", "text"],
|
41 |
-
title=title,
|
42 |
-
description=description,
|
43 |
-
article=article,
|
44 |
-
examples=examples,
|
45 |
-
css=css,
|
46 |
-
# enable_queue=True
|
47 |
-
)
|
48 |
-
app.queue(max_size=10)
|
49 |
-
app.launch(debug=True)
|
|
|
1 |
import os
|
2 |
+
import tempfile
|
3 |
+
import shutil
|
4 |
+
import logging
|
5 |
+
|
6 |
os.system('pip install paddlepaddle==2.4.2')
|
|
|
7 |
os.system('pip install paddleocr')
|
8 |
+
|
9 |
from paddleocr import PaddleOCR, draw_ocr
|
10 |
from PIL import Image
|
11 |
import gradio as gr
|
12 |
import torch
|
13 |
+
from fastapi import FastAPI, File, UploadFile, Form
|
14 |
+
from fastapi.responses import FileResponse
|
15 |
+
import uvicorn
|
16 |
+
|
17 |
+
# Configure logging
|
18 |
+
logging.basicConfig(level=logging.INFO)
|
19 |
+
logger = logging.getLogger(__name__)
|
20 |
+
|
21 |
+
CUSTOM_PATH = "/ocr"
|
22 |
+
|
23 |
+
app = FastAPI()
|
24 |
+
|
25 |
+
@app.get("/")
|
26 |
+
def read_main():
|
27 |
+
return {"message": "This is your main app"}
|
28 |
+
|
29 |
+
io = gr.Interface(lambda x: "Hello, " + x + "!", "textbox", "textbox")
|
30 |
|
31 |
torch.hub.download_url_to_file('https://i.imgur.com/aqMBT0i.jpg', 'example.jpg')
|
32 |
|
33 |
+
@app.post("/ocr/")
|
34 |
+
async def ocr_endpoint(img: UploadFile = File(...), lang: str = Form(...)):
|
35 |
+
logger.info("Processing OCR request")
|
36 |
+
|
37 |
+
# Save the uploaded image to a temporary file
|
38 |
+
with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as temp_img:
|
39 |
+
shutil.copyfileobj(img.file, temp_img)
|
40 |
+
img_path = temp_img.name
|
41 |
+
|
42 |
+
# Perform OCR
|
43 |
+
ocr = PaddleOCR(use_angle_cls=True, lang=lang, use_gpu=False)
|
44 |
result = ocr.ocr(img_path, cls=True)[0]
|
45 |
|
46 |
boxes = [line[0] for line in result]
|
47 |
txts = [line[1][0] for line in result]
|
48 |
scores = [line[1][1] for line in result]
|
49 |
+
|
50 |
image = Image.open(img_path).convert('RGB')
|
51 |
+
im_show = draw_ocr(image, boxes, txts=None, scores=None, font_path='simfang.ttf')
|
|
|
52 |
im_show = Image.fromarray(im_show)
|
53 |
+
result_img_path = 'result.jpg'
|
54 |
+
im_show.save(result_img_path)
|
55 |
+
|
56 |
+
# Prepare the response
|
57 |
+
response_data = {
|
58 |
+
"result_image": result_img_path,
|
59 |
+
"ocr_result": result,
|
60 |
+
"extracted_text": '\n'.join(txts)
|
61 |
+
}
|
62 |
+
|
63 |
+
logger.info("OCR request processed successfully")
|
64 |
+
return response_data
|
65 |
+
|
66 |
+
app = gr.mount_gradio_app(app, io, path=CUSTOM_PATH)
|
67 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|