ultralytics
Eval Results
File size: 7,627 Bytes
8c738c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5cdf41
8c738c4
 
 
 
 
 
 
c5cdf41
 
 
 
 
 
8c738c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5cdf41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c738c4
c5cdf41
8c738c4
c5cdf41
 
 
 
 
 
 
 
 
 
 
8c738c4
 
c5cdf41
 
 
 
 
8c738c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c5cdf41
8c738c4
 
 
 
c5cdf41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8c738c4
 
 
 
 
 
 
 
 
 
 
 
c5cdf41
 
 
 
 
8c738c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import os
import sys
from typing import Optional

import av
import torch
import streamlit as st
from streamlit_webrtc import webrtc_streamer, VideoProcessorBase, RTCConfiguration

try:
    from ultralytics import YOLO
except Exception as e:
    st.error(f"Ultralytics import failed: {e}")
    raise


st.set_page_config(page_title="YOLO11 Webcam", layout="wide")
st.title("YOLO11 Webcam Demo")
st.caption("Live object detection with YOLO11 using your webcam. Use the sidebar to configure the model and thresholds.")


def _resolve_device(device: str) -> str:
    """Map UI device option to a valid torch/Ultralytics device string."""
    d = (device or "").lower().strip()
    if d in {"cpu", "cuda", "mps", "xpu", "dml"}:
        return d
    if d == "auto":
        if torch.cuda.is_available():
            return "cuda"
        # Apple MPS (macOS). Kept for completeness even if not typical on Windows.
        if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
            return "mps"
        # Windows DirectML (requires torch-directml)
        try:
            import torch_directml  # noqa: F401
            return "dml"
        except Exception:
            pass
        return "cpu"
    # Fallback to CPU for any unknown value
    return "cpu"


@st.cache_resource(show_spinner=True)
def load_model(model_source: str, device: str = "auto"):
    """
    Load an Ultralytics model from a local path, built-in alias, or Hugging Face Hub path.
    Examples for model_source:
      - "yolo11n.pt" (auto-downloads if available)
      - "C:/path/to/your_model.pt"
      - "hf://Ultralytics/YOLO11/yolo11n.pt" (HF Hub file)
    """
    # Resolve device (maps 'auto' to an actual device)
    resolved_device = _resolve_device(device)

    # Gentle hint for common mistake: loading a multi-model container instead of a single YOLO weight
    try:
        base_name = os.path.basename(model_source)
        if base_name.lower().startswith("models_multi"):
            st.info("Seçilen .pt birden fazla ağırlık içeren bir paket olabilir. Lütfen tek bir YOLO ağırlık dosyası seçin (ör. yolo11n.pt).")
    except Exception:
        pass

    # 1) Kaynağı yerel dosyaya indir/çöz ve YOLO nesnesini oluştur
    def _create_yolo(path_or_alias: str):
        if path_or_alias.startswith("hf://"):
            try:
                from huggingface_hub import hf_hub_download
            except Exception:
                st.error("huggingface_hub yüklü değil. Yükleyin veya yerel/yerleşik model kullanın.")
                raise
            path = path_or_alias.replace("hf://", "", 1)
            if "/" not in path:
                raise ValueError("hf:// için hf://<repo_id>/<filename> biçimini kullanın")
            repo_id, filename = path.split("/", 1)
            st.info(f"{repo_id} deposundan {filename} indiriliyor…")
            local_path = hf_hub_download(repo_id=repo_id, filename=filename, token=os.getenv("HF_TOKEN"))
            return YOLO(local_path)
        else:
            return YOLO(path_or_alias)

    # Önce modeli yüklemeyi deneyin (hata ==> dosya/format sorunu)
    try:
        model = _create_yolo(model_source)
    except Exception as e:
        st.error(
            "Model dosyası yüklenemedi. Bu dosya geçerli bir YOLO ağırlığı olmayabilir (örn. eğitim checkpoint'i veya paketlenmiş container).\n"
            f"Detay: {e}"
        )
        raise

    # 2) Sonra cihaza taşımayı deneyin (hata ==> CUDA/MPS taşınamıyor → CPU'ya düş)
    try:
        return model.to(resolved_device)
    except Exception as e:
        st.warning(f"{resolved_device} cihaza taşınamadı, CPU'ya düşülüyor. Detay: {e}")
        try:
            return model.to("cpu")
        except Exception:
            # CPU'ya taşınma da başarısızsa, ham modeli döndür (son çare)
            return model


class YOLOProcessor(VideoProcessorBase):
    def __init__(self, model: YOLO, conf: float, iou: float):
        self.model = model
        self.conf = conf
        self.iou = iou

    def recv(self, frame: av.VideoFrame) -> av.VideoFrame:
        img = frame.to_ndarray(format="bgr24")
        results = self.model.predict(img, conf=self.conf, iou=self.iou, verbose=False)
        plotted = results[0].plot()
        return av.VideoFrame.from_ndarray(plotted, format="bgr24")


with st.sidebar:
    st.header("Ayarlar")
    default_model = "yolo11n.pt"  # Ultralytics dağıtımından otomatik indirilmeye çalışılır
    model_path = st.text_input("Model yolu veya alias", value=default_model, help="Yerel .pt yolu, yerleşik alias (örn. yolo11n.pt) veya hf://Ultralytics/YOLO11/yolo11n.pt")
    device = st.selectbox("Cihaz", options=["auto", "cpu", "cuda", "dml"], index=0)
    conf = st.slider("Confidence", min_value=0.1, max_value=0.9, value=0.25, step=0.05)
    iou = st.slider("IoU", min_value=0.1, max_value=0.9, value=0.45, step=0.05)
    load_btn = st.button("Modeli Yükle")

    # Ortam durumu (Torch/CUDA)
    try:
        tv = torch.__version__
        cv = getattr(torch.version, "cuda", None)
        ca = torch.cuda.is_available()
        # Detect DirectML
        has_dml = False
        try:
            import torch_directml  # noqa: F401
            has_dml = True
        except Exception:
            has_dml = False
        dml_txt = " • DirectML: Hazır" if has_dml else ""
        st.caption(f"Torch {tv} • CUDA: {cv or 'yok'} • GPU etkin (CUDA): {'Evet' if ca else 'Hayır'}{dml_txt}")
    except Exception:
        pass


st.session_state.setdefault("model", None)


if load_btn:
    try:
        st.session_state.model = load_model(model_path, device=device)
        names = getattr(st.session_state.model, "names", None)
        if isinstance(names, dict):
            st.success(f"Model yüklendi. Sınıflar: {len(names)}")
        else:
            st.success("Model yüklendi.")
        try:
            active_device = next((p.device.type for p in st.session_state.model.parameters()), "unknown")
            st.info(f"Aktif cihaz: {active_device}")
        except Exception:
            pass
    except Exception as e:
        st.exception(e)


rtc_config = RTCConfiguration({
    "iceServers": [{"urls": ["stun:stun.l.google.com:19302"]}],
})


col1, col2 = st.columns([2, 1])
with col1:
    st.subheader("Webcam")
    if st.session_state.get("model") is None:
        st.info("Önce soldan bir model yükleyin.")
    else:
        # Capture current values to avoid session_state race during worker creation
        _model = st.session_state.get("model")
        _conf = float(conf)
        _iou = float(iou)

        def video_processor_factory(model=_model, conf_val=_conf, iou_val=_iou):
            return YOLOProcessor(model, conf=conf_val, iou=iou_val)

        webrtc_streamer(
            key="yolo11-webcam",
            video_processor_factory=video_processor_factory,
            rtc_configuration=rtc_config,
            media_stream_constraints={"video": True, "audio": False},
        )

with col2:
    st.subheader("İpuçları")
    st.markdown(
        """
        - Model alanına şunlardan birini girebilirsiniz:
            - Yerleşik: `yolo11n.pt` (veya sizde olan başka bir .pt)
            - Yerel dosya: `C:/yolov11/weights/custom.pt`
            - HF Hub: `hf://Ultralytics/YOLO11/yolo11n.pt`
        - Hugging Face özel/korumalı dosyalar için `HF_TOKEN` ortam değişkenini ayarlayabilirsiniz.
        - GPU (CUDA) yoksa cihazı `cpu` bırakabilirsiniz.
        - Stream durmuyorsa tarayıcı izinlerini kontrol edin ve sayfayı yenileyin.
        """
    )