Spaces:
Runtime error
Runtime error
Rename app.py to backend.py
Browse files- app.py +0 -54
- backend.py +30 -0
app.py
DELETED
|
@@ -1,54 +0,0 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from paddleocr import PaddleOCR
|
| 3 |
-
from PIL import Image, ImageOps
|
| 4 |
-
import numpy as np
|
| 5 |
-
|
| 6 |
-
# Initialize PaddleOCR
|
| 7 |
-
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 8 |
-
|
| 9 |
-
# Function to process the uploaded/captured image
|
| 10 |
-
def process_uploaded_image(image, camera_type="rear"):
|
| 11 |
-
if image is None:
|
| 12 |
-
return "No image uploaded."
|
| 13 |
-
|
| 14 |
-
# Flip the image horizontally for the rear camera
|
| 15 |
-
if camera_type == "rear":
|
| 16 |
-
image = ImageOps.mirror(image)
|
| 17 |
-
|
| 18 |
-
# Perform OCR on the corrected image
|
| 19 |
-
result = ocr.ocr(np.array(image))
|
| 20 |
-
extracted_text = []
|
| 21 |
-
for line in result[0]:
|
| 22 |
-
extracted_text.append(line[1][0])
|
| 23 |
-
return "\n".join(extracted_text)
|
| 24 |
-
|
| 25 |
-
# Gradio Interface
|
| 26 |
-
def create_app():
|
| 27 |
-
with gr.Blocks() as app:
|
| 28 |
-
gr.Markdown("## Mobile-Friendly OCR with Rear Camera Fix")
|
| 29 |
-
|
| 30 |
-
# Inputs for camera type and image capture/upload
|
| 31 |
-
with gr.Row():
|
| 32 |
-
camera_type = gr.Dropdown(
|
| 33 |
-
choices=["front", "rear"],
|
| 34 |
-
label="Camera Type",
|
| 35 |
-
value="rear",
|
| 36 |
-
interactive=True
|
| 37 |
-
)
|
| 38 |
-
image_input = gr.Image(type="pil", label="Upload or Capture Image")
|
| 39 |
-
|
| 40 |
-
# Output for extracted text
|
| 41 |
-
extracted_text = gr.Textbox(label="Extracted Text")
|
| 42 |
-
|
| 43 |
-
# Button to process the image
|
| 44 |
-
process_button = gr.Button("Process Image for OCR")
|
| 45 |
-
process_button.click(
|
| 46 |
-
fn=process_uploaded_image,
|
| 47 |
-
inputs=[image_input, camera_type],
|
| 48 |
-
outputs=extracted_text
|
| 49 |
-
)
|
| 50 |
-
|
| 51 |
-
return app
|
| 52 |
-
|
| 53 |
-
if __name__ == "__main__":
|
| 54 |
-
create_app().launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
backend.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from paddleocr import PaddleOCR
|
| 3 |
+
from PIL import Image
|
| 4 |
+
import numpy as np
|
| 5 |
+
|
| 6 |
+
# Initialize PaddleOCR
|
| 7 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en')
|
| 8 |
+
|
| 9 |
+
# Function to process uploaded image
|
| 10 |
+
def process_uploaded_image(image):
|
| 11 |
+
if image is None:
|
| 12 |
+
return "No image uploaded."
|
| 13 |
+
|
| 14 |
+
# Perform OCR
|
| 15 |
+
result = ocr.ocr(np.array(image))
|
| 16 |
+
extracted_text = []
|
| 17 |
+
for line in result[0]:
|
| 18 |
+
extracted_text.append(line[1][0])
|
| 19 |
+
return "\n".join(extracted_text)
|
| 20 |
+
|
| 21 |
+
# Gradio Interface
|
| 22 |
+
app = gr.Interface(
|
| 23 |
+
fn=process_uploaded_image,
|
| 24 |
+
inputs=gr.Image(type="pil", label="Upload Image"),
|
| 25 |
+
outputs=gr.Textbox(label="Extracted Text"),
|
| 26 |
+
title="OCR Processor"
|
| 27 |
+
)
|
| 28 |
+
|
| 29 |
+
if __name__ == "__main__":
|
| 30 |
+
app.launch(share=True)
|