#!/usr/bin/env python3 """Test liveview avec PyQt6. Lance: python3 scripts/test_pyqt6.py""" import sys import time import io import gphoto2 as gp from PyQt6.QtWidgets import QApplication, QLabel, QMainWindow, QVBoxLayout, QWidget from PyQt6.QtCore import QThread, pyqtSignal, Qt from PyQt6.QtGui import QPixmap, QImage class PreviewThread(QThread): frame_ready = pyqtSignal(bytes, float) premier_frame_signal = pyqtSignal(float) def __init__(self, cam): super().__init__() self.cam = cam self._running = True self._premier = False def run(self): t_start = time.time() while self._running: try: t0 = time.time() f = self.cam.capture_preview() data = bytes(f.get_data_and_size()) dt = time.time() - t0 if not self._premier: self._premier = True self.premier_frame_signal.emit(time.time() - t_start) self.frame_ready.emit(data, dt) except Exception as e: print(f"Erreur preview: {e}") break def stop(self): self._running = False class MainWindow(QMainWindow): def __init__(self, cam): super().__init__() self.setWindowTitle("Test liveview PyQt6") self.resize(960, 640) self.t_start = time.time() self.frame_count = 0 self.label_img = QLabel() self.label_img.setAlignment(Qt.AlignmentFlag.AlignCenter) self.label_info = QLabel("Attente premiere image...") self.label_info.setStyleSheet("color: yellow; background: black; font-size: 18px; padding: 5px;") layout = QVBoxLayout() layout.addWidget(self.label_img) layout.addWidget(self.label_info) widget = QWidget() widget.setLayout(layout) self.setCentralWidget(widget) self.thread = PreviewThread(cam) self.thread.frame_ready.connect(self.on_frame) self.thread.premier_frame_signal.connect(self.on_premier) self.thread.start() def on_premier(self, t): print(f"1ere image en : {t:.2f}s") def on_frame(self, data, dt): self.frame_count += 1 img = QImage.fromData(data) pix = QPixmap.fromImage(img).scaled(960, 600, Qt.AspectRatioMode.KeepAspectRatio) self.label_img.setPixmap(pix) fps = self.frame_count / (time.time() - self.t_start) self.label_info.setText(f"FPS: {fps:.1f} frame: {dt*1000:.0f}ms total frames: {self.frame_count}") def closeEvent(self, e): self.thread.stop() self.thread.wait() super().closeEvent(e) cam = gp.Camera() cam.init() print("Camera connectee") app = QApplication(sys.argv) win = MainWindow(cam) win.show() ret = app.exec() cam.exit() sys.exit(ret)