Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from skimage import data
|
| 4 |
+
from ultralytics.yolo.data import utils
|
| 5 |
+
import ultralytics
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
from torchkeras import plots
|
| 8 |
+
|
| 9 |
+
model = ultralytics.YOLO('yolov8n.pt')
|
| 10 |
+
|
| 11 |
+
#load class_names
|
| 12 |
+
yaml_path = str(Path(ultralytics.__file__).parent/'datasets/coco128.yaml')
|
| 13 |
+
class_names = utils.yaml_load(yaml_path)['names']
|
| 14 |
+
|
| 15 |
+
def detect(img):
|
| 16 |
+
if isinstance(img,str):
|
| 17 |
+
img = get_url_img(img) if img.startswith('http') else Image.open(img).convert('RGB')
|
| 18 |
+
result = model.predict(source=img)
|
| 19 |
+
if len(result[0].boxes.boxes)>0:
|
| 20 |
+
vis = plots.plot_detection(img,boxes=result[0].boxes.boxes,
|
| 21 |
+
class_names=class_names, min_score=0.2)
|
| 22 |
+
else:
|
| 23 |
+
vis = img
|
| 24 |
+
return vis
|
| 25 |
+
|
| 26 |
+
with gr.Blocks() as demo:
|
| 27 |
+
gr.Markdown("# yolov8目标检测演示")
|
| 28 |
+
|
| 29 |
+
with gr.Tab("捕捉摄像头喔"):
|
| 30 |
+
in_img = gr.Image(source='webcam',type='pil')
|
| 31 |
+
button = gr.Button("执行检测",variant="primary")
|
| 32 |
+
|
| 33 |
+
gr.Markdown("## 预测输出")
|
| 34 |
+
out_img = gr.Image(type='pil')
|
| 35 |
+
|
| 36 |
+
button.click(detect,
|
| 37 |
+
inputs=in_img,
|
| 38 |
+
outputs=out_img)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
gr.close_all()
|
| 42 |
+
demo.queue(concurrency_count=5)
|
| 43 |
+
demo.launch()
|