DeltaSatellite1 commited on
Commit
bed40a0
·
verified ·
1 Parent(s): ff00a91

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +44 -69
app.py CHANGED
@@ -1,69 +1,44 @@
1
- import os
2
- os.system("pip uninstall -y mmcv-full")
3
- os.system("mim install 'mmengine>=0.6.0'")
4
- # os.system("pip install mmcv==2.0.1 -f https://download.openmmlab.com/mmcv/dist/cu118/torch2.0/index.html")
5
- os.system("mim install 'mmcv-lite==2.0.1'")
6
- os.system("mim install 'mmdet>=3.0.0,<4.0.0'")
7
- os.system("mim install 'mmyolo'")
8
- os.system("pip install -e .")
9
-
10
- import argparse
11
- import os.path as osp
12
-
13
- from mmengine.config import Config, DictAction
14
- from mmengine.runner import Runner
15
- from mmengine.dataset import Compose
16
- from mmyolo.registry import RUNNERS
17
-
18
- from tools.demo import demo
19
-
20
-
21
- def parse_args():
22
- parser = argparse.ArgumentParser(
23
- description='YOLO-World Demo')
24
- parser.add_argument('--config', default='configs/pretrain/yolo_world_xl_t2i_bn_2e-4_100e_4x8gpus_obj365v1_goldg_train_lvis_minival.py')
25
- parser.add_argument('--checkpoint', default='yolo_world_v2_xl_obj365v1_goldg_cc3mlite_pretrain.pth')
26
- parser.add_argument(
27
- '--work-dir',
28
- help='the directory to save the file containing evaluation metrics')
29
- parser.add_argument(
30
- '--cfg-options',
31
- nargs='+',
32
- action=DictAction,
33
- help='override some settings in the used config, the key-value pair '
34
- 'in xxx=yyy format will be merged into config file. If the value to '
35
- 'be overwritten is a list, it should be like key="[a,b]" or key=a,b '
36
- 'It also allows nested list/tuple values, e.g. key="[(a,b),(c,d)]" '
37
- 'Note that the quotation marks are necessary and that no white space '
38
- 'is allowed.')
39
- args = parser.parse_args()
40
- return args
41
-
42
-
43
- if __name__ == '__main__':
44
- args = parse_args()
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)