lyhue1991 commited on
Commit
45b3f1c
1 Parent(s): 730d26f

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +88 -0
app.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import pandas as pd
3
+
4
+ from skimage import data
5
+ from PIL import Image
6
+ from torchkeras import plots
7
+ from torchkeras.data import get_url_img
8
+
9
+ from pathlib import Path
10
+ from ultralytics import YOLO
11
+ import ultralytics
12
+ from ultralytics.yolo.data import utils
13
+
14
+ model = YOLO('yolov8n.pt')
15
+
16
+ #prepare example images
17
+ Image.fromarray(data.coffee()).save('coffee.jpeg')
18
+ Image.fromarray(data.astronaut()).save('people.jpeg')
19
+ Image.fromarray(data.cat()).save('cat.jpeg')
20
+
21
+ #load class_names
22
+ yaml_path = str(Path(ultralytics.__file__).parent/'datasets/coco128.yaml')
23
+ class_names = utils.yaml_load(yaml_path)['names']
24
+
25
+ def detect(img):
26
+ if isinstance(img,str):
27
+ img = get_url_img(img) if img.startswith('http') else Image.open(img).convert('RGB')
28
+ result = model.predict(source=img)
29
+ if len(result[0].boxes.boxes)>0:
30
+ vis = plots.plot_detection(img,boxes=result[0].boxes.boxes,
31
+ class_names=class_names, min_score=0.2)
32
+ else:
33
+ vis = img
34
+ return vis
35
+
36
+ with gr.Blocks() as demo:
37
+ gr.Markdown("# yolov8目标检测演示")
38
+
39
+ with gr.Tab("选择测试图片"):
40
+ files = ['people.jpeg','coffee.jpeg','cat.jpeg']
41
+ drop_down = gr.Dropdown(choices=files,value=files[0])
42
+ button = gr.Button("执行检测",variant="primary")
43
+
44
+
45
+ gr.Markdown("## 预测输出")
46
+ out_img = gr.Image(type='pil')
47
+
48
+ button.click(detect,
49
+ inputs=drop_down,
50
+ outputs=out_img)
51
+
52
+ with gr.Tab("输入图片链接"):
53
+ default_url = 'https://t7.baidu.com/it/u=3601447414,1764260638&fm=193&f=GIF'
54
+ url = gr.Textbox(value=default_url)
55
+ button = gr.Button("执行检测",variant="primary")
56
+
57
+ gr.Markdown("## 预测输出")
58
+ out_img = gr.Image(type='pil')
59
+
60
+ button.click(detect,
61
+ inputs=url,
62
+ outputs=out_img)
63
+
64
+ with gr.Tab("上传本地图片"):
65
+ input_img = gr.Image(type='pil')
66
+ button = gr.Button("执行检测",variant="primary")
67
+
68
+ gr.Markdown("## 预测输出")
69
+ out_img = gr.Image(type='pil')
70
+
71
+ button.click(detect,
72
+ inputs=input_img,
73
+ outputs=out_img)
74
+
75
+ with gr.Tab("捕捉摄像头喔"):
76
+ input_img = gr.Image(source='webcam',type='pil')
77
+ button = gr.Button("执行检测",variant="primary")
78
+
79
+ gr.Markdown("## 预测输出")
80
+ out_img = gr.Image(type='pil')
81
+
82
+ button.click(detect,
83
+ inputs=input_img,
84
+ outputs=out_img)
85
+
86
+ gr.close_all()
87
+ demo.queue()
88
+ demo.launch()