Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,80 +1,98 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from paddleocr import PaddleOCR
|
5 |
from doctr.io import DocumentFile
|
6 |
from doctr.models import ocr_predictor
|
7 |
-
import numpy as np
|
8 |
-
from PIL import Image
|
9 |
-
import io
|
10 |
-
|
11 |
-
# Set up logging
|
12 |
-
logging.basicConfig(level=logging.INFO)
|
13 |
-
logger = logging.getLogger(__name__)
|
14 |
|
15 |
-
app = FastAPI()
|
16 |
|
17 |
-
app.add_middleware(
|
18 |
-
CORSMiddleware,
|
19 |
-
allow_origins=["*"],
|
20 |
-
allow_credentials=True,
|
21 |
-
allow_methods=["*"],
|
22 |
-
allow_headers=["*"],
|
23 |
-
)
|
24 |
|
25 |
-
# Initialize models once at startup
|
26 |
ocr_model = ocr_predictor(pretrained=True)
|
27 |
-
paddle_ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
28 |
|
|
|
|
|
|
|
|
|
|
|
29 |
def ocr_with_doctr(file):
|
30 |
text_output = ''
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
|
|
42 |
return text_output
|
43 |
|
|
|
|
|
|
|
44 |
def ocr_with_paddle(img):
|
45 |
finaltext = ''
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
logger.error(f"Error processing image: {e}")
|
54 |
-
raise HTTPException(status_code=500, detail=f"Error processing image: {e}")
|
55 |
return finaltext
|
56 |
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
import requests
|
4 |
+
import os
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
import huggingface_hub
|
8 |
+
from huggingface_hub import Repository
|
9 |
+
from datetime import datetime
|
10 |
+
import scipy.ndimage.interpolation as inter
|
11 |
+
import datasets
|
12 |
+
from datasets import load_dataset, Image
|
13 |
+
from PIL import Image
|
14 |
from paddleocr import PaddleOCR
|
15 |
from doctr.io import DocumentFile
|
16 |
from doctr.models import ocr_predictor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
17 |
|
|
|
18 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
|
|
|
20 |
ocr_model = ocr_predictor(pretrained=True)
|
|
|
21 |
|
22 |
+
|
23 |
+
|
24 |
+
"""
|
25 |
+
Perform OCR with doctr
|
26 |
+
"""
|
27 |
def ocr_with_doctr(file):
|
28 |
text_output = ''
|
29 |
+
|
30 |
+
# Load the document
|
31 |
+
doc = DocumentFile.from_pdf(file)
|
32 |
+
|
33 |
+
# Perform OCR
|
34 |
+
result = ocr_model(doc)
|
35 |
+
|
36 |
+
# Extract text from OCR result
|
37 |
+
for page in result.pages:
|
38 |
+
for block in page.blocks:
|
39 |
+
for line in block.lines:
|
40 |
+
text_output += " ".join([word.value for word in line.words]) + "\n"
|
41 |
+
|
42 |
return text_output
|
43 |
|
44 |
+
"""
|
45 |
+
Paddle OCR
|
46 |
+
"""
|
47 |
def ocr_with_paddle(img):
|
48 |
finaltext = ''
|
49 |
+
ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
50 |
+
# img_path = 'exp.jpeg'
|
51 |
+
result = ocr.ocr(img)
|
52 |
+
|
53 |
+
for i in range(len(result[0])):
|
54 |
+
text = result[0][i][1][0]
|
55 |
+
finaltext += ' '+ text
|
|
|
|
|
56 |
return finaltext
|
57 |
|
58 |
+
def generate_ocr(Method, file):
|
59 |
+
text_output = ''
|
60 |
+
if isinstance(file, bytes): # Handle file uploaded as bytes
|
61 |
+
file = io.BytesIO(file)
|
62 |
+
|
63 |
+
if file.name.endswith('.pdf'):
|
64 |
+
# Perform OCR on the PDF using doctr
|
65 |
+
text_output = ocr_with_doctr(file)
|
66 |
+
|
67 |
+
else:
|
68 |
+
# Handle image file
|
69 |
+
img_np = np.array(Image.open(file))
|
70 |
+
text_output = generate_text_from_image(Method, img_np)
|
71 |
+
|
72 |
+
return text_output
|
73 |
+
|
74 |
+
def generate_text_from_image(Method, img):
|
75 |
+
text_output = ''
|
76 |
+
if Method == 'PaddleOCR':
|
77 |
+
text_output = ocr_with_paddle(img)
|
78 |
+
return text_output
|
79 |
+
|
80 |
+
|
81 |
+
import gradio as gr
|
82 |
+
|
83 |
+
image_or_pdf = gr.File(label="Upload an image or PDF")
|
84 |
+
method = gr.Radio(["PaddleOCR"], value="PaddleOCR")
|
85 |
+
output = gr.Textbox(label="Output")
|
86 |
+
|
87 |
+
demo = gr.Interface(
|
88 |
+
generate_ocr,
|
89 |
+
[method, image_or_pdf],
|
90 |
+
output,
|
91 |
+
title="Optical Character Recognition",
|
92 |
+
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
93 |
+
article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
|
94 |
+
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
95 |
+
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
96 |
+
)
|
97 |
+
|
98 |
+
demo.launch(show_error=True)
|