Files
web_yolo_control/templates/index.html
T

568 lines
17 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>YOLO Stream Control</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
background: #0f0f0f;
color: #e0e0e0;
font-family: 'Segoe UI', system-ui, sans-serif;
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
h1 {
font-size: 1.5rem;
font-weight: 300;
letter-spacing: 2px;
text-transform: uppercase;
color: #888;
margin-bottom: 20px;
}
.container {
max-width: 1100px;
width: 100%;
display: grid;
grid-template-columns: 1fr 320px;
gap: 20px;
}
.stream-section {
background: #1a1a1a;
border-radius: 12px;
overflow: hidden;
border: 1px solid #2a2a2a;
}
.stream-section img {
width: 100%;
display: block;
aspect-ratio: 4/3;
object-fit: contain;
background: #000;
}
.controls {
display: flex;
flex-direction: column;
gap: 16px;
}
.panel {
background: #1a1a1a;
border-radius: 12px;
padding: 16px;
border: 1px solid #2a2a2a;
}
.panel h2 {
font-size: 0.75rem;
font-weight: 600;
text-transform: uppercase;
letter-spacing: 1px;
color: #666;
margin-bottom: 12px;
}
.stat-grid {
display: grid;
grid-template-columns: 1fr 1fr;
gap: 12px;
}
.stat {
background: #242424;
border-radius: 8px;
padding: 12px;
text-align: center;
}
.stat .value {
font-size: 1.5rem;
font-weight: 600;
color: #4fc3f7;
}
.stat .label {
font-size: 0.7rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: #777;
margin-top: 4px;
}
select, input[type="text"], button {
width: 100%;
padding: 10px 12px;
background: #242424;
border: 1px solid #333;
border-radius: 8px;
color: #e0e0e0;
font-size: 0.9rem;
outline: none;
transition: border-color 0.2s;
}
select:focus, input[type="text"]:focus {
border-color: #4fc3f7;
}
select option { background: #242424; }
.btn {
background: #4fc3f7;
color: #000;
border: none;
font-weight: 600;
cursor: pointer;
transition: background 0.2s;
}
.btn:hover { background: #39b0e4; }
.btn:active { background: #2a9fd4; }
.btn.secondary {
background: #333;
color: #e0e0e0;
}
.btn.secondary:hover { background: #444; }
.input-group {
display: flex;
gap: 8px;
}
.input-group input { flex: 1; }
.input-group button { width: auto; white-space: nowrap; padding: 10px 16px; }
.tag {
display: inline-block;
padding: 2px 8px;
border-radius: 4px;
font-size: 0.7rem;
font-weight: 600;
margin-left: 6px;
}
.tag.yolo { background: #1b5e20; color: #81c784; }
.tag.yoloe { background: #4a148c; color: #ce93d8; }
.badge {
display: inline-block;
padding: 4px 10px;
border-radius: 20px;
font-size: 0.7rem;
background: #1a3a1a;
color: #81c784;
margin-top: 8px;
}
.badge.offline { background: #3a1a1a; color: #ef9a9a; }
#status-bar {
display: flex;
align-items: center;
gap: 8px;
margin-bottom: 16px;
font-size: 0.8rem;
color: #888;
}
#status-bar .dot {
width: 8px;
height: 8px;
border-radius: 50%;
background: #4caf50;
display: inline-block;
}
#status-bar .dot.offline { background: #f44336; }
.classes-area textarea {
width: 100%;
padding: 10px 12px;
background: #242424;
border: 1px solid #333;
border-radius: 8px;
color: #e0e0e0;
font-size: 0.85rem;
resize: vertical;
min-height: 80px;
outline: none;
font-family: inherit;
}
.classes-area textarea:focus { border-color: #ce93d8; }
.toast {
position: fixed;
bottom: 20px;
right: 20px;
padding: 12px 20px;
border-radius: 8px;
background: #333;
color: #e0e0e0;
font-size: 0.85rem;
opacity: 0;
transition: opacity 0.3s;
pointer-events: none;
z-index: 100;
}
.toast.show { opacity: 1; }
.toast.success { background: #1b5e20; color: #a5d6a7; }
.toast.error { background: #b71c1c; color: #ef9a9a; }
.toast.info { background: #0d47a1; color: #90caf9; }
@media (max-width: 768px) {
.container { grid-template-columns: 1fr; }
}
</style>
</head>
<body>
<h1>YOLO Stream Controller</h1>
<div id="status-bar">
<span class="dot" id="status-dot"></span>
<span id="status-text">Connecting...</span>
</div>
<div class="container">
<div class="stream-section">
<img id="stream" src="/video_feed" alt="YOLO Stream">
</div>
<div class="controls">
<div class="panel">
<h2>Model</h2>
<select id="model-select"></select>
<div style="margin-top:8px;display:flex;gap:8px;">
<button class="btn" id="apply-model" style="flex:1;">Apply Model</button>
<button class="btn secondary" id="refresh-models" style="width:40px;">&#x21bb;</button>
</div>
<div id="model-badge" class="badge">yolo26n.pt</div>
</div>
<div class="panel">
<h2>Status</h2>
<div class="stat-grid">
<div class="stat">
<div class="value" id="stat-fps">0</div>
<div class="label">FPS</div>
</div>
<div class="stat">
<div class="value" id="stat-dets">0</div>
<div class="label">Detections</div>
</div>
</div>
</div>
<div class="panel">
<h2>Tracking</h2>
<label style="display:flex;align-items:center;gap:10px;cursor:pointer;padding:4px 0;">
<input type="checkbox" id="track-toggle" style="width:18px;height:18px;accent-color:#4fc3f7;">
<span style="font-size:0.9rem;">Enable tracking (<code>model.track()</code>)</span>
</label>
<div id="track-status" style="font-size:0.75rem;color:#888;margin-top:6px;">Tracking off</div>
</div>
<div class="panel">
<h2>RTSP Source</h2>
<div class="input-group">
<input type="text" id="source-input" placeholder="rtsp://..." value="rtsp://192.168.2.72:80/h264ESVideoTest">
<button class="btn" id="apply-source">Apply</button>
</div>
</div>
<div class="panel classes-area" id="yoloe-panel" style="display:none;">
<h2>YOLOE Classes</h2>
<textarea id="classes-input" placeholder="Enter comma-separated classes...">grass, tree, fence, chain fence, gate, wall, person, ball, block, house, pole, post, board, ground, dirt, field, bush</textarea>
<button class="btn secondary" id="apply-classes" style="margin-top:8px;width:100%;">Set Classes</button>
</div>
<div class="panel">
<h2>Ollama LLM</h2>
<select id="llm-model-select"></select>
<div style="margin-top:8px;display:flex;gap:8px;">
<button class="btn" id="test-llm" style="flex:1;">Test Connection</button>
<button class="btn secondary" id="refresh-llm" style="width:40px;">&#x21bb;</button>
</div>
<div id="llm-badge" class="badge offline">disconnected</div>
<div id="llm-info" style="font-size:0.72rem;color:#777;margin-top:6px;line-height:1.5;"></div>
<div id="llm-reply" style="display:none;font-size:0.75rem;color:#aaa;margin-top:8px;padding:8px;background:#242424;border-radius:8px;white-space:pre-wrap;word-break:break-word;"></div>
</div>
</div>
</div>
<div class="toast" id="toast"></div>
<script>
const stream = document.getElementById('stream');
const modelSelect = document.getElementById('model-select');
const applyModelBtn = document.getElementById('apply-model');
const refreshModelsBtn = document.getElementById('refresh-models');
const modelBadge = document.getElementById('model-badge');
const statFps = document.getElementById('stat-fps');
const statDets = document.getElementById('stat-dets');
const sourceInput = document.getElementById('source-input');
const applySourceBtn = document.getElementById('apply-source');
const statusDot = document.getElementById('status-dot');
const statusText = document.getElementById('status-text');
const yoloePanel = document.getElementById('yoloe-panel');
const classesInput = document.getElementById('classes-input');
const trackToggle = document.getElementById('track-toggle');
const trackStatus = document.getElementById('track-status');
const applyClassesBtn = document.getElementById('apply-classes');
const llmModelSelect = document.getElementById('llm-model-select');
const testLlmBtn = document.getElementById('test-llm');
const refreshLlmBtn = document.getElementById('refresh-llm');
const llmBadge = document.getElementById('llm-badge');
const llmInfo = document.getElementById('llm-info');
const llmReply = document.getElementById('llm-reply');
const toast = document.getElementById('toast');
let currentModel = '';
function showToast(msg, type) {
toast.textContent = msg;
toast.className = 'toast ' + type + ' show';
clearTimeout(toast._hide);
toast._hide = setTimeout(() => toast.classList.remove('show'), 3000);
}
const TASK_LABELS = {detect: 'Detection', segment: 'Segmentation', classify: 'Classification', pose: 'Pose', obb: 'OBB'};
async function fetchModels() {
try {
const res = await fetch('/api/models');
const data = await res.json();
currentModel = data.current;
const groups = {};
data.models.forEach(m => {
const t = m.task || 'detect';
if (!groups[t]) groups[t] = [];
groups[t].push(m);
});
modelSelect.innerHTML = '';
for (const [task, models] of Object.entries(groups)) {
const grp = document.createElement('optgroup');
grp.label = TASK_LABELS[task] || task;
models.forEach(m => {
const opt = document.createElement('option');
opt.value = m.name;
let label = m.name;
if (m.type === 'yoloe') label += ' [YOLOE]';
opt.textContent = label;
if (m.name === data.current) opt.selected = true;
grp.appendChild(opt);
});
modelSelect.appendChild(grp);
}
updateBadge(currentModel);
updateDot(true, 'Connected');
return data;
} catch(e) {
updateDot(false, 'Disconnected');
showToast('Cannot connect to server', 'error');
}
}
async function refreshStatus() {
try {
const res = await fetch('/api/status');
const data = await res.json();
statFps.textContent = data.fps;
statDets.textContent = data.detections;
if (document.activeElement !== sourceInput) sourceInput.value = data.source;
trackToggle.checked = data.tracking;
trackStatus.textContent = data.tracking ? 'Tracking active (persist=True)' : 'Tracking off';
if (data.model !== currentModel) {
currentModel = data.model;
updateBadge(data.model);
for (const opt of modelSelect.options) {
if (opt.value === data.model) { opt.selected = true; break; }
}
}
yoloePanel.style.display = data.model.includes('YOLOE') ? 'block' : 'none';
updateDot(true, 'Connected');
} catch(e) {
updateDot(false, 'Disconnected');
}
}
function updateBadge(name) {
modelBadge.textContent = name;
modelBadge.className = 'badge' + (name.includes('YOLOE') ? '' : '');
}
function updateDot(online, text) {
statusDot.className = 'dot' + (online ? '' : ' offline');
statusText.textContent = text;
}
applyModelBtn.addEventListener('click', async () => {
const name = modelSelect.value;
if (!name) return;
applyModelBtn.disabled = true;
applyModelBtn.textContent = 'Switching...';
try {
const res = await fetch('/api/models', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name})
});
const data = await res.json();
if (data.status === 'ok') {
showToast(`Switched to ${data.model}`, 'success');
currentModel = data.model;
updateBadge(data.model);
} else {
showToast(data.message || 'Failed to switch model', 'error');
}
} catch(e) {
showToast('Error switching model', 'error');
}
applyModelBtn.disabled = false;
applyModelBtn.textContent = 'Apply Model';
});
refreshModelsBtn.addEventListener('click', fetchModels);
trackToggle.addEventListener('change', async () => {
try {
await fetch('/api/track', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({enabled: trackToggle.checked})
});
showToast(trackToggle.checked ? 'Tracking ON' : 'Tracking OFF', 'info');
} catch(e) {
showToast('Error toggling tracking', 'error');
}
});
applySourceBtn.addEventListener('click', async () => {
const url = sourceInput.value.trim();
if (!url) return;
applySourceBtn.disabled = true;
applySourceBtn.textContent = 'Applying...';
try {
const res = await fetch('/api/source', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({url})
});
const data = await res.json();
if (data.status === 'ok') {
showToast('Source updated, reconnecting stream...', 'info');
stream.src = '/video_feed?' + Date.now();
} else {
showToast(data.message || 'Failed', 'error');
}
} catch(e) {
showToast('Error updating source', 'error');
}
applySourceBtn.disabled = false;
applySourceBtn.textContent = 'Apply';
});
applyClassesBtn.addEventListener('click', async () => {
const classes = classesInput.value.split(',').map(s => s.trim()).filter(Boolean);
if (!classes.length) return;
try {
const res = await fetch('/api/yoloe/classes', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({classes})
});
const data = await res.json();
if (data.status === 'ok') {
showToast('Classes updated', 'success');
} else {
showToast(data.message || 'Failed', 'error');
}
} catch(e) {
showToast('Error setting classes', 'error');
}
});
async function fetchLlmModels() {
try {
const res = await fetch('/api/llm/models');
const data = await res.json();
if (data.status !== 'ok') {
llmBadge.textContent = 'unreachable';
llmBadge.className = 'badge offline';
llmInfo.textContent = data.message || 'Cannot reach Ollama';
return;
}
const prev = llmModelSelect.value;
llmModelSelect.innerHTML = '';
data.models.forEach(m => {
const opt = document.createElement('option');
opt.value = m.name;
opt.textContent = m.vision ? m.name + ' [vision]' : m.name;
llmModelSelect.appendChild(opt);
});
if (prev && data.models.some(m => m.name === prev)) llmModelSelect.value = prev;
else if (data.models.some(m => m.name === data.current)) llmModelSelect.value = data.current;
llmInfo.textContent = data.host;
} catch(e) {
llmBadge.textContent = 'unreachable';
llmBadge.className = 'badge offline';
llmInfo.textContent = 'Cannot reach controller';
}
}
async function refreshLlmStatus() {
try {
const res = await fetch('/api/llm/status');
const data = await res.json();
const online = data.reachable && data.model_present;
llmBadge.textContent = data.reachable
? (data.model_present ? data.model : 'model missing')
: 'unreachable';
llmBadge.className = 'badge' + (online ? '' : ' offline');
if (data.reachable) {
llmInfo.textContent = [data.version, data.model_count + ' models',
data.latency_ms != null ? data.latency_ms + ' ms' : null].filter(Boolean).join(' · ');
} else if (data.error) {
llmInfo.textContent = data.error;
}
} catch(e) {}
}
llmModelSelect.addEventListener('change', async () => {
try {
const res = await fetch('/api/llm/model', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({name: llmModelSelect.value})
});
const data = await res.json();
if (data.status === 'ok') showToast('LLM set to ' + data.model, 'success');
else showToast(data.message || 'Failed', 'error');
} catch(e) {
showToast('Error setting LLM model', 'error');
}
});
testLlmBtn.addEventListener('click', async () => {
testLlmBtn.disabled = true;
testLlmBtn.textContent = 'Testing...';
llmReply.style.display = 'none';
try {
const res = await fetch('/api/llm/test', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({})
});
const data = await res.json();
if (data.status === 'ok') {
llmReply.textContent = `${data.reply}\n\n${data.duration_ms} ms` + (data.eval_count ? ` · ${data.eval_count} tokens` : '');
llmReply.style.display = 'block';
showToast('LLM reachable', 'success');
} else {
llmReply.textContent = data.message || 'Test failed';
llmReply.style.display = 'block';
showToast('LLM test failed', 'error');
}
} catch(e) {
showToast('Error testing LLM', 'error');
}
testLlmBtn.disabled = false;
testLlmBtn.textContent = 'Test Connection';
});
refreshLlmBtn.addEventListener('click', fetchLlmModels);
// Reconnect stream on error
stream.addEventListener('error', () => {
setTimeout(() => { stream.src = '/video_feed?' + Date.now(); }, 2000);
});
fetchModels();
fetchLlmModels();
refreshLlmStatus();
setInterval(refreshStatus, 2000);
setInterval(refreshLlmStatus, 5000);
</script>
</body>
</html>