Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -2,82 +2,35 @@ import gradio as gr
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from ultralytics import YOLO
|
5 |
-
from
|
6 |
-
import tempfile
|
7 |
-
import os
|
8 |
|
9 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
model = None
|
11 |
|
12 |
-
def
|
13 |
global model
|
14 |
-
|
15 |
-
|
16 |
-
save_path = f"/tmp/{model_file.name}"
|
17 |
-
with open(save_path, "wb") as f:
|
18 |
-
f.write(model_file.read())
|
19 |
-
|
20 |
-
# 加载模型
|
21 |
-
model = YOLO(save_path)
|
22 |
-
return "✅ 模型加载成功!"
|
23 |
-
except Exception as e:
|
24 |
-
return f"❌ 加载失败:{str(e)}"
|
25 |
-
|
26 |
-
def process_frame(frame):
|
27 |
-
if model is None:
|
28 |
-
raise gr.Error("请先上传模型文件")
|
29 |
-
|
30 |
-
results = model.predict(
|
31 |
-
source=frame,
|
32 |
-
device="cpu",
|
33 |
-
conf=0.5,
|
34 |
-
verbose=False
|
35 |
-
)
|
36 |
-
return cv2.cvtColor(results[0].plot(), cv2.COLOR_BGR2RGB)
|
37 |
|
38 |
-
|
39 |
-
with gr.Blocks(title="YOLO检测系统") as demo:
|
40 |
-
gr.Markdown("# 🔍 自定义YOLO检测系统")
|
41 |
-
|
42 |
-
with gr.Tab("🔧 模型管理"):
|
43 |
-
upload_btn = gr.UploadButton(
|
44 |
-
"上传.pt模型文件",
|
45 |
-
file_types=[".pt"],
|
46 |
-
variant="primary"
|
47 |
-
)
|
48 |
-
status = gr.Textbox(label="状态")
|
49 |
-
upload_btn.upload(load_custom_model, upload_btn, status)
|
50 |
-
|
51 |
with gr.Tab("📷 实时检测"):
|
52 |
-
webcam =
|
53 |
-
|
54 |
-
streaming=True,
|
55 |
-
label="摄像头画面"
|
56 |
-
)
|
57 |
-
output = gr.Image(label="检测结果")
|
58 |
webcam.stream(
|
59 |
-
fn=lambda x:
|
60 |
inputs=webcam,
|
61 |
outputs=output
|
62 |
)
|
63 |
-
|
64 |
-
with gr.Tab("🖼️ 图片检测"):
|
65 |
-
img_input = gr.Image(type="filepath")
|
66 |
-
img_output = gr.Image()
|
67 |
-
img_input.change(
|
68 |
-
fn=lambda x: process_frame(cv2.imread(x)),
|
69 |
-
inputs=img_input,
|
70 |
-
outputs=img_output
|
71 |
-
)
|
72 |
-
|
73 |
-
with gr.Tab("🎥 视频检测"):
|
74 |
-
vid_input = gr.Video()
|
75 |
-
vid_output = gr.Video()
|
76 |
-
vid_input.change(
|
77 |
-
fn=lambda x: video_pipeline(x),
|
78 |
-
inputs=vid_input,
|
79 |
-
outputs=vid_output
|
80 |
-
)
|
81 |
|
82 |
if __name__ == "__main__":
|
83 |
-
demo.launch(
|
|
|
2 |
import cv2
|
3 |
import numpy as np
|
4 |
from ultralytics import YOLO
|
5 |
+
from importlib.metadata import version
|
|
|
|
|
6 |
|
7 |
+
# 动态组件选择
|
8 |
+
GRADIO_VERSION = version("gradio")
|
9 |
+
if int(GRADIO_VERSION.split('.')[0]) >= 4 and int(GRADIO_VERSION.split('.')[1]) >= 12:
|
10 |
+
Webcam = gr.Webcam
|
11 |
+
else:
|
12 |
+
class Webcam(gr.Image):
|
13 |
+
def __init__(self, **kwargs):
|
14 |
+
kwargs.update(source="webcam", streaming=True)
|
15 |
+
super().__init__(**kwargs)
|
16 |
+
|
17 |
+
# YOLO模型初始化
|
18 |
model = None
|
19 |
|
20 |
+
def load_model(model_file):
|
21 |
global model
|
22 |
+
model = YOLO(model_file.name)
|
23 |
+
return "✅ 模型加载成功"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
+
with gr.Blocks() as demo:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
with gr.Tab("📷 实时检测"):
|
27 |
+
webcam = Webcam(label="摄像头")
|
28 |
+
output = gr.Image()
|
|
|
|
|
|
|
|
|
29 |
webcam.stream(
|
30 |
+
fn=lambda x: model.predict(x)[0].plot(),
|
31 |
inputs=webcam,
|
32 |
outputs=output
|
33 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
if __name__ == "__main__":
|
36 |
+
demo.launch()
|