Spaces:
Sleeping
Sleeping
Removed gradio
Browse files
app.py
CHANGED
@@ -1,138 +1,57 @@
|
|
1 |
-
import
|
2 |
-
|
3 |
-
import
|
4 |
-
import requests
|
5 |
-
import cv2
|
6 |
-
import os
|
7 |
-
import csv
|
8 |
import numpy as np
|
9 |
-
import pandas as pd
|
10 |
-
import huggingface_hub
|
11 |
-
from huggingface_hub import Repository
|
12 |
-
from datetime import datetime
|
13 |
-
import scipy.ndimage.interpolation as inter
|
14 |
-
import easyocr
|
15 |
-
import datasets
|
16 |
-
from datasets import load_dataset, Image
|
17 |
from PIL import Image
|
18 |
from paddleocr import PaddleOCR
|
19 |
from doctr.io import DocumentFile
|
20 |
from doctr.models import ocr_predictor
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
-
|
23 |
-
|
24 |
ocr_model = ocr_predictor(pretrained=True)
|
|
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
"""
|
29 |
-
Perform OCR with doctr
|
30 |
-
"""
|
31 |
def ocr_with_doctr(file):
|
32 |
text_output = ''
|
33 |
-
|
34 |
-
# Load the document
|
35 |
doc = DocumentFile.from_pdf(file)
|
36 |
-
|
37 |
-
# Perform OCR
|
38 |
result = ocr_model(doc)
|
39 |
-
|
40 |
-
# Extract text from OCR result
|
41 |
for page in result.pages:
|
42 |
for block in page.blocks:
|
43 |
for line in block.lines:
|
44 |
text_output += " ".join([word.value for word in line.words]) + "\n"
|
45 |
-
|
46 |
return text_output
|
47 |
|
48 |
-
"""
|
49 |
-
Paddle OCR
|
50 |
-
"""
|
51 |
def ocr_with_paddle(img):
|
52 |
finaltext = ''
|
53 |
-
|
54 |
-
# img_path = 'exp.jpeg'
|
55 |
-
result = ocr.ocr(img)
|
56 |
-
|
57 |
for i in range(len(result[0])):
|
58 |
text = result[0][i][1][0]
|
59 |
-
finaltext += ' '+ text
|
60 |
return finaltext
|
61 |
|
62 |
-
|
63 |
-
|
64 |
-
"""
|
65 |
-
def ocr_with_keras(img):
|
66 |
-
output_text = ''
|
67 |
-
pipeline=keras_ocr.pipeline.Pipeline()
|
68 |
-
images=[keras_ocr.tools.read(img)]
|
69 |
-
predictions=pipeline.recognize(images)
|
70 |
-
first=predictions[0]
|
71 |
-
for text,box in first:
|
72 |
-
output_text += ' '+ text
|
73 |
-
return output_text
|
74 |
-
|
75 |
-
"""
|
76 |
-
easy OCR
|
77 |
-
"""
|
78 |
-
# gray scale image
|
79 |
-
def get_grayscale(image):
|
80 |
-
return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
81 |
-
|
82 |
-
# Thresholding or Binarization
|
83 |
-
def thresholding(src):
|
84 |
-
return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]
|
85 |
-
def ocr_with_easy(img):
|
86 |
-
gray_scale_image=get_grayscale(img)
|
87 |
-
thresholding(gray_scale_image)
|
88 |
-
cv2.imwrite('image.png',gray_scale_image)
|
89 |
-
reader = easyocr.Reader(['th','en'])
|
90 |
-
bounds = reader.readtext('image.png',paragraph="False",detail = 0)
|
91 |
-
bounds = ''.join(bounds)
|
92 |
-
return bounds
|
93 |
-
|
94 |
-
def generate_ocr(Method, file):
|
95 |
-
text_output = ''
|
96 |
-
if isinstance(file, bytes): # Handle file uploaded as bytes
|
97 |
-
file = io.BytesIO(file)
|
98 |
-
|
99 |
-
if file.name.endswith('.pdf'):
|
100 |
-
# Perform OCR on the PDF using doctr
|
101 |
-
text_output = ocr_with_doctr(file)
|
102 |
|
|
|
|
|
|
|
|
|
|
|
103 |
else:
|
104 |
-
|
105 |
-
|
106 |
-
|
107 |
-
|
108 |
-
return text_output
|
109 |
-
|
110 |
-
def generate_text_from_image(Method, img):
|
111 |
-
text_output = ''
|
112 |
-
if Method == 'EasyOCR':
|
113 |
-
text_output = ocr_with_easy(img)
|
114 |
-
elif Method == 'KerasOCR':
|
115 |
-
text_output = ocr_with_keras(img)
|
116 |
-
elif Method == 'PaddleOCR':
|
117 |
-
text_output = ocr_with_paddle(img)
|
118 |
-
return text_output
|
119 |
-
|
120 |
-
|
121 |
-
import gradio as gr
|
122 |
-
|
123 |
-
image_or_pdf = gr.File(label="Upload an image or PDF")
|
124 |
-
method = gr.Radio(["PaddleOCR", "EasyOCR", "KerasOCR"], value="PaddleOCR")
|
125 |
-
output = gr.Textbox(label="Output")
|
126 |
-
|
127 |
-
demo = gr.Interface(
|
128 |
-
generate_ocr,
|
129 |
-
[method, image_or_pdf],
|
130 |
-
output,
|
131 |
-
title="Optical Character Recognition",
|
132 |
-
css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
|
133 |
-
article="""<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at
|
134 |
-
<a href="mailto:[email protected]" target="_blank">[email protected]</a>
|
135 |
-
<p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
|
136 |
-
)
|
137 |
|
138 |
-
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from fastapi.middleware.cors import CORSMiddleware
|
3 |
+
from fastapi.middleware.gzip import GZipMiddleware
|
|
|
|
|
|
|
|
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
from PIL import Image
|
6 |
from paddleocr import PaddleOCR
|
7 |
from doctr.io import DocumentFile
|
8 |
from doctr.models import ocr_predictor
|
9 |
+
import io
|
10 |
+
|
11 |
+
app = FastAPI()
|
12 |
+
app.add_middleware(
|
13 |
+
CORSMiddleware,
|
14 |
+
allow_origins=["*"],
|
15 |
+
allow_credentials=True,
|
16 |
+
allow_methods=["*"],
|
17 |
+
allow_headers=["*"]
|
18 |
+
)
|
19 |
|
20 |
+
# Initialize models once at startup
|
|
|
21 |
ocr_model = ocr_predictor(pretrained=True)
|
22 |
+
paddle_ocr = PaddleOCR(lang='en', use_angle_cls=True)
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
def ocr_with_doctr(file):
|
25 |
text_output = ''
|
|
|
|
|
26 |
doc = DocumentFile.from_pdf(file)
|
|
|
|
|
27 |
result = ocr_model(doc)
|
|
|
|
|
28 |
for page in result.pages:
|
29 |
for block in page.blocks:
|
30 |
for line in block.lines:
|
31 |
text_output += " ".join([word.value for word in line.words]) + "\n"
|
|
|
32 |
return text_output
|
33 |
|
|
|
|
|
|
|
34 |
def ocr_with_paddle(img):
|
35 |
finaltext = ''
|
36 |
+
result = paddle_ocr.ocr(img)
|
|
|
|
|
|
|
37 |
for i in range(len(result[0])):
|
38 |
text = result[0][i][1][0]
|
39 |
+
finaltext += ' ' + text
|
40 |
return finaltext
|
41 |
|
42 |
+
def generate_text_from_image(img):
|
43 |
+
return ocr_with_paddle(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
44 |
|
45 |
+
@app.post("/ocr/")
|
46 |
+
async def perform_ocr(file: UploadFile = File(...)):
|
47 |
+
file_bytes = await file.read()
|
48 |
+
if file.filename.endswith('.pdf'):
|
49 |
+
text_output = ocr_with_doctr(io.BytesIO(file_bytes))
|
50 |
else:
|
51 |
+
img = np.array(Image.open(io.BytesIO(file_bytes)))
|
52 |
+
text_output = generate_text_from_image(img)
|
53 |
+
return {"ocr_text": text_output}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
54 |
|
55 |
+
@app.get("/test/")
|
56 |
+
async def test_call():
|
57 |
+
return {"message": "Hi. I'm running"}
|