video.html 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  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. @media (max-width: 600px) {
  85. .top-bar { padding: 10px 12px; }
  86. .top-bar h1 { font-size: 15px; }
  87. .nav-link { padding: 4px 10px; font-size: 12px; }
  88. .video-area { padding: 8px; }
  89. .controls { gap: 6px; }
  90. .btn { padding: 8px 14px; font-size: 13px; }
  91. .roi-info { padding: 10px 12px; font-size: 12px; }
  92. }
  93. </style>
  94. </head>
  95. <body>
  96. <div class="top-bar">
  97. <h1 id="page-title">视频监控</h1>
  98. <div class="nav-links">
  99. <a href="/" class="nav-link">📊 监控台</a>
  100. <a href="/video?stream=in-low" class="nav-link" id="nav-in-low">低位进站</a>
  101. <a href="/video?stream=in-high" class="nav-link" id="nav-in-high">高位进站</a>
  102. <a href="/video?stream=out-low" class="nav-link" id="nav-out-low">低位出站</a>
  103. <a href="/video?stream=out-high" class="nav-link" id="nav-out-high">高位出站</a>
  104. <a href="/help" class="nav-link" id="nav-help" style="display:none;">📖 帮助</a>
  105. </div>
  106. </div>
  107. <div class="video-area">
  108. <div class="video-wrapper" id="video-wrapper">
  109. <div class="stream-label" id="stream-label">加载中...</div>
  110. <div class="stream-status" id="stream-status">⏳ 连接中</div>
  111. <div class="stream-status" id="shared-badge" style="display:none; top:18px; right:100px; background:rgba(102,126,234,0.7); color:white;">🔗 共享模式</div>
  112. <img id="video-stream" src="" alt="视频流">
  113. <canvas id="roi-canvas"></canvas>
  114. </div>
  115. <div class="controls">
  116. <button class="btn primary" onclick="takeSnapshot()">📷 截图</button>
  117. <button class="btn" id="btn-draw-roi" onclick="startDrawROI()">✏️ 绘制ROI</button>
  118. <button class="btn" id="btn-clear-roi" onclick="clearROI()">🗑️ 清除ROI</button>
  119. <button class="btn" onclick="refreshStream()">🔄 刷新视频</button>
  120. </div>
  121. <div class="roi-info" id="roi-info">
  122. <span class="label">加载配置中...</span>
  123. </div>
  124. <div class="help-text" id="help-text"></div>
  125. </div>
  126. <script>
  127. function getCookie(n){let m=document.cookie.match(new RegExp("(^| )"+n+"=([^;]+)"));return m?m[2]:"";}
  128. var STREAM_NAMES = {
  129. 'in-low': '低位进站', 'in-high': '高位进站',
  130. 'out-low': '低位出站', 'out-high': '高位出站'
  131. };
  132. var ROI_STREAMS = ['in-low', 'out-low'];
  133. var currentStream = 'in-low';
  134. var hasROI = false;
  135. var isDrawingROI = false;
  136. var drawStart = null;
  137. var currentROI = null;
  138. var videoResolution = { width: 1920, height: 1080 };
  139. (function init() {
  140. var params = new URLSearchParams(window.location.search);
  141. currentStream = params.get('stream') || 'in-low';
  142. hasROI = ROI_STREAMS.indexOf(currentStream) >= 0;
  143. var streamName = STREAM_NAMES[currentStream] || '未知';
  144. document.title = streamName + ' - 视频监控';
  145. document.getElementById('page-title').textContent = streamName;
  146. document.getElementById('stream-label').textContent = streamName;
  147. var navEl = document.getElementById('nav-' + currentStream);
  148. if (navEl) navEl.classList.add('active');
  149. if (!hasROI) {
  150. document.getElementById('btn-draw-roi').style.display = 'none';
  151. document.getElementById('btn-clear-roi').style.display = 'none';
  152. document.getElementById('help-text').textContent = '高位摄像头无ROI功能';
  153. } else {
  154. document.getElementById('help-text').innerHTML =
  155. 'ROI仅影响推理裁剪区域,<b>上传照片仍为原始全帧</b> | ESC退出绘制模式';
  156. }
  157. startStream();
  158. loadRoiConfig();
  159. document.addEventListener('keydown', function(e) {
  160. if (e.key === 'Escape' && isDrawingROI) cancelDrawROI();
  161. });
  162. var img = document.getElementById('video-stream');
  163. img.onload = function() {
  164. document.getElementById('stream-status').textContent = '🟢 在线';
  165. document.getElementById('stream-status').className = 'stream-status';
  166. };
  167. img.onerror = function() {
  168. document.getElementById('stream-status').textContent = '🔴 离线';
  169. document.getElementById('stream-status').className = 'stream-status offline';
  170. setTimeout(function() {
  171. img.src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now();
  172. }, 3000);
  173. };
  174. })();
  175. function startStream() {
  176. document.getElementById('video-stream').src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now();
  177. }
  178. function refreshStream() {
  179. var img = document.getElementById('video-stream');
  180. img.src = '';
  181. setTimeout(function() { img.src = '/api/mjpeg?stream=' + currentStream + '&t=' + Date.now(); }, 100);
  182. }
  183. function takeSnapshot() {
  184. window.open('/api/stream?stream=' + currentStream + '&t=' + Date.now(), '_blank');
  185. }
  186. function loadRoiConfig() {
  187. fetch('/api/roi/config')
  188. .then(function(r) { return r.json(); })
  189. .then(function(data) {
  190. var roi = null;
  191. if (currentStream === 'in-low') roi = data.roi_in;
  192. else if (currentStream === 'out-low') roi = data.roi_out;
  193. if (data.resolution && data.resolution[currentStream]) {
  194. videoResolution = data.resolution[currentStream];
  195. }
  196. // ✅ fix24-v9: 共享模式下出站页面显示提示
  197. if (data.shared_mode && currentStream === 'out-low') {
  198. var badge = document.getElementById('shared-badge');
  199. if (badge) badge.style.display = 'block';
  200. }
  201. var infoEl = document.getElementById('roi-info');
  202. if (!hasROI) {
  203. infoEl.innerHTML = '<span class="label">高位摄像头 — 无ROI配置</span>';
  204. return;
  205. }
  206. if (roi && roi.enabled) {
  207. currentROI = { x: roi.x, y: roi.y, w: roi.w, h: roi.h };
  208. infoEl.innerHTML =
  209. '<span class="label">ROI区域:</span> <span class="value">(' +
  210. roi.x + ', ' + roi.y + ') ' + roi.w + '×' + roi.h +
  211. '</span> <span class="label">| 分辨率:</span> <span class="value">' +
  212. videoResolution.width + '×' + videoResolution.height +
  213. '</span><br><span class="label">状态:</span> <span class="value" style="color:#52c41a;">✅ 已启用</span>' +
  214. ' <span class="label">| 推理裁剪,上传全帧</span>';
  215. } else {
  216. currentROI = null;
  217. infoEl.innerHTML =
  218. '<span class="label">ROI状态:</span> <span class="value" style="color:#faad14;">⚪ 未启用(全帧推理)</span>' +
  219. '<br><span class="label">分辨率:</span> <span class="value">' +
  220. videoResolution.width + '×' + videoResolution.height + '</span>';
  221. }
  222. })
  223. .catch(function(e) {
  224. document.getElementById('roi-info').innerHTML =
  225. '<span class="label" style="color:#f5222d;">⚠️ 配置加载失败</span>';
  226. });
  227. }
  228. function startDrawROI() {
  229. if (!hasROI) return;
  230. isDrawingROI = true;
  231. drawStart = null;
  232. var img = document.getElementById('video-stream');
  233. var canvas = document.getElementById('roi-canvas');
  234. var btn = document.getElementById('btn-draw-roi');
  235. img.src = '/api/stream?stream=' + currentStream + '&t=' + Date.now();
  236. resizeCanvas();
  237. canvas.style.display = 'block';
  238. btn.classList.add('drawing');
  239. btn.innerHTML = '🔲 绘制中...';
  240. canvas.onmousedown = onDrawStart;
  241. canvas.onmousemove = onDrawMove;
  242. canvas.onmouseup = onDrawEnd;
  243. canvas.ontouchstart = onTouchStart;
  244. canvas.ontouchmove = onTouchMove;
  245. canvas.ontouchend = onTouchEnd;
  246. }
  247. function cancelDrawROI() {
  248. isDrawingROI = false;
  249. drawStart = null;
  250. var canvas = document.getElementById('roi-canvas');
  251. var btn = document.getElementById('btn-draw-roi');
  252. canvas.style.display = 'none';
  253. canvas.getContext('2d').clearRect(0, 0, canvas.width, canvas.height);
  254. btn.classList.remove('drawing');
  255. btn.innerHTML = '✏️ 绘制ROI';
  256. startStream();
  257. }
  258. function finishDrawROI(rect) {
  259. var canvas = document.getElementById('roi-canvas');
  260. var scaleX = videoResolution.width / canvas.clientWidth;
  261. var scaleY = videoResolution.height / canvas.clientHeight;
  262. var roiX = Math.round(rect.x * scaleX);
  263. var roiY = Math.round(rect.y * scaleY);
  264. var roiW = Math.round(rect.w * scaleX);
  265. var roiH = Math.round(rect.h * scaleY);
  266. roiX = Math.max(0, Math.min(roiX, videoResolution.width - 1));
  267. roiY = Math.max(0, Math.min(roiY, videoResolution.height - 1));
  268. roiW = Math.min(roiW, videoResolution.width - roiX);
  269. roiH = Math.min(roiH, videoResolution.height - roiY);
  270. if (roiW < 50 || roiH < 50) {
  271. alert('ROI区域太小(最小50×50像素),请重新绘制');
  272. cancelDrawROI();
  273. return;
  274. }
  275. var side = (currentStream === 'in-low') ? 'in' : 'out';
  276. var sideName = (side === 'in') ? '进站' : '出站';
  277. if (confirm('确认应用' + sideName + 'ROI区域?\n坐标: (' + roiX + ', ' + roiY + ')\n尺寸: ' + roiW + '×' + roiH)) {
  278. fetch('/api/roi/update', {
  279. method: 'POST',
  280. headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCookie('csrf_token') },
  281. body: JSON.stringify({ side: side, x: roiX, y: roiY, w: roiW, h: roiH, enabled: true })
  282. })
  283. .then(function(r) { return r.json(); })
  284. .then(function(data) {
  285. if (data.success) {
  286. alert('✅ ' + sideName + 'ROI已更新并保存到config.ini');
  287. loadRoiConfig();
  288. } else {
  289. alert('❌ 更新失败: ' + (data.message || '未知错误'));
  290. }
  291. })
  292. .catch(function(e) { alert('请求失败: ' + e); });
  293. }
  294. cancelDrawROI();
  295. }
  296. function clearROI() {
  297. if (!hasROI) return;
  298. var side = (currentStream === 'in-low') ? 'in' : 'out';
  299. var sideName = (side === 'in') ? '进站' : '出站';
  300. if (!confirm('确定清除' + sideName + 'ROI区域?\n清除后将使用全帧推理')) return;
  301. fetch('/api/roi/update', {
  302. method: 'POST',
  303. headers: { 'Content-Type': 'application/json', 'X-CSRF-Token': getCookie('csrf_token') },
  304. body: JSON.stringify({ side: side, x: 0, y: 0, w: 0, h: 0, enabled: false })
  305. })
  306. .then(function(r) { return r.json(); })
  307. .then(function(data) {
  308. if (data.success) {
  309. alert('✅ ROI已清除,恢复全帧推理');
  310. loadRoiConfig();
  311. } else {
  312. alert('❌ 清除失败: ' + (data.message || '未知错误'));
  313. }
  314. })
  315. .catch(function(e) { alert('请求失败: ' + e); });
  316. }
  317. function resizeCanvas() {
  318. var wrapper = document.getElementById('video-wrapper');
  319. var canvas = document.getElementById('roi-canvas');
  320. canvas.width = wrapper.clientWidth;
  321. canvas.height = wrapper.clientHeight;
  322. }
  323. function getCanvasPos(e) {
  324. var rect = document.getElementById('roi-canvas').getBoundingClientRect();
  325. return { x: e.clientX - rect.left, y: e.clientY - rect.top };
  326. }
  327. function onDrawStart(e) { drawStart = getCanvasPos(e); }
  328. function onDrawMove(e) {
  329. if (!drawStart) return;
  330. var canvas = document.getElementById('roi-canvas');
  331. var ctx = canvas.getContext('2d');
  332. var pos = getCanvasPos(e);
  333. ctx.clearRect(0, 0, canvas.width, canvas.height);
  334. var rx = Math.min(drawStart.x, pos.x);
  335. var ry = Math.min(drawStart.y, pos.y);
  336. var rw = Math.abs(pos.x - drawStart.x);
  337. var rh = Math.abs(pos.y - drawStart.y);
  338. ctx.fillStyle = 'rgba(255, 0, 0, 0.15)';
  339. ctx.fillRect(rx, ry, rw, rh);
  340. ctx.strokeStyle = '#ff0000';
  341. ctx.lineWidth = 2;
  342. ctx.setLineDash([6, 3]);
  343. ctx.strokeRect(rx, ry, rw, rh);
  344. ctx.setLineDash([]);
  345. var scaleX = videoResolution.width / canvas.clientWidth;
  346. var scaleY = videoResolution.height / canvas.clientHeight;
  347. ctx.fillStyle = '#ff0000';
  348. ctx.font = '14px sans-serif';
  349. ctx.fillText(Math.round(rw * scaleX) + '×' + Math.round(rh * scaleY), rx + 4, ry + 18);
  350. }
  351. function onDrawEnd(e) {
  352. if (!drawStart) return;
  353. var pos = getCanvasPos(e);
  354. var x = Math.min(drawStart.x, pos.x);
  355. var y = Math.min(drawStart.y, pos.y);
  356. var w = Math.abs(pos.x - drawStart.x);
  357. var h = Math.abs(pos.y - drawStart.y);
  358. if (w < 5 || h < 5) { cancelDrawROI(); return; }
  359. finishDrawROI({ x: x, y: y, w: w, h: h });
  360. }
  361. function onTouchStart(e) {
  362. e.preventDefault();
  363. var t = e.touches[0];
  364. onDrawStart({ clientX: t.clientX, clientY: t.clientY });
  365. }
  366. function onTouchMove(e) {
  367. e.preventDefault();
  368. var t = e.touches[0];
  369. onDrawMove({ clientX: t.clientX, clientY: t.clientY });
  370. }
  371. function onTouchEnd(e) {
  372. e.preventDefault();
  373. var t = e.changedTouches[0];
  374. onDrawEnd({ clientX: t.clientX, clientY: t.clientY });
  375. }
  376. // 角色检查:仅超级管理员可看到帮助链接
  377. fetch('/api/auth/status', {credentials:'same-origin'}).then(r=>r.json()).then(data=>{
  378. if(data.authenticated && data.role >= 2){
  379. var el = document.getElementById('nav-help');
  380. if(el) el.style.display = '';
  381. }
  382. }).catch(()=>{});
  383. </script>
  384. </body>
  385. </html>