video.bak4.html 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>视频监控</title>
  7. <style>
  8. * { box-sizing: border-box; margin: 0; padding: 0; }
  9. body {
  10. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  11. background: #1a1a2e; color: #eee; min-height: 100vh; display: flex; flex-direction: column;
  12. }
  13. .top-bar {
  14. background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  15. padding: 12px 20px; display: flex; align-items: center; justify-content: space-between;
  16. flex-shrink: 0;
  17. }
  18. .top-bar h1 { font-size: 18px; color: white; }
  19. .nav-links { display: flex; gap: 8px; }
  20. .nav-link {
  21. background: rgba(255,255,255,0.2); color: white; padding: 6px 14px;
  22. border-radius: 16px; text-decoration: none; font-size: 13px;
  23. transition: background 0.2s; white-space: nowrap;
  24. }
  25. .nav-link:hover { background: rgba(255,255,255,0.35); }
  26. .nav-link.active { background: rgba(255,255,255,0.45); font-weight: bold; }
  27. .video-area {
  28. flex: 1; display: flex; flex-direction: column; align-items: center;
  29. padding: 16px; overflow: auto;
  30. }
  31. .video-wrapper {
  32. position: relative; background: #000; border-radius: 8px;
  33. overflow: hidden; box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  34. max-width: 960px; width: 100%;
  35. }
  36. .video-wrapper img { width: 100%; display: block; }
  37. #roi-canvas {
  38. position: absolute; top: 0; left: 0; width: 100%; height: 100%;
  39. cursor: crosshair; display: none; z-index: 10;
  40. }
  41. .stream-label {
  42. position: absolute; top: 18px; left: 12px; background: rgba(0,0,0,0.6);
  43. color: white; padding: 4px 10px; border-radius: 4px; font-size: 13px;
  44. pointer-events: none; z-index: 5;
  45. }
  46. .stream-status {
  47. position: absolute; top: 18px; right: 12px; background: rgba(0,0,0,0.6);
  48. color: #52c41a; padding: 4px 10px; border-radius: 4px; font-size: 12px;
  49. pointer-events: none; z-index: 5;
  50. }
  51. .stream-status.offline { color: #f5222d; }
  52. .controls {
  53. margin-top: 14px; display: flex; gap: 10px; flex-wrap: wrap;
  54. justify-content: center; max-width: 960px; width: 100%;
  55. }
  56. .btn {
  57. padding: 10px 20px; border: 1px solid #444; background: #2a2a4a;
  58. color: #eee; border-radius: 8px; cursor: pointer; font-size: 14px;
  59. transition: all 0.2s; display: flex; align-items: center; gap: 6px;
  60. }
  61. .btn:hover { background: #3a3a5a; border-color: #667eea; }
  62. .btn.primary { background: #667eea; border-color: #667eea; }
  63. .btn.primary:hover { background: #5a6ed6; }
  64. .btn.danger { background: #ff4d4f; border-color: #ff4d4f; color: white; }
  65. .btn.danger:hover { background: #e04445; }
  66. .btn.drawing { background: #ff4d4f; color: white; border-color: #ff4d4f; animation: pulse 1.5s infinite; }
  67. .btn:disabled { opacity: 0.5; cursor: not-allowed; }
  68. @keyframes pulse {
  69. 0%, 100% { opacity: 1; }
  70. 50% { opacity: 0.7; }
  71. }
  72. .roi-info {
  73. margin-top: 12px; padding: 12px 16px; background: #2a2a4a;
  74. border-radius: 8px; font-size: 13px; line-height: 1.8;
  75. max-width: 960px; width: 100%;
  76. }
  77. .roi-info .label { color: #999; }
  78. .roi-info .value { color: #667eea; font-weight: bold; }
  79. .help-text { margin-top: 8px; font-size: 12px; color: #666; text-align: center; }
  80. @media (max-width: 600px) {
  81. .top-bar { flex-direction: column; gap: 8px; }
  82. .nav-links { flex-wrap: wrap; justify-content: center; }
  83. }
  84. </style>
  85. </head>
  86. <body>
  87. <div class="top-bar">
  88. <h1 id="page-title">视频监控</h1>
  89. <div class="nav-links">
  90. <a href="/" class="nav-link">📊 监控台</a>
  91. <a href="/video?stream=in-low" class="nav-link" id="nav-in-low">低位进站</a>
  92. <a href="/video?stream=in-high" class="nav-link" id="nav-in-high">高位进站</a>
  93. <a href="/video?stream=out-low" class="nav-link" id="nav-out-low">低位出站</a>
  94. <a href="/video?stream=out-high" class="nav-link" id="nav-out-high">高位出站</a>
  95. </div>
  96. </div>
  97. <div class="video-area">
  98. <div class="video-wrapper" id="video-wrapper">
  99. <div class="stream-label" id="stream-label">加载中...</div>
  100. <div class="stream-status" id="stream-status">⏳ 连接中</div>
  101. <img id="video-stream" src="" alt="视频流">
  102. <canvas id="roi-canvas"></canvas>
  103. </div>
  104. <div class="controls">
  105. <button class="btn primary" onclick="takeSnapshot()">📷 截图</button>
  106. <button class="btn" id="btn-draw-roi" onclick="startDrawROI()">✏️ 绘制ROI</button>
  107. <button class="btn" id="btn-clear-roi" onclick="clearROI()">🗑️ 清除ROI</button>
  108. <button class="btn" onclick="refreshStream()">🔄 刷新视频</button>
  109. </div>
  110. <div class="roi-info" id="roi-info">
  111. <span class="label">加载配置中...</span>
  112. </div>
  113. <div class="help-text" id="help-text"></div>
  114. </div>
  115. <script>
  116. var STREAM_NAMES = {
  117. 'in-low': '低位进站', 'in-high': '高位进站',
  118. 'out-low': '低位出站', 'out-high': '高位出站'
  119. };
  120. var ROI_STREAMS = ['in-low', 'out-low'];
  121. var currentStream = 'in-low';
  122. var hasROI = false;
  123. var isDrawingROI = false;
  124. var drawStart = null;
  125. var currentROI = null;
  126. var videoResolution = { width: 1920, height: 1080 };
  127. (function init() {
  128. var params = new URLSearchParams(window.location.search);
  129. currentStream = params.get('stream') || 'in-low';
  130. hasROI = ROI_STREAMS.indexOf(currentStream) >= 0;
  131. var streamName = STREAM_NAMES[currentStream] || '未知';
  132. document.title = streamName + ' - 视频监控';
  133. document.getElementById('page-title').textContent = streamName;
  134. document.getElementById('stream-label').textContent = streamName;
  135. var navEl = document.getElementById('nav-' + currentStream);
  136. if (navEl) navEl.classList.add('active');
  137. if (!hasROI) {
  138. document.getElementById('btn-draw-roi').style.display = 'none';
  139. document.getElementById('btn-clear-roi').style.display = 'none';
  140. document.getElementById('help-text').textContent = '高位摄像头无ROI功能';
  141. } else {
  142. document.getElementById('help-text').innerHTML =
  143. 'ROI仅影响推理裁剪区域,<b>上传照片仍为原始全帧</b> | ESC退出绘制模式';
  144. }
  145. startStream();
  146. loadRoiConfig();
  147. document.addEventListener('keydown', function(e) {
  148. if (e.key === 'Escape' && isDrawingROI) cancelDrawROI();
  149. });
  150. var img = document.getElementById('video-stream');
  151. img.onload = function() {
  152. document.getElementById('stream-status').textContent = '🟢 在线';
  153. document.getElementById('stream-status').className = 'stream-status';
  154. };
  155. img.onerror = function() {
  156. document.getElementById('stream-status').textContent = '🔴 离线';
  157. document.getElementById('stream-status').className = 'stream-status offline';
  158. setTimeout(function() {
  159. img.src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now();
  160. }, 3000);
  161. };
  162. })();
  163. function startStream() {
  164. document.getElementById('video-stream').src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now();
  165. }
  166. function refreshStream() {
  167. var img = document.getElementById('video-stream');
  168. img.src = '';
  169. setTimeout(function() { img.src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now(); }, 100);
  170. }
  171. function takeSnapshot() {
  172. window.open('/api/stream?stream=' + currentStream + '&t=' + Date.now(), '_blank');
  173. }
  174. function loadRoiConfig() {
  175. fetch('/api/roi/config')
  176. .then(function(r) { return r.json(); })
  177. .then(function(data) {
  178. var roi = null;
  179. if (currentStream === 'in-low') roi = data.roi_in;
  180. else if (currentStream === 'out-low') roi = data.roi_out;
  181. if (data.resolution && data.resolution[currentStream]) {
  182. videoResolution = data.resolution[currentStream];
  183. }
  184. var infoEl = document.getElementById('roi-info');
  185. if (!hasROI) {
  186. infoEl.innerHTML = '<span class="label">高位摄像头 — 无ROI配置</span>';
  187. return;
  188. }
  189. if (roi && roi.enabled) {
  190. currentROI = { x: roi.x, y: roi.y, w: roi.w, h: roi.h };
  191. infoEl.innerHTML =
  192. '<span class="label">ROI区域:</span> <span class="value">(' +
  193. roi.x + ', ' + roi.y + ') ' + roi.w + '×' + roi.h +
  194. '</span> <span class="label">| 分辨率:</span> <span class="value">' +
  195. videoResolution.width + '×' + videoResolution.height +
  196. '</span><br><span class="label">状态:</span> <span class="value" style="color:#52c41a;">✅ 已启用</span>' +
  197. ' <span class="label">| 推理裁剪,上传全帧</span>';
  198. } else {
  199. currentROI = null;
  200. infoEl.innerHTML =
  201. '<span class="label">ROI状态:</span> <span class="value" style="color:#faad14;">⚪ 未启用(全帧推理)</span>' +
  202. '<br><span class="label">分辨率:</span> <span class="value">' +
  203. videoResolution.width + '×' + videoResolution.height + '</span>';
  204. }
  205. })
  206. .catch(function(e) {
  207. document.getElementById('roi-info').innerHTML =
  208. '<span class="label" style="color:#f5222d;">⚠️ 配置加载失败</span>';
  209. });
  210. }
  211. function startDrawROI() {
  212. if (!hasROI) return;
  213. isDrawingROI = true;
  214. drawStart = null;
  215. var img = document.getElementById('video-stream');
  216. var canvas = document.getElementById('roi-canvas');
  217. var btn = document.getElementById('btn-draw-roi');
  218. img.src = '/api/stream?stream=' + currentStream + '&t=' + Date.now();
  219. resizeCanvas();
  220. canvas.style.display = 'block';
  221. btn.classList.add('drawing');
  222. btn.innerHTML = '🔲 绘制中...';
  223. canvas.onmousedown = onDrawStart;
  224. canvas.onmousemove = onDrawMove;
  225. canvas.onmouseup = onDrawEnd;
  226. canvas.ontouchstart = onTouchStart;
  227. canvas.ontouchmove = onTouchMove;
  228. canvas.ontouchend = onTouchEnd;
  229. }
  230. function cancelDrawROI() {
  231. isDrawingROI = false;
  232. drawStart = null;
  233. var canvas = document.getElementById('roi-canvas');
  234. var btn = document.getElementById('btn-draw-roi');
  235. canvas.style.display = 'none';
  236. canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
  237. btn.classList.remove('drawing');
  238. btn.innerHTML = '✏️ 绘制ROI';
  239. startStream();
  240. }
  241. function finishDrawROI(rect) {
  242. var canvas = document.getElementById('roi-canvas');
  243. var scaleX = videoResolution.width / canvas.clientWidth;
  244. var scaleY = videoResolution.height / canvas.clientHeight;
  245. var roiX = Math.round(rect.x * scaleX);
  246. var roiY = Math.round(rect.y * scaleY);
  247. var roiW = Math.round(rect.w * scaleX);
  248. var roiH = Math.round(rect.h * scaleY);
  249. roiX = Math.max(0, Math.min(roiX, videoResolution.width - 1));
  250. roiY = Math.max(0, Math.min(roiY, videoResolution.height - 1));
  251. roiW = Math.min(roiW, videoResolution.width - roiX);
  252. roiH = Math.min(roiH, videoResolution.height - roiY);
  253. if (roiW < 50 || roiH < 50) {
  254. alert('ROI区域太小(最小50×50像素),请重新绘制');
  255. cancelDrawROI();
  256. return;
  257. }
  258. var side = (currentStream === 'in-low') ? 'in' : 'out';
  259. var sideName = (side === 'in') ? '进站' : '出站';
  260. if (confirm('确认应用' + sideName + 'ROI区域?\n坐标: (' + roiX + ', ' + roiY + ')\n尺寸: ' + roiW + '×' + roiH)) {
  261. fetch('/api/roi/update', {
  262. method: 'POST',
  263. headers: { 'Content-Type': 'application/json' },
  264. body: JSON.stringify({ side: side, x: roiX, y: roiY, w: roiW, h: roiH, enabled: true })
  265. })
  266. .then(function(r) { return r.json(); })
  267. .then(function(data) {
  268. if (data.success) {
  269. alert('✅ ' + sideName + 'ROI已更新并保存到config.ini');
  270. loadRoiConfig();
  271. } else {
  272. alert('❌ 更新失败: ' + (data.message || '未知错误'));
  273. }
  274. })
  275. .catch(function(e) { alert('请求失败: ' + e); });
  276. }
  277. cancelDrawROI();
  278. }
  279. function clearROI() {
  280. if (!hasROI) return;
  281. var side = (currentStream === 'in-low') ? 'in' : 'out';
  282. var sideName = (side === 'in') ? '进站' : '出站';
  283. if (!confirm('确定清除' + sideName + 'ROI区域?\n清除后将使用全帧推理')) return;
  284. fetch('/api/roi/update', {
  285. method: 'POST',
  286. headers: { 'Content-Type': 'application/json' },
  287. body: JSON.stringify({ side: side, x: 0, y: 0, w: 0, h: 0, enabled: false })
  288. })
  289. .then(function(r) { return r.json(); })
  290. .then(function(data) {
  291. if (data.success) {
  292. alert('✅ ROI已清除,恢复全帧推理');
  293. loadRoiConfig();
  294. } else {
  295. alert('❌ 清除失败: ' + (data.message || '未知错误'));
  296. }
  297. })
  298. .catch(function(e) { alert('请求失败: ' + e); });
  299. }
  300. function resizeCanvas() {
  301. var wrapper = document.getElementById('video-wrapper');
  302. var canvas = document.getElementById('roi-canvas');
  303. canvas.width = wrapper.clientWidth;
  304. canvas.height = wrapper.clientHeight;
  305. }
  306. function getCanvasPos(e) {
  307. var rect = document.getElementById('roi-canvas').getBoundingClientRect();
  308. return { x: e.clientX - rect.left, y: e.clientY - rect.top };
  309. }
  310. function onDrawStart(e) { drawStart = getCanvasPos(e); }
  311. function onDrawMove(e) {
  312. if (!drawStart) return;
  313. var canvas = document.getElementById('roi-canvas');
  314. var ctx = canvas.getContext('2d');
  315. var pos = getCanvasPos(e);
  316. ctx.clearRect(0, 0, canvas.width, canvas.height);
  317. var rx = Math.min(drawStart.x, pos.x);
  318. var ry = Math.min(drawStart.y, pos.y);
  319. var rw = Math.abs(pos.x - drawStart.x);
  320. var rh = Math.abs(pos.y - drawStart.y);
  321. ctx.fillStyle = 'rgba(255, 0, 0, 0.15)';
  322. ctx.fillRect(rx, ry, rw, rh);
  323. ctx.strokeStyle = '#ff0000';
  324. ctx.lineWidth = 2;
  325. ctx.setLineDash([6, 3]);
  326. ctx.strokeRect(rx, ry, rw, rh);
  327. ctx.setLineDash([]);
  328. var scaleX = videoResolution.width / canvas.clientWidth;
  329. var scaleY = videoResolution.height / canvas.clientHeight;
  330. ctx.fillStyle = '#ff0000';
  331. ctx.font = '14px sans-serif';
  332. ctx.fillText(Math.round(rw * scaleX) + '×' + Math.round(rh * scaleY), rx + 4, ry + 18);
  333. }
  334. function onDrawEnd(e) {
  335. if (!drawStart) return;
  336. var pos = getCanvasPos(e);
  337. var x = Math.min(drawStart.x, pos.x);
  338. var y = Math.min(drawStart.y, pos.y);
  339. var w = Math.abs(pos.x - drawStart.x);
  340. var h = Math.abs(pos.y - drawStart.y);
  341. if (w < 5 || h < 5) { cancelDrawROI(); return; }
  342. finishDrawROI({ x: x, y: y, w: w, h: h });
  343. }
  344. function onTouchStart(e) {
  345. e.preventDefault();
  346. var t = e.touches[0];
  347. onDrawStart({ clientX: t.clientX, clientY: t.clientY });
  348. }
  349. function onTouchMove(e) {
  350. e.preventDefault();
  351. var t = e.touches[0];
  352. onDrawMove({ clientX: t.clientX, clientY: t.clientY });
  353. }
  354. function onTouchEnd(e) {
  355. e.preventDefault();
  356. var t = e.changedTouches[0];
  357. onDrawEnd({ clientX: t.clientX, clientY: t.clientY });
  358. }
  359. </script>
  360. </body>
  361. </html>