Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,69 +1,44 @@
|
|
| 1 |
-
import
|
| 2 |
-
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
# load config
|
| 47 |
-
cfg = Config.fromfile(args.config)
|
| 48 |
-
if args.cfg_options is not None:
|
| 49 |
-
cfg.merge_from_dict(args.cfg_options)
|
| 50 |
-
|
| 51 |
-
if args.work_dir is not None:
|
| 52 |
-
cfg.work_dir = args.work_dir
|
| 53 |
-
elif cfg.get('work_dir', None) is None:
|
| 54 |
-
cfg.work_dir = osp.join('./work_dirs',
|
| 55 |
-
osp.splitext(osp.basename(args.config))[0])
|
| 56 |
-
|
| 57 |
-
cfg.load_from = args.checkpoint
|
| 58 |
-
|
| 59 |
-
if 'runner_type' not in cfg:
|
| 60 |
-
runner = Runner.from_cfg(cfg)
|
| 61 |
-
else:
|
| 62 |
-
runner = RUNNERS.build(cfg)
|
| 63 |
-
|
| 64 |
-
runner.call_hook('before_run')
|
| 65 |
-
runner.load_or_resume()
|
| 66 |
-
pipeline = cfg.test_dataloader.dataset.pipeline
|
| 67 |
-
runner.pipeline = Compose(pipeline)
|
| 68 |
-
runner.model.eval()
|
| 69 |
-
demo(runner, args, cfg)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import PIL.Image as Image
|
| 3 |
+
|
| 4 |
+
from ultralytics import ASSETS, YOLO
|
| 5 |
+
|
| 6 |
+
model = None
|
| 7 |
+
|
| 8 |
+
|
| 9 |
+
def predict_image(img, conf_threshold, iou_threshold, model_name):
|
| 10 |
+
"""Predicts objects in an image using a YOLOv8 model with adjustable confidence and IOU thresholds."""
|
| 11 |
+
model = YOLO(model_name)
|
| 12 |
+
results = model.predict(
|
| 13 |
+
source=img,
|
| 14 |
+
conf=conf_threshold,
|
| 15 |
+
iou=iou_threshold,
|
| 16 |
+
show_labels=True,
|
| 17 |
+
show_conf=True,
|
| 18 |
+
imgsz=640,
|
| 19 |
+
)
|
| 20 |
+
|
| 21 |
+
for r in results:
|
| 22 |
+
im_array = r.plot()
|
| 23 |
+
im = Image.fromarray(im_array[..., ::-1])
|
| 24 |
+
|
| 25 |
+
return im
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
iface = gr.Interface(
|
| 29 |
+
fn=predict_image,
|
| 30 |
+
inputs=[
|
| 31 |
+
gr.Image(type="pil", label="Upload Image"),
|
| 32 |
+
gr.Slider(minimum=0, maximum=1, value=0.25, label="Confidence threshold"),
|
| 33 |
+
gr.Slider(minimum=0, maximum=1, value=0.45, label="IoU threshold"),
|
| 34 |
+
gr.Radio(choices=["yolo11n", "yolo11s", "yolo11n-seg", "yolo11s-seg", "yolo11n-pose", "yolo11s-pose"], label="Model Name", value="yolo11n"),
|
| 35 |
+
],
|
| 36 |
+
outputs=gr.Image(type="pil", label="Result"),
|
| 37 |
+
title="Ultralytics Gradio Application 🚀",
|
| 38 |
+
description="Upload images for inference. The Ultralytics YOLO11n model is used by default.",
|
| 39 |
+
examples=[
|
| 40 |
+
[ASSETS / "bus.jpg", 0.25, 0.45, "yolo11n.pt"],
|
| 41 |
+
[ASSETS / "zidane.jpg", 0.25, 0.45, "yolo11n.pt"],
|
| 42 |
+
],
|
| 43 |
+
)
|
| 44 |
+
iface.launch(share=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|