Fix camera capture reliability and rewrite tracking for available mediapipe/hardware

This commit is contained in:
2026-07-31 14:01:06 +02:00
parent 2e6f444302
commit db5d09e156
12 changed files with 400 additions and 420 deletions
+27 -36
View File
@@ -1,7 +1,7 @@
import cv2, numpy as np
import os
from Tbd.helper import CameraObject, DShowMJPGCapture
from Tbd.helper import CameraObject
from UI.UILogger import UILogger
from PySide6.QtCore import Qt, QTimer
@@ -90,39 +90,31 @@ class SetupPage(QWidget):
else:
self.logger.log("Path not found")
indexToAdd = [0, 3]
#self.cameraObject = CameraObject(
# capture=None,
# index=None
#)
indexToAdd = [1, 2, 3, 4, 5, 6]
# Cameras are verified one at a time and left closed afterwards - this
# hardware can't sustain many concurrent capture graphs (confirmed via
# both CAP_DSHOW and a raw DirectShow graph), so callers open a camera
# right before they need it (CameraSetupWidget.Start, calibration capture)
# and close it right after, instead of keeping all 6 open simultaneously.
for index in indexToAdd:
try:
capture = DShowMJPGCapture(index, 2592, 1944, fourcc_substr="MJPG")
except RuntimeError as e:
self.logger.log(str(e))
print(str(e))
camera = CameraObject(capture=None, index=index)
if not camera.open():
self.logger.log(f"cam{index}: failed to open, skipping.")
continue
self.logger.log(f"Selected format: {capture.selected_format}")
print(f"Selected format: {capture.selected_format}")
ret, frame = capture.read()
ret, frame = camera.capture.read()
if ret:
self.logger.log(f"Actual frame shape: {frame.shape}")
print(f"Actual frame shape: {frame.shape}")
self.logger.log(f"cam{index}: verified, frame shape={frame.shape}")
else:
self.logger.log("Failed to read a frame after configuring camera")
print("Failed to read a frame after configuring camera")
self.logger.log(f"cam{index}: opened but failed to read a frame.")
#self.logger.log("Initial frame:", w, "x", h)
self.cameraList.append(CameraObject(
capture=capture,
index=index
))
camera.close()
self.cameraList.append(camera)
self._cameraList_changed()
return
@@ -177,15 +169,16 @@ class SetupPage(QWidget):
index = int(self.cameraIndex.text())
self.release_camera()
self.cameraObject.capture = cv2.VideoCapture(index, cv2.CAP_DSHOW)
self.cameraObject.index = index
if not self.cameraObject.capture.isOpened():
self.cameraObject.capture = None
self.cameraObject.index = None
# Reuse the CameraObject from cameraList (if this index is already known)
# instead of opening a second handle to the same physical device.
existing = next((cam for cam in self.cameraList if cam.index == index), None)
self.cameraObject = existing if existing is not None else CameraObject(capture=None, index=index)
if not self.cameraObject.open():
self.camera_preview.setText(f"Failed to open camera {index}")
return False
self.camera_preview.setText("")
return True
@@ -193,10 +186,8 @@ class SetupPage(QWidget):
def release_camera(self):
if self.cameraObject.capture is not None:
self.timer.stop()
self.cameraObject.capture.release()
self.cameraObject.capture = None
self.cameraObject.index = None
self.cameraObject.close()
def closeEvent(self, event):
self.release_camera()
event.accept()