Qml Webcam Integration
Unsolved
Qt for Python
-
wrote on 7 Apr 2025, 10:59 last edited by
Hello, I want to monitoring my webcam screen on my UI. I am using jetson nano ubuntu pc and I didn't make that. I can select the webcams but webcam's screen does not appear on my UI.
This is my qml part.
/ Camera view container
Rectangle {
id: viewfinder
anchors.fill: parent
color: "black"MediaDevices { id: mediaDevices } CaptureSession { id: captureSession camera: Camera { id: camera // Activate the camera when isActive is true active: root.isActive onErrorOccurred: { console.error("Camera error:", errorString) root.error(errorString) } onActiveChanged: { console.log("Camera active changed:", active) } } videoOutput: VideoOutput { id: videoOutput anchors.fill: parent fillMode: VideoOutput.PreserveAspectFit onSourceRectChanged: { console.log("Video source rect changed:", sourceRect) if (sourceRect.width > 0 && sourceRect.height > 0) { busyIndicator.running = false } } } }
and this is my .py part
@Slot(str, result=bool)
def start_camera(self, device_id):
"""Start the selected camera using Qt Multimedia."""
try:
print(f"Attempting to start Qt camera with ID: {device_id}")
self.cleanup_camera()
return self._start_qt_camera(device_id)
except Exception as e:
print(f"Error starting camera: {str(e)}")
return Falsedef _start_qt_camera(self, device_id): """Internal method to start the camera via Qt Multimedia.""" try: devices = QMediaDevices() available_cameras = devices.videoInputs() print(f"Available cameras: {[cam.description() for cam in available_cameras]}") # Debug log camera_info = None for cam in available_cameras: if cam.id().data().decode() == device_id: camera_info = cam break if camera_info: print(f"Starting camera: {camera_info.description()}") # Debug log self.current_camera = QCamera(camera_info) self.current_camera.start() import time time.sleep(0.5) if self.current_camera.isActive(): print("Camera is active.") # Debug log self.capture_session = QMediaCaptureSession() self.capture_session.setCamera(self.current_camera) self.image_capture = QImageCapture() self.capture_session.setImageCapture(self.image_capture) self.image_capture.imageSaved.connect( lambda id, path: self.imageSaved.emit(path) ) return True else: print("Camera is not active after starting.") # Debug log else: print("No matching camera found.") # Debug log return False except Exception as e: print(f"Error starting Qt camera: {str(e)}") return False!
Disconnected is not important, it is not related to webcam in screenshoot.
1/1