refactor: enhance camera rotation functionality and integrate with live view

This commit is contained in:
2025-10-13 20:54:04 +02:00
parent 4d00f83083
commit 0bc6c01e7e
3 changed files with 46 additions and 11 deletions

View File

@@ -18,6 +18,7 @@ class CameraWorker(QObject):
self.fps = 15
self.is_streaming = False
self.is_connected = False
self._rotation_index = 0
self._camera_mutex = QMutex()
@Slot()
@@ -93,13 +94,30 @@ class CameraWorker(QObject):
self.error_occurred.emit(error_msg)
return
if frame is not None:
# Process the frame and emit it.
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
self.frame_ready.emit(pixmap)
if frame is None:
return
if self._rotation_index == 1:
frame = cv2.rotate(frame, cv2.ROTATE_90_CLOCKWISE)
elif self._rotation_index == 2:
frame = cv2.rotate(frame, cv2.ROTATE_180)
elif self._rotation_index == 3:
frame = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
# Process the frame and emit it.
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb_image.shape
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format.Format_RGB888)
pixmap = QPixmap.fromImage(qimg)
self.frame_ready.emit(pixmap)
@Slot()
def rotate_left(self):
self._rotation_index = (self._rotation_index - 1) % 4
@Slot()
def rotate_right(self):
self._rotation_index = (self._rotation_index + 1) % 4
@@ -115,6 +133,8 @@ class CameraController(QObject):
_stop_camera_requested = Signal()
_start_stream_requested = Signal()
_stop_stream_requested = Signal()
_rotate_left_requested = Signal()
_rotate_right_requested = Signal()
def __init__(self, parent: QObject | None = None) -> None:
super().__init__(parent)
@@ -135,6 +155,8 @@ class CameraController(QObject):
self._stop_camera_requested.connect(self._worker.stop_camera)
self._start_stream_requested.connect(self._worker.start_stream)
self._stop_stream_requested.connect(self._worker.stop_stream)
self._rotate_left_requested.connect(self._worker.rotate_left)
self._rotate_right_requested.connect(self._worker.rotate_right)
# Initialize worker when thread starts
self._thread.started.connect(self._worker.initialize_worker)
@@ -162,4 +184,11 @@ class CameraController(QObject):
def stop_stream(self) -> None:
self._stop_stream_requested.emit()
def rotate_left(self):
self._rotate_left_requested.emit()
def rotate_right(self):
self._rotate_right_requested.emit()

View File

@@ -115,6 +115,12 @@ class CameraManager(QObject):
def get_active_camera_info(self) -> dict | None:
return self._active_camera_info
def rotate_left(self):
self._camera_controller.rotate_left()
def rotate_right(self):
self._camera_controller.rotate_right()
def shutdown(self) -> None:
"""Zamyka kontroler kamery i jego wątek."""
self.stop_camera()