File size: 2,048 Bytes
45b3f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fe319
 
 
45b3f1c
 
 
 
 
 
99fe319
45b3f1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
99fe319
45b3f1c
 
 
 
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import gradio as gr 
import pandas as pd 

from PIL import Image
from torchkeras import plots 
from torchkeras.data import get_url_img

from pathlib import Path
from ultralytics import YOLO
import ultralytics
from ultralytics.yolo.data import utils 

model = YOLO('yolov8n.pt')


#load class_names
yaml_path = str(Path(ultralytics.__file__).parent/'datasets/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.boxes)>0:
        vis = plots.plot_detection(img,boxes=result[0].boxes.boxes,
                     class_names=class_names, min_score=0.2)
    else:
        vis = img
    return vis

with gr.Blocks() as demo:

    with gr.Tab("捕捉摄像倴喔"):
        input_img = gr.Image(source='webcam',type='pil')
        button = gr.Button("ζ‰§θ‘Œζ£€ζ΅‹",variant="primary")
        
        gr.Markdown("## ι’„ζ΅‹θΎ“ε‡Ί")
        out_img = gr.Image(type='pil')
        
        button.click(detect,
                     inputs=input_img, 
                     outputs=out_img)
        
    with gr.Tab("θΎ“ε…₯图片链ζŽ₯"):
        default_url = 'https://t7.baidu.com/it/u=3601447414,1764260638&fm=193&f=GIF'
        url = gr.Textbox(value=default_url)
        button = gr.Button("ζ‰§θ‘Œζ£€ζ΅‹",variant="primary")
        
        gr.Markdown("## ι’„ζ΅‹θΎ“ε‡Ί")
        out_img = gr.Image(type='pil')
        
        button.click(detect,
                     inputs=url, 
                     outputs=out_img)
        
    with gr.Tab("δΈŠδΌ ζœ¬εœ°ε›Ύη‰‡"):
        input_img = gr.Image(type='pil')
        button = gr.Button("ζ‰§θ‘Œζ£€ζ΅‹",variant="primary")
        
        gr.Markdown("## ι’„ζ΅‹θΎ“ε‡Ί")
        out_img = gr.Image(type='pil')
        
        button.click(detect,
                     inputs=input_img, 
                     outputs=out_img)
        

        
gr.close_all() 
demo.queue()
demo.launch()