Overview
TheCameraWidget displays a live preview of the camera feed at ~30 FPS. It binds to a Camera instance and updates the preview using a Qt timer.
Module: klaus.ui.camera_widget
Inherits: QWidget (PyQt6)
Class Definition
Constants
Maximum preview width in pixels. Set to
theme.CAMERA_PREVIEW_WIDTH (280).Constructor
- A
QLabelfor displaying the video frame (#camera-previewobject name) - Minimum size: 280x210
- Maximum height: 300px
- Maximum width:
PREVIEW_WIDTH(280px) - A
QTimerfor periodic frame updates (not started untilset_camera()is called)
Layout Structure
The widget uses aQVBoxLayout with:
QLabel(_video_label) centered, fixed size 280x210
Methods
Binding a Camera
Camera instance and starts the preview if the camera is running.
Parameters:
camera: Aklaus.camera.Camerainstance (orNoneto unbind)
- If
camerais notNoneandcamera.is_runningisTrue, starts a 33ms timer (~30 FPS) - Otherwise, stops the timer
Updating Frames
camera.get_frame_rgb(), scales it to fit PREVIEW_WIDTH, and displays it in the QLabel.
Scaling:
- If frame width >
PREVIEW_WIDTH, scales down usingcv2.INTER_AREAinterpolation - Preserves aspect ratio
- Ensures frame is contiguous in memory (
np.ascontiguousarray) - Converts to
QImagewithFormat_RGB888 - Converts to
QPixmapand sets on the label
Stopping
set_camera(None) to unbind.
Frame Update Pipeline
- Timer fires every 33ms (if camera is running)
- Fetch frame via
camera.get_frame_rgb()(returnsnumpy.ndarrayorNone) - Scale if width > 280px using OpenCV
resize - Convert to
QImage(RGB888 format) - Display as
QPixmapin theQLabel
Dependencies
- cv2 (OpenCV): For frame resizing
- numpy: For array operations
- PyQt6:
QWidget,QLabel,QTimer,QImage,QPixmap - klaus.ui.theme: For
CAMERA_PREVIEW_WIDTHconstant
Usage Example
Notes
- Frame rate: ~30 FPS (33ms timer interval)
- Scaling: Only width is constrained; height scales proportionally
- No frame drop: If
get_frame_rgb()returnsNone, the widget silently skips the update - Object name:
#camera-previewfor QSS styling - Max dimensions: 280px wide, 300px tall