OCR-Demo / app.py
sayedM's picture
Update app.py
027bb26
import supervision as sv
import gradio as gr
from ultralytics import YOLO
import sahi
import numpy as np
# Images
sahi.utils.file.download_from_url(
"https://www.erbanotizie.com/wp-content/uploads/2014/01/Casello.jpg",
"ocr1.jpg",
)
sahi.utils.file.download_from_url(
"https://media-cdn.tripadvisor.com/media/photo-s/15/1d/03/18/receipt.jpg",
"ocr2.jpg",
)
sahi.utils.file.download_from_url(
"https://upload.forumfree.net/i/ff11450850/b5ef33b7-01da-4055-9ece-089b2a35a193.jpg",
"ocr3.jpg",
)
annotatorbbox = sv.BoxAnnotator()
annotatormask=sv.MaskAnnotator()
model = YOLO("best_Receipt.pt")
def yolov8_inference(
image: gr.inputs.Image = None,
conf_threshold: gr.inputs.Slider = 0.5,
iou_threshold: gr.inputs.Slider = 0.45,
):
image=image[:, :, ::-1].astype(np.uint8)
model = YOLO("https://huggingface.co/spaces/devisionx/first-demo/blob/main/best_Receipt.pt")
results = model(image,imgsz=320,conf=conf_threshold,iou=iou_threshold)[0]
image=image[:, :, ::-1].astype(np.uint8)
detections = sv.Detections.from_yolov8(results)
annotated_image = annotatormask.annotate(scene=image, detections=detections)
annotated_image = annotatorbbox.annotate(scene=annotated_image , detections=detections)
return annotated_image
'''
image_input = gr.inputs.Image() # Adjust the shape according to your requirements
inputs = [
gr.inputs.Image(label="Input Image"),
gr.Slider(
minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold"
),
gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold"),
]
outputs = gr.Image(type="filepath", label="Output Image")
title = "OCR Demo"
'''
examples = [
["ocr1.jpg", 0.6, 0.45],
["ocr2.jpg", 0.25, 0.45],
["ocr3.jpg", 0.25, 0.45],
]
outputs_images = [
["1.jpg"], # First example: an output image for the cat example
["2.jpg"] # Second example: an output image for the dog example
,["3.jpg"]
]
readme_html = """
<html>
<head>
<style>
.description {
margin: 20px;
padding: 10px;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<div class="description">
<p><strong>More details:</strong></p>
<p>We present a demo for performing object segmentation using a model trained on OCR-Receipt dataset. The model was trained on 54 training images and validated on 15 images.</p>
<p><strong>Usage:</strong></p>
<p>You can upload receipt images, and the demo will provide you with your segmented image.</p>
<p><strong>Dataset:</strong></p>
<p>This dataset comprises a total of 77 images, which are divided into three distinct sets for various purposes:</p>
<ul>
<li><strong>Training Set:</strong> It includes 54 images and is intended for training the model.</li>
<li><strong>Validation Set:</strong> There are 15 images in the validation set, which is used for optimizing model parameters during development.</li>
<li><strong>Test Set:</strong> This set consists of 8 images and serves as a separate evaluation dataset to assess the performance of trained models.</li>
</ul>
<p><strong>License:</strong> This dataset is made available under the Creative Commons Attribution 4.0 International License (CC BY 4.0).</p>
<p>To access and download this dataset, please follow this link: <a href=" https://universe.roboflow.com/study-0w9zw/ocr-receipt" target="_blank">Dataset Download</a></p>
</body>
</html>
"""
with gr.Blocks() as demo:
gr.Markdown(
"""
<div style="text-align: center;">
<h1>OCR Demo</h1>
Powered by <a href="https://Tuba.ai">Tuba</a>
</div>
"""
)
# Define the input components and add them to the layout
with gr.Row():
image_input = gr.inputs.Image()
outputs = gr.Image(type="filepath", label="Output Image")
# Define the output component and add it to the layout
with gr.Row():
conf_slider=gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.05, label="Confidence Threshold" )
with gr.Row():
IOU_Slider=gr.Slider(minimum=0.0, maximum=1.0, value=0.45, step=0.05, label="IOU Threshold")
button = gr.Button("Run")
# Define the event listener that connects the input and output components and triggers the function
button.click(fn=yolov8_inference, inputs=[image_input, conf_slider,IOU_Slider], outputs=outputs, api_name="yolov8_inference")
gr.Examples(
fn=yolov8_inference,
examples=examples,
inputs=[image_input, conf_slider,IOU_Slider],
outputs=[outputs]
)
# gr.Examples(inputs=examples, outputs=outputs_images)
# Add the description below the layout
gr.Markdown(readme_html)
# Launch the app
demo.launch(share=False)