Spaces:
Running
Running
File size: 15,509 Bytes
d646dd7 | 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 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 | """
ๆๅๆบ็ป้ขๆงไปถ โโ ๆพ็คบไธ่ทฏๆๅๆบ็ๅฎๆถ็ป้ขๅๅฝๅถๆงๅถๆ้ฎใ
็ป้ขๆฏๆ้ผ ๆ ๆไฝ:
- ๆป่ฝฎ็ผฉๆพ๏ผไปฅ็ป้ขไธญๅฟไธบๅบๅ๏ผ
- ๅทฆ้ฎๆๆฝๅนณ็งป
- ๅๅป่ฟๅ๏ผ้็ฝฎ็ผฉๆพๅๅนณ็งป๏ผ
"""
from __future__ import annotations
from typing import Optional
import cv2
import numpy as np
from PyQt5.QtCore import Qt, pyqtSignal, QPointF, QRectF
from PyQt5.QtGui import (
QImage, QPixmap, QFont, QColor, QPainter,
)
from PyQt5.QtWidgets import (
QWidget, QVBoxLayout, QHBoxLayout, QLabel, QPushButton,
QFrame, QSizePolicy, QToolButton,
)
from config import settings
from config.i18n import tr, lang_manager
from core.helpers import format_duration
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๅฏ็ผฉๆพๆๆฝ็่ง้ขๆพ็คบๆงไปถ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class ZoomableVideoWidget(QWidget):
"""
ๆฏๆ้ผ ๆ ไบคไบ็่ง้ข็ป้ขๆงไปถใ
ๆไฝๆนๅผ:
- ๆป่ฝฎ โ ไปฅ็ป้ขไธญๅฟไธบๅบๅ็ผฉๆพ๏ผ1.1ร / 0.9ร ๆฏ็บง๏ผ
- ๆๆฝ โ ๅนณ็งป็ป้ข
- ๅๅป โ ่ฟๅๅฐ้ๅบ็ชๅฃๅคงๅฐ
็ผฉๆพ่ๅด: 0.1ร ~ 10ร
"""
ZOOM_MIN = 0.25
ZOOM_MAX = 8.0
ZOOM_STEP = 1.08 # ๆฏ็บงๆป่ฝฎๅ็๏ผๆดๅนณๆป๏ผ
def __init__(self, parent: QWidget = None):
super().__init__(parent)
self._pixmap: Optional[QPixmap] = None
self._zoom = 1.0
self._offset = QPointF(0, 0)
self._dragging = False
self._last_mouse_pos = QPointF()
self._status_text = tr("ๆ ไฟกๅท")
self._has_frame = False
# ็ผฉๆพ็ต้ๆ็คบ๏ผๅณไธ่ง็ญๆๆพ็คบ๏ผ
self._show_zoom_hint = False
self._zoom_hint_timer = 0
self.setMouseTracking(True)
self.setCursor(Qt.OpenHandCursor)
self.setMinimumSize(160, 120)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setStyleSheet("background:#000000; border:none;")
# โโ ๅ
ฌๅผๆฅๅฃ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def reset_view(self):
"""Reset zoom and pan to default."""
self._zoom = 1.0
self._offset = QPointF(0, 0)
self.update()
def set_frame(self, bgr_frame: np.ndarray, flip_vertical: bool = False):
"""ไผ ๅ
ฅ BGR ๅธงๅนถ่งฆๅ้็ปใ
flip_vertical: ๆฏๅฆไธไธ็ฟป่ฝฌ็ป้ข๏ผๆๅๆบ่ง้ข้่ฆ๏ผไผ ๆๅจๆธฒๆไธ้่ฆ๏ผใ
"""
try:
if flip_vertical:
bgr_frame = cv2.flip(bgr_frame, 0)
# BGR โ RGB ๅ็จ Format_RGB888๏ผFormat_BGR888 ๅจ Qt x86 ไธ็บข่ไบๆข๏ผ
rgb_frame = cv2.cvtColor(bgr_frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_frame.shape
qimg = QImage(rgb_frame.data, w, h, w * ch, QImage.Format_RGB888)
self._pixmap = QPixmap.fromImage(qimg)
self._has_frame = True
except Exception:
pass
if self._show_zoom_hint:
self._zoom_hint_timer -= 1
if self._zoom_hint_timer <= 0:
self._show_zoom_hint = False
self.update()
def set_status_text(self, text: str):
"""่ฎพ็ฝฎๆ ็ป้ขๆถๆพ็คบ็ๆๅญใ"""
self._status_text = text
self._has_frame = False
self._pixmap = None
self.update()
def reset_view(self):
"""่ฟๅ็ผฉๆพๅๅนณ็งปๅฐๅๅง็ถๆใ"""
self._zoom = 1.0
self._offset = QPointF(0, 0)
self.update()
# โโ ็ปๅถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def paintEvent(self, _event):
painter = QPainter(self)
painter.setRenderHint(QPainter.Antialiasing)
painter.setRenderHint(QPainter.SmoothPixmapTransform)
ww = self.width()
wh = self.height()
if self._pixmap is None or not self._has_frame:
painter.fillRect(self.rect(), Qt.black)
painter.setPen(QColor("#888888"))
painter.setFont(QFont("Microsoft YaHei", 12))
painter.drawText(self.rect(), Qt.AlignCenter, self._status_text)
return
pw = self._pixmap.width()
ph = self._pixmap.height()
base_scale = min(ww / pw, wh / ph) if pw > 0 and ph > 0 else 1.0
scale = base_scale * self._zoom
sw = pw * scale
sh = ph * scale
cx = (ww - sw) / 2.0 + self._offset.x()
cy = (wh - sh) / 2.0 + self._offset.y()
target = QRectF(cx, cy, sw, sh)
source = QRectF(0, 0, pw, ph)
painter.drawPixmap(target, self._pixmap, source)
# ็ญๆ็็ผฉๆพ็พๅๆฏๆ็คบ๏ผๅณไธ่ง๏ผ
if self._show_zoom_hint:
pct = int(self._zoom * 100)
hint = f"{pct}%"
painter.setPen(Qt.NoPen)
painter.setBrush(QColor(0, 0, 0, 160))
fm = painter.fontMetrics()
tw = fm.horizontalAdvance(hint) + 16
th = fm.height() + 8
painter.drawRoundedRect(
QRectF(ww - tw - 12, wh - th - 12, tw, th), 6, 6
)
painter.setPen(QColor("#FFFFFF"))
painter.setFont(QFont("Segoe UI", 11, QFont.Bold))
painter.drawText(
QRectF(ww - tw - 12, wh - th - 12, tw, th),
Qt.AlignCenter, hint
)
# โโ ้ผ ๆ ไบไปถ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def wheelEvent(self, event):
"""ๆป่ฝฎ็ผฉๆพ โโ ไปฅ้ผ ๆ ไฝ็ฝฎไธบๅบๅ็น็ผฉๆพใ"""
if self._pixmap is None or not self._has_frame:
return
mouse_pos = QPointF(event.pos())
old_zoom = self._zoom
if event.angleDelta().y() > 0:
self._zoom = min(self.ZOOM_MAX, self._zoom * self.ZOOM_STEP)
else:
self._zoom = max(self.ZOOM_MIN, self._zoom / self.ZOOM_STEP)
if abs(self._zoom - old_zoom) < 1e-6:
return
# ไปฅ้ผ ๆ ไฝ็ฝฎไธบๅบๅ่ฐๆดๅ็งป
ratio = self._zoom / old_zoom
self._offset = mouse_pos - ratio * (mouse_pos - self._offset)
# ๆพ็คบ็ผฉๆพ็พๅๆฏๆ็คบ๏ผๆ็ปญ็บฆ 1 ็ง๏ผ
self._show_zoom_hint = True
self._zoom_hint_timer = 30 # ~30 ๅธง โ 1 ็ง
self.update()
def mousePressEvent(self, event):
if event.button() == Qt.LeftButton:
self._dragging = True
self._last_mouse_pos = QPointF(event.pos())
self.setCursor(Qt.ClosedHandCursor)
event.accept()
def mouseMoveEvent(self, event):
if self._dragging:
delta = QPointF(event.pos()) - self._last_mouse_pos
self._offset += delta
self._last_mouse_pos = QPointF(event.pos())
self.update()
event.accept()
def mouseReleaseEvent(self, event):
if event.button() == Qt.LeftButton and self._dragging:
self._dragging = False
self.setCursor(Qt.OpenHandCursor)
event.accept()
def mouseDoubleClickEvent(self, event):
"""ๅๅป่ฟๅ็ผฉๆพๅๅนณ็งปใ"""
if event.button() == Qt.LeftButton:
self.reset_view()
event.accept()
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๆๅๆบ้ขๆฟ๏ผ็ปๅ่ง้ขๆงไปถ + ๅฝๅถๆงๅถๆ ๏ผ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
class CameraWidget(QFrame):
"""
ๅ่ทฏๆๅๆบ้ขๆฟ๏ผ่ง้ข็ป้ข + ็ถๆ่ฆ็ๆกใ
ไฟกๅท
----
remove_requested(str) โ ่ฏทๆฑ็งป้คๆญคๆๅๆบ
exposure_clicked(str) โ ็นๅปๆๅ
ๆ้ฎ๏ผไธป็ชๅฃๆๆงฝไฝๆๅฑ่ฎพๅคๅผนๅฏน่ฏๆก๏ผ
"""
remove_requested = pyqtSignal(str)
exposure_clicked = pyqtSignal(str)
def __init__(self, slot_id: str, camera_name: str = "",
parent: QWidget = None):
super().__init__(parent)
self.slot_id = slot_id
self.camera_name = camera_name or f"Camera {slot_id}"
self._fps = 0.0
self._setup_ui()
# ็ๅฌ่ฏญ่จๅๆข
lang_manager.language_changed.connect(self._refresh_texts)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# UI ๆๅปบ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def _setup_ui(self):
self.setMinimumSize(settings.FEED_MIN_WIDTH, settings.FEED_MIN_HEIGHT)
self.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
self.setStyleSheet(f"background:{settings.COLOR_BG_WIDGET}; "
f"border:1px solid {settings.COLOR_BORDER};")
main_layout = QVBoxLayout(self)
main_layout.setContentsMargins(2, 2, 2, 2)
main_layout.setSpacing(0)
# โโ ่ง้ขๆพ็คบๅบๅ๏ผๅฏ็ผฉๆพๆๆฝ๏ผ โโโโโโโโโโโโโโโโ
self.video_widget = ZoomableVideoWidget()
main_layout.addWidget(self.video_widget, 1)
# โโ ่ฆ็ๆก๏ผ่ง้ข็ป้ข้กถ้จ๏ผๅง็ปๅบๅฎๅจๅทฆไธ่ง๏ผ โโโ
self.overlay = QFrame(self.video_widget)
self.overlay.setStyleSheet(
"background:rgba(0,0,0,160); border-radius:4px;"
)
self.overlay.setFixedHeight(28)
# ๆณจๆ๏ผ่ฆ็ๆก็ปไธ่ฝ่ฎพ WA_TransparentForMouseEventsโโQt ่ฏญไนๆฏ
# ใ่ฏฅๆงไปถๅๅ
ถๅ
จ้จๅญๆงไปถไธๆฅๆถ้ผ ๆ ใ๏ผไผ่ฟ โ ๆ้ฎไธ่ตทๅฑ่ฝใ
# ไฟกๆฏๆกๆๆฝ็ฑ CameraGrid ็ไบไปถ่ฟๆปคๅจ็ปไธๆฆๆช๏ผๆ ้็ฉฟ้ใ
overlay_layout = QHBoxLayout(self.overlay)
overlay_layout.setContentsMargins(8, 0, 8, 0)
overlay_layout.setSpacing(12)
self.name_label = QLabel(self.camera_name)
self.name_label.setStyleSheet(
"color:#FFFFFF; font-weight:bold; font-size:11px; background:transparent;"
)
overlay_layout.addWidget(self.name_label)
# โ ๆๅ
ๆ้ฎ๏ผ้ป่ฎค้่๏ผไธป็ชๅฃๅชๅฏน่ฎพๅค็"ไธปๆงฝไฝ"ๆพ็คบใ
# _no_drag ๅฑๆง่ฎฉ็ฝๆ ผๆๆฝ่ฟๆปคๅจๆพ่ก๏ผๆไธไธไผ่งฆๅ็ป้ขๆๆฝ/ๅๅป่ฟๅ๏ผ
self.exposure_btn = QToolButton()
self.exposure_btn.setText("โ")
self.exposure_btn.setFixedSize(24, 22)
self.exposure_btn.setCursor(Qt.PointingHandCursor)
self.exposure_btn.setToolTip(tr("ๆๅ
่ฎพ็ฝฎ"))
self.exposure_btn._no_drag = True
self.exposure_btn.setStyleSheet(
"QToolButton{background:transparent; color:#FFD54F; border:none;"
"font-size:13px; padding:0;}"
"QToolButton:hover{background:rgba(255,255,255,40); border-radius:3px;}"
"QToolButton:disabled{color:#666666;}"
)
self.exposure_btn.clicked.connect(
lambda: self.exposure_clicked.emit(self.slot_id))
self.exposure_btn.setVisible(False)
overlay_layout.addWidget(self.exposure_btn)
overlay_layout.addStretch()
self.fps_label = QLabel("FPS: --")
self.fps_label.setStyleSheet(
"color:#AAAAAA; font-size:10px; background:transparent;"
)
overlay_layout.addWidget(self.fps_label)
self.state_dot = QLabel("โ")
self.state_dot.setStyleSheet(
f"color:{settings.COLOR_STOPPED}; font-size:10px; background:transparent;"
)
overlay_layout.addWidget(self.state_dot)
self.overlay.move(4, 4)
self.overlay.setFixedWidth(260)
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ๅ
ฌๅผๆฅๅฃ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def update_frame(self, bgr_frame: np.ndarray):
"""็จ็ธๆบ้้็ BGR ๅธงๆดๆฐ่ง้ขๆพ็คบ๏ผไธไธ็ฟป่ฝฌ้้
ๆๅๅคดๅฎ่ฃ
ๆนๅ๏ผใ"""
self.video_widget.set_frame(bgr_frame, flip_vertical=True)
def update_fps(self, fps: float):
"""ๆดๆฐ FPS ๆพ็คบใ"""
self._fps = fps
self.fps_label.setText(f"FPS: {fps:.0f}")
def set_frame_number(self, n: int):
"""ๅๆพ็จ๏ผๅจไฟกๆฏๆกๅณไพงๆพ็คบๅฝๅๅธงๅท๏ผๅค็จ FPS ๆ ็ญพไฝ๏ผใ"""
self.fps_label.setText(f"#{n}")
def set_camera_state(self, state: str):
"""ๆ นๆฎ็ธๆบ็ถๆๆดๆฐๆ็คบ็ฏ้ข่ฒๅ่ง้ขๅบๅๆๅญใ"""
if state == "recording":
self.state_dot.setStyleSheet(
f"color:{settings.COLOR_RECORDING}; font-size:10px; background:transparent;"
)
elif state == "error":
self.state_dot.setStyleSheet(
f"color:{settings.COLOR_ABNORMAL}; font-size:10px; background:transparent;"
)
elif state == "disconnected":
self.state_dot.setStyleSheet("color:#BDBDBD; font-size:10px; background:transparent;")
self.video_widget.set_status_text(tr("ๅทฒๆญๅผ"))
else:
self.state_dot.setStyleSheet(
f"color:{settings.COLOR_STOPPED}; font-size:10px; background:transparent;"
)
if state == "idle":
self.video_widget.set_status_text(tr("็ญๅพ
ไธญโฆ"))
# โโ ๆๅ
ๅ
ฅๅฃ๏ผโ ๆ้ฎ๏ผ โโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def set_exposure_button_visible(self, visible: bool):
"""ๆพ็คบ/้่ๆๅ
ๆ้ฎ๏ผๅชๅฏน่ฎพๅค็"ไธปๆงฝไฝ"ๆพ็คบ๏ผใ"""
self.exposure_btn.setVisible(visible)
def set_exposure_enabled(self, enabled: bool):
"""ๅฏ็จ/็ฆ็จๆๅ
ๆ้ฎ๏ผๅฝๅถไธญ็ฆ็จ๏ผใ"""
self.exposure_btn.setEnabled(enabled)
self.exposure_btn.setToolTip(
tr("ๆๅ
่ฎพ็ฝฎ") if enabled else tr("ๅฝๅถไธญไธๅฏ่ฐๆดๆๅ
"))
def _refresh_texts(self, _lang: str = ""):
"""่ฏญ่จๅๆขๆถๅทๆฐๆๅญใ"""
self.exposure_btn.setToolTip(
tr("ๆๅ
่ฎพ็ฝฎ") if self.exposure_btn.isEnabled()
else tr("ๅฝๅถไธญไธๅฏ่ฐๆดๆๅ
"))
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
# ไบไปถ้ๅ
# โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
def resizeEvent(self, event):
"""็ชๅฃๅคงๅฐๅๅๆถ่ฐๆด่ฆ็ๆกไฝ็ฝฎๅๅฎฝๅบฆใ"""
super().resizeEvent(event)
self.overlay.setFixedWidth(min(self.video_widget.width() - 8, 300))
self.overlay.move(4, 4)
|