Download app.py from olive100/detect: direct link, hf CLI and curl.
- Browser
- Download file 1.12 kB
-
https://huggingface.co/spaces/olive100/detect/resolve/main/app.py
- Command line
-
hf download hf://spaces/olive100/detect/app.py
-
curl -L -o app.py https://huggingface.co/spaces/olive100/detect/resolve/main/app.py
1.12 kB
| import gradio as gr | |
| from ultralytics.yolo.data import utils | |
| import ultralytics | |
| from pathlib import Path | |
| from torchkeras import plots | |
| model = ultralytics.YOLO('yolov8n.pt') | |
| #load class_names | |
| yaml_path = str('coco128.yaml') | |
| class_names = utils.yaml_load(yaml_path)['names'] | |
| def detect(img): | |
| if isinstance(img,str): | |
| img = get_url_img(img) if img.startswith('http') else Image.open(img).convert('RGB') | |
| result = model.predict(source=img) | |
| if len(result[0].boxes.data)>0: | |
| vis = plots.plot_detection(img,boxes=result[0].boxes.data, | |
| class_names=class_names, min_score=0.2) | |
| else: | |
| vis = img | |
| return vis | |
| with gr.Blocks() as demo: | |
| gr.Markdown("# yolov8目标检测演示") | |
| with gr.Row(): | |
| in_img = gr.Image(source='webcam',type='pil') | |
| out_img = gr.Image(type='pil') | |
| with gr.Row(): | |
| button = gr.Button("执行检测",variant="primary") | |
| button.click(detect, | |
| inputs=in_img, | |
| outputs=out_img) | |
| gr.close_all() | |
| demo.queue(concurrency_count=5) | |
| demo.launch() | |