Commit
·
d40cf0a
1
Parent(s):
cff4932
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from sahi.prediction import ObjectPrediction
|
| 4 |
+
from sahi.utils.cv import visualize_object_predictions, read_image
|
| 5 |
+
from ultralyticsplus import YOLO, render_result
|
| 6 |
+
|
| 7 |
+
def yolov8_inference(
|
| 8 |
+
image: gr.inputs.Image = None,
|
| 9 |
+
model_path: gr.inputs.Dropdown = None,
|
| 10 |
+
image_size: gr.inputs.Slider = 640,
|
| 11 |
+
conf_threshold: gr.inputs.Slider = 0.25,
|
| 12 |
+
iou_threshold: gr.inputs.Slider = 0.45,
|
| 13 |
+
):
|
| 14 |
+
"""
|
| 15 |
+
YOLOv8 inference function
|
| 16 |
+
Args:
|
| 17 |
+
image: Input image
|
| 18 |
+
model_path: Path to the model
|
| 19 |
+
image_size: Image size
|
| 20 |
+
conf_threshold: Confidence threshold
|
| 21 |
+
iou_threshold: IOU threshold
|
| 22 |
+
Returns:
|
| 23 |
+
Rendered image
|
| 24 |
+
"""
|
| 25 |
+
model = YOLO(model_path)
|
| 26 |
+
model.overrides['conf'] = conf_threshold
|
| 27 |
+
model.overrides['iou']= iou_threshold
|
| 28 |
+
model.overrides['agnostic_nms'] = False # NMS class-agnostic
|
| 29 |
+
model.overrides['max_det'] = 1000
|
| 30 |
+
image = read_image(image)
|
| 31 |
+
results = model.predict(image)
|
| 32 |
+
render = render_result(model=model, image=image, result=results[0])
|
| 33 |
+
|
| 34 |
+
return render
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
inputs = [
|
| 38 |
+
gr.inputs.Image(type="filepath", label="Input Image"),
|
| 39 |
+
gr.inputs.Dropdown(["foduucom/web-form-ui-field-detection"],
|
| 40 |
+
default="foduucom/web-form-ui-field-detection", label="Model"),
|
| 41 |
+
gr.inputs.Slider(minimum=320, maximum=1280, default=640, step=32, label="Image Size"),
|
| 42 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.25, step=0.05, label="Confidence Threshold"),
|
| 43 |
+
gr.inputs.Slider(minimum=0.0, maximum=1.0, default=0.45, step=0.05, label="IOU Threshold"),
|
| 44 |
+
]
|
| 45 |
+
|
| 46 |
+
outputs = gr.outputs.Image(type="filepath", label="Output Image")
|
| 47 |
+
title = "Ui form : web form ui field Detection in Images"
|
| 48 |
+
|
| 49 |
+
demo_app = gr.Interface(
|
| 50 |
+
fn=yolov8_inference,
|
| 51 |
+
inputs=inputs,
|
| 52 |
+
outputs=outputs,
|
| 53 |
+
title=title,
|
| 54 |
+
|
| 55 |
+
cache_examples=True,
|
| 56 |
+
theme='huggingface',
|
| 57 |
+
)
|
| 58 |
+
demo_app.launch(debug=True, enable_queue=True)
|