Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,87 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from ultralytics import YOLO
|
5 |
+
from pathlib import Path
|
6 |
+
import tempfile
|
7 |
+
|
8 |
+
# 初始化模型占位符
|
9 |
+
model = YOLO('yolov8n.pt') # 默认加载官方模型
|
10 |
+
|
11 |
+
def load_custom_model(model_path):
|
12 |
+
global model
|
13 |
+
model = YOLO(model_path)
|
14 |
+
return "模型加载成功!"
|
15 |
+
|
16 |
+
def detect_media(input_type, input_data):
|
17 |
+
if input_type == "camera":
|
18 |
+
# 摄像头实时检测
|
19 |
+
frame = input_data
|
20 |
+
results = model.predict(source=frame, stream=True)
|
21 |
+
annotated_frame = results[0].plot()
|
22 |
+
return annotated_frame[:, :, ::-1] # BGR转RGB
|
23 |
+
|
24 |
+
elif input_type == "image":
|
25 |
+
# 图片检测
|
26 |
+
results = model.predict(source=input_data)
|
27 |
+
return results[0].plot()[:, :, ::-1]
|
28 |
+
|
29 |
+
elif input_type == "video":
|
30 |
+
# 视频检测处理
|
31 |
+
cap = cv2.VideoCapture(input_data)
|
32 |
+
output_frames = []
|
33 |
+
|
34 |
+
while cap.isOpened():
|
35 |
+
ret, frame = cap.read()
|
36 |
+
if not ret: break
|
37 |
+
|
38 |
+
results = model.predict(source=frame)
|
39 |
+
annotated_frame = results[0].plot()
|
40 |
+
output_frames.append(annotated_frame)
|
41 |
+
|
42 |
+
# 生成临时输出视频
|
43 |
+
output_path = str(Path(tempfile.gettempdir()) / "output.mp4")
|
44 |
+
out = cv2.VideoWriter(output_path, cv2.VideoWriter_fourcc(*'mp4v'),
|
45 |
+
30, (annotated_frame.shape[1], annotated_frame.shape[0]))
|
46 |
+
for f in output_frames:
|
47 |
+
out.write(f)
|
48 |
+
out.release()
|
49 |
+
|
50 |
+
return output_path
|
51 |
+
|
52 |
+
# Gradio界面布局
|
53 |
+
with gr.Blocks(title="YOLOv8检测系统") as demo:
|
54 |
+
gr.Markdown("# 🚀 YOLOv8多功能检测系统")
|
55 |
+
|
56 |
+
with gr.Tab("⚙️ 模型管理"):
|
57 |
+
model_upload = gr.File(label="上传模型文件(.pt)", file_types=[".pt"])
|
58 |
+
load_btn = gr.Button("加载模型", variant="primary")
|
59 |
+
model_status = gr.Textbox(label="模型状态")
|
60 |
+
|
61 |
+
with gr.Tab("📷 实时摄像头"):
|
62 |
+
webcam = gr.Image(source="webcam", streaming=True, label="摄像头画面")
|
63 |
+
cam_output = gr.Image(label="检测结果", interactive=False)
|
64 |
+
webcam.stream(fn=lambda x: detect_media("camera", x),
|
65 |
+
inputs=webcam, outputs=cam_output)
|
66 |
+
|
67 |
+
with gr.Tab("🖼️ 图片检测"):
|
68 |
+
img_input = gr.Image(type="filepath", label="上传图片")
|
69 |
+
img_btn = gr.Button("开始检测", variant="primary")
|
70 |
+
img_output = gr.Image(label="检测结果")
|
71 |
+
|
72 |
+
with gr.Tab("🎥 视频检测"):
|
73 |
+
vid_input = gr.Video(label="上传视频")
|
74 |
+
vid_btn = gr.Button("处理视频", variant="primary")
|
75 |
+
vid_output = gr.Video(label="处理结果")
|
76 |
+
|
77 |
+
# 事件绑定
|
78 |
+
load_btn.click(fn=load_custom_model, inputs=model_upload, outputs=model_status)
|
79 |
+
img_btn.click(fn=lambda x: detect_media("image", x), inputs=img_input, outputs=img_output)
|
80 |
+
vid_btn.click(fn=lambda x: detect_media("video", x), inputs=vid_input, outputs=vid_output)
|
81 |
+
|
82 |
+
if __name__ == "__main__":
|
83 |
+
demo.queue(concurrency_count=3).launch(
|
84 |
+
server_name="0.0.0.0",
|
85 |
+
server_port=7860,
|
86 |
+
share=True
|
87 |
+
)
|