windows test

This commit is contained in:
Thies Mueller
2026-04-23 18:53:32 +02:00
parent 75537258a7
commit 07ecb4ffb1
+22 -35
View File
@@ -23,47 +23,35 @@ def save_config(cfg):
CONFIG = load_config() CONFIG = load_config()
def create_capture(index): def create_capture(index):
backends = []
if sys.platform == "darwin": if os.name == "nt":
backends = [ cap = cv2.VideoCapture(index, cv2.CAP_DSHOW)
cv2.CAP_AVFOUNDATION,
cv2.CAP_ANY
]
elif os.name == "nt":
backends = [
cv2.CAP_DSHOW,
cv2.CAP_MSMF,
cv2.CAP_ANY
]
else: else:
backends = [ cap = cv2.VideoCapture(index)
cv2.CAP_V4L2,
cv2.CAP_ANY
]
for backend in backends: if not cap.isOpened():
cap = cv2.VideoCapture(index, backend) return None
if not cap.isOpened(): cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
continue cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))
for _ in range(10): for _ in range(20):
ret, frame = cap.read() ret, frame = cap.read()
if ret and frame is not None: if ret and frame is not None:
return cap return cap
cap.release()
cap.release()
return None return None
def find_cameras(): def find_cameras():
working = [] working = []
for i in range(2): for i in range(10):
cap = create_capture(i) cap = create_capture(i)
if cap is not None: if cap is not None:
working.append(i) working.append(i)
cap.release() cap.release()
@@ -123,6 +111,8 @@ class PhotoApp(QWidget):
def init_camera(self): def init_camera(self):
cams = find_cameras() cams = find_cameras()
if not cams:
sys.exit(1)
last = CONFIG.get("camera", {}).get("last_used") last = CONFIG.get("camera", {}).get("last_used")
if last in cams: if last in cams:
@@ -134,6 +124,9 @@ class PhotoApp(QWidget):
QApplication.processEvents() QApplication.processEvents()
cam_index = selector.selected cam_index = selector.selected
if cam_index is None:
sys.exit(1)
CONFIG["camera"]["last_used"] = cam_index CONFIG["camera"]["last_used"] = cam_index
save_config(CONFIG) save_config(CONFIG)
@@ -142,12 +135,6 @@ class PhotoApp(QWidget):
if self.cap is None: if self.cap is None:
sys.exit(1) sys.exit(1)
for _ in range(10):
self.cap.read()
if not self.cap.isOpened():
sys.exit(1)
def update_frame(self): def update_frame(self):
if not self.cap: if not self.cap:
return return
@@ -175,7 +162,7 @@ class PhotoApp(QWidget):
rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB) rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = rgb.shape h, w, ch = rgb.shape
img = QImage(rgb.data, w, h, ch * w, QImage.Format.Format_RGB888) img = QImage(rgb.data, w, h, ch * w, QImage.Format.Format_RGB888).copy()
pix = QPixmap.fromImage(img) pix = QPixmap.fromImage(img)
painter = QPainter(pix) painter = QPainter(pix)