126 lines
4.1 KiB
Python
126 lines
4.1 KiB
Python
import sys
|
||
import cv2
|
||
import numpy as np
|
||
from PySide6.QtWidgets import QApplication, QWidget, QLabel, QPushButton, QVBoxLayout, QHBoxLayout
|
||
from PySide6.QtGui import QImage, QPixmap
|
||
from PySide6.QtCore import QTimer
|
||
import gphoto2 as gp
|
||
|
||
class LiveViewApp(QWidget):
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.setWindowTitle("Canon LiveView")
|
||
|
||
# Widget do obrazu
|
||
self.image_label = QLabel("Brak obrazu")
|
||
self.image_label.setFixedSize(640, 480)
|
||
|
||
# Przyciski start/stop
|
||
self.start_button = QPushButton("Start LiveView")
|
||
self.stop_button = QPushButton("Stop LiveView")
|
||
self.stop_button.setEnabled(False)
|
||
|
||
self.start_button.clicked.connect(self.start_liveview)
|
||
self.stop_button.clicked.connect(self.stop_liveview)
|
||
|
||
# Layout
|
||
button_layout = QHBoxLayout()
|
||
button_layout.addWidget(self.start_button)
|
||
button_layout.addWidget(self.stop_button)
|
||
|
||
layout = QVBoxLayout()
|
||
layout.addWidget(self.image_label)
|
||
layout.addLayout(button_layout)
|
||
|
||
self.setLayout(layout)
|
||
|
||
# Timer do odświeżania obrazu
|
||
self.timer = QTimer()
|
||
self.timer.timeout.connect(self.update_frame)
|
||
|
||
# kamera (na razie None – podepniesz gphoto2)
|
||
self.camera = None
|
||
self.set_dummy_frame()
|
||
|
||
|
||
|
||
def set_dummy_frame(self):
|
||
self.dummy_frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
||
cv2.putText(self.dummy_frame, "LiveView OFF", (200, 240),
|
||
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
||
|
||
rgb_image = cv2.cvtColor(self.dummy_frame, cv2.COLOR_BGR2RGB)
|
||
h, w, ch = rgb_image.shape
|
||
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format_RGB888)
|
||
pixmap = QPixmap.fromImage(qimg)
|
||
|
||
self.image_label.setPixmap(pixmap)
|
||
|
||
def start_liveview(self):
|
||
print("Start LiveView")
|
||
self.start_button.setEnabled(False)
|
||
self.stop_button.setEnabled(True)
|
||
# TODO: tu zainicjalizujesz kamerę gphoto2
|
||
# Przykład inicjalizacji kamery przez gphoto2 (wymaga zainstalowanego python-gphoto2)
|
||
try:
|
||
self.camera = gp.Camera()
|
||
self.camera.init()
|
||
except Exception as e:
|
||
print(f"Błąd inicjalizacji kamery: {e}")
|
||
self.camera = None
|
||
self.timer.start(100) # odświeżanie co 100ms
|
||
|
||
def stop_liveview(self):
|
||
print("Stop LiveView")
|
||
self.timer.stop()
|
||
self.start_button.setEnabled(True)
|
||
self.stop_button.setEnabled(False)
|
||
self.image_label.setText("Brak obrazu")
|
||
self.image_label.setPixmap(QPixmap()) # czyści obraz
|
||
|
||
self.set_dummy_frame()
|
||
# TODO: tu zamkniesz kamerę gphoto2
|
||
if self.camera:
|
||
self.camera.exit()
|
||
self.camera = None
|
||
self.start_button.setEnabled(True)
|
||
self.stop_button.setEnabled(False)
|
||
|
||
def update_frame(self):
|
||
# Na razie sztuczna klatka testowa z OpenCV (czarne tło + napis)
|
||
# frame = np.zeros((480, 640, 3), dtype=np.uint8)
|
||
# cv2.putText(frame, "Canon LiveView", (50, 240),
|
||
# cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
||
|
||
if self.camera:
|
||
try:
|
||
file = self.camera.capture_preview()
|
||
data = file.get_data_and_size()
|
||
frame = np.frombuffer(data, dtype=np.uint8)
|
||
frame = cv2.imdecode(frame, cv2.IMREAD_COLOR)
|
||
|
||
if frame is None:
|
||
return
|
||
|
||
except gp.GPhoto2Error as e:
|
||
print(f"Błąd odczytu LiveView: {e}")
|
||
return
|
||
except Exception as e:
|
||
print(f"Nieoczekiwany błąd: {e}")
|
||
return
|
||
else:
|
||
return
|
||
# Konwersja OpenCV -> QImage
|
||
rgb_image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
|
||
h, w, ch = rgb_image.shape
|
||
qimg = QImage(rgb_image.data, w, h, ch * w, QImage.Format_RGB888)
|
||
pixmap = QPixmap.fromImage(qimg)
|
||
|
||
self.image_label.setPixmap(pixmap)
|
||
|
||
if __name__ == "__main__":
|
||
app = QApplication(sys.argv)
|
||
window = LiveViewApp()
|
||
window.show()
|
||
sys.exit(app.exec())
|