File size: 1,118 Bytes
45064fc
9699264
45064fc
 
 
 
 
 
 
f349298
45064fc
 
 
 
 
 
fe572ca
 
8f18873
45064fc
 
 
 
 
 
 
9de3aed
45064fc
 
9de3aed
 
45064fc
 
 
 
 
 
 
 
 
1
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
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()