Spaces:
Sleeping
Sleeping
File size: 931 Bytes
6743685 d42ac84 6743685 e56e569 6743685 e56e569 d42ac84 6743685 e56e569 6743685 e56e569 2764f0d e56e569 2764f0d e56e569 2764f0d e56e569 2764f0d a770113 2764f0d 6743685 e56e569 |
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 |
import gradio as gr
import cv2
import numpy as np
from ultralytics import YOLO
from importlib.metadata import version
# 动态组件选择
GRADIO_VERSION = version("gradio")
if int(GRADIO_VERSION.split('.')[0]) >= 4 and int(GRADIO_VERSION.split('.')[1]) >= 12:
Webcam = gr.Webcam
else:
class Webcam(gr.Image):
def __init__(self, **kwargs):
kwargs.update(source="webcam", streaming=True)
super().__init__(**kwargs)
# YOLO模型初始化
model = None
def load_model(model_file):
global model
model = YOLO(model_file.name)
return "✅ 模型加载成功"
with gr.Blocks() as demo:
with gr.Tab("📷 实时检测"):
webcam = Webcam(label="摄像头")
output = gr.Image()
webcam.stream(
fn=lambda x: model.predict(x)[0].plot(),
inputs=webcam,
outputs=output
)
if __name__ == "__main__":
demo.launch() |