|
import supervision as sv |
|
import gradio as gr |
|
from ultralytics import YOLO |
|
import sahi |
|
|
|
|
|
|
|
|
|
|
|
|
|
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, |
|
model_name: gr.inputs.Dropdown = None, |
|
image_size: gr.inputs.Slider = 320, |
|
conf_threshold: gr.inputs.Slider = 0.25, |
|
iou_threshold: gr.inputs.Slider = 0.45, |
|
): |
|
|
|
|
|
results = model(image,conf=conf_threshold,iou=iou_threshold ,imgsz=320)[0] |
|
detections = sv.Detections.from_yolov8(results) |
|
annotated_image = annotatorbbox.annotate(scene=image, detections=detections) |
|
annotated_image = annotatormask.annotate(scene=annotated_image, detections=detections) |
|
|
|
|
|
|
|
|
|
return annotated_image |
|
|
|
image_input = gr.inputs.Image() |
|
|
|
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 = "YOLOv8 Segmentation Demo Receipt" |
|
examples = [ |
|
["ocr1.jpg", 0.6, 0.45], |
|
["ocr2.jpg", 0.25, 0.45], |
|
["ocr3.jpg", 0.25, 0.45], |
|
] |
|
demo_app = gr.Interface(examples=examples, |
|
fn=yolov8_inference, |
|
inputs=inputs, |
|
outputs=outputs, |
|
title=title, |
|
cache_examples=True, |
|
theme="default", |
|
) |
|
demo_app.launch(debug=False, enable_queue=True) |