Change to CAP_DSHOW for VideoCapture

This commit is contained in:
DuOtto
2026-07-31 09:02:21 +02:00
parent 09e94b218c
commit 2e6f444302
6 changed files with 125 additions and 39 deletions
+20 -14
View File
@@ -436,25 +436,25 @@ class HomeWindow(QWidget):
for camera in self.cameraList: # Loop all Cameras
ok, frame = camera.capture.read() #Capture a frame
if not ok: # Check if captured frame is valid, if even one frame is invalide the whole capture failed
if not ok: # A single failed grab shouldn't discard what other cameras saw
self.logger.log(f"Could not grab frame for calibration sample, from camera {camera.index}.")
return
h, w = frame.shape[:2]
continue
h, w = frame.shape[:2]
self.logger.log(f"cam{camera.index} capture size = {w}x{h}")
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Turn the frame black and white
gray = 255 - gray # Invert the color of the frame, as the ChArCuo board I use is inverted (to save printer Ink)
h, w = gray.shape[:2]
h, w = gray.shape[:2]
self.logger.log(f"cam{camera.index} capture size = {w}x{h}")
#This is apparently a fast check, bevor i detect the actuall board
corner, ids, _ = self.arcuo_detector.detectMarkers(gray) #Detect ChArCuo Markers
if ids is None or len(ids) == 0: #Check if Markers where found, if none where found the sample is invalid
if ids is None or len(ids) == 0: #Board not visible from this camera this time, skip it, not the whole sample
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
return
continue
# Detecting the Charcuo board and evaluating if its good as a sample
result = self.charuco_detector.detectBoard(gray)
@@ -463,12 +463,12 @@ class HomeWindow(QWidget):
if charuco_ids is None:
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
return
continue
if len(charuco_ids) < MIN_CHARUCO_CORNERS:
self.logger.log(f"Not enought markers found for calibration sample, from camera {camera.index}.")
return
continue
# Adding the sample for the current camera to detections
detections[camera.index] = CharucoDetection(
camera_id = camera.index,
@@ -477,12 +477,18 @@ class HomeWindow(QWidget):
gray_frame = gray
)
# All cameras found valide markers so the sample capture was succesfull
# Need at least two cameras' worth of detections for this sample to be
# useful for stereo pairing (a single-camera detection can still help
# that camera's intrinsics, but can't contribute to any camera pair).
if len(detections) < 2:
self.logger.log("Not enough cameras saw the board in this sample, discarding.")
return
self.calibration_samples.append(
CalibrationSample(detections=detections)
)
self.logger.log("Added calibration sample.")
self.logger.log(f"Added calibration sample ({len(detections)}/{len(self.cameraList)} cameras saw the board).")
def updateCameras(self):
self._clearLayout()
+17 -25
View File
@@ -1,8 +1,7 @@
import cv2, numpy as np
import os
from Tbd.helper import CameraObject
from Tbd.helper import CameraObject, DShowMJPGCapture
from UI.UILogger import UILogger
from PySide6.QtCore import Qt, QTimer
@@ -98,31 +97,24 @@ class SetupPage(QWidget):
# index=None
#)
for index in indexToAdd:
capture = cv2.VideoCapture(index, cv2.CAP_MSMF)
#capture = cv2.VideoCapture(index, cv2.CAP_DSHOW)
try:
capture = DShowMJPGCapture(index, 2592, 1944, fourcc_substr="MJPG")
except RuntimeError as e:
self.logger.log(str(e))
print(str(e))
continue
capture.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
capture.set(cv2.CAP_PROP_FPS, 30)
capture.set(cv2.CAP_PROP_FRAME_WIDTH, 2592)
capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1944)
self.logger.log(f"Selected format: {capture.selected_format}")
print(f"Selected format: {capture.selected_format}")
#capture.set(cv2.CAP_PROP_FRAME_WIDTH, 2048.0)
#capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1536.0)
width = capture.get(cv2.CAP_PROP_FRAME_WIDTH)
height = capture.get(cv2.CAP_PROP_FRAME_HEIGHT)
fps = capture.get(cv2.CAP_PROP_FPS)
ret, frame = capture.read()
if ret:
self.logger.log(f"Actual frame shape: {frame.shape}")
print(f"Actual 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")
fourcc = int(capture.get(cv2.CAP_PROP_FOURCC))
fourcc_str = "".join([chr((fourcc >> 8*i) & 0xFF) for i in range(4)])
print(f"Resolution: {width}x{height}")
print(f"FPS: {fps}")
print(f"Format (FOURCC): {fourcc_str}")
self.logger.log(f"Resolution: {width}x{height}")
self.logger.log(f"FPS: {fps}")
self.logger.log(f"Format (FOURCC): {fourcc_str}")
#currentcapture = cv2.VideoCapture(index, cv2.CAP_MSMF)
#self.logger.log("Initial frame:", w, "x", h)
self.cameraList.append(CameraObject(
capture=capture,
@@ -185,7 +177,7 @@ class SetupPage(QWidget):
index = int(self.cameraIndex.text())
self.release_camera()
self.cameraObject.capture = cv2.VideoCapture(index)
self.cameraObject.capture = cv2.VideoCapture(index, cv2.CAP_DSHOW)
self.cameraObject.index = index
if not self.cameraObject.capture.isOpened():