deepak191z commited on
Commit
669df7a
1 Parent(s): 3a21f1e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -32
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
- def inference(img, lang):
13
- ocr = PaddleOCR(use_angle_cls=True, lang=lang,use_gpu=False)
14
- img_path = img
 
 
 
 
 
 
 
 
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, # https://github.com/PaddlePaddle/PaddleOCR/blob/release/2.7/tools/infer/utility.py#L365
23
- font_path='simfang.ttf')
24
  im_show = Image.fromarray(im_show)
25
- im_show.save('result.jpg')
26
-
27
- return 'result.jpg', result, '\n'.join(txts)
28
-
29
- # return 'result.jpg'
30
-
31
- title = 'PaddleOCR'
32
- description = 'Gradio demo for PaddleOCR. PaddleOCR demo supports Chinese, English, French, German, Korean and Japanese. To use it, simply upload your image and choose a language from the dropdown menu, or click one of the examples to load them. Read more at the links below.'
33
- article = "<p style='text-align: center'><a href='https://www.paddlepaddle.org.cn/hub/scene/ocr'>Awesome multilingual OCR toolkits based on PaddlePaddle (practical ultra lightweight OCR system, support 80+ languages recognition, provide data annotation and synthesis tools, support training and deployment among server, mobile, embedded and IoT devices)</a> | <a href='https://github.com/PaddlePaddle/PaddleOCR'>Github Repo</a></p>"
34
- examples = [['example.jpg','en']]
35
- css = ".output_image, .input_image {height: 40rem !important; width: 100% !important;}"
36
- app = gr.Interface(
37
- inference,
38
- [gr.Image(type='filepath', label='Input'),gr.Dropdown(choices=['ch', 'en', 'fr', 'german', 'korean', 'japan', 'ar'], type="value", value='ch', label='language')],
39
- # gr.outputs.Image(type='file', label='Output'),
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)