65 lines
1.8 KiB
Python
65 lines
1.8 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
class GPhoto2Error(Exception):
|
|
pass
|
|
|
|
|
|
class CameraFileMock:
|
|
"""Mock obiektu zwracanego przez gphoto2.Camera.capture_preview()"""
|
|
|
|
def __init__(self, frame: np.ndarray):
|
|
# Kodowanie do JPEG, żeby symulować prawdziwe dane z kamery
|
|
success, buf = cv2.imencode(".jpg", frame)
|
|
if not success:
|
|
raise GPhoto2Error("Nie udało się zakodować ramki testowej.")
|
|
self._data = buf.tobytes()
|
|
|
|
def get_data_and_size(self):
|
|
return self._data
|
|
return self._data, len(self._data)
|
|
|
|
|
|
class Camera:
|
|
def __init__(self):
|
|
self._frame_counter = 0
|
|
self._running = False
|
|
|
|
def init(self):
|
|
self._running = True
|
|
print("[my_gphoto] Kamera MOCK zainicjalizowana")
|
|
|
|
def exit(self):
|
|
self._running = False
|
|
print("[my_gphoto] Kamera MOCK wyłączona")
|
|
|
|
def capture_preview(self):
|
|
if not self._running:
|
|
raise GPhoto2Error("Kamera MOCK nie jest uruchomiona")
|
|
|
|
# przykład 1: wczytaj stały obrazek z pliku
|
|
# frame = cv2.imread("test_frame.jpg")
|
|
# if frame is None:
|
|
# raise GPhoto2Error("Nie znaleziono test_frame.jpg")
|
|
|
|
# przykład 2: wygeneruj kolorową planszę
|
|
h, w = 480, 640
|
|
color = (self._frame_counter % 255, 100, 200)
|
|
frame = np.full((h, w, 3), color, dtype=np.uint8)
|
|
|
|
# dodanie napisu
|
|
text = "OBRAZ TESTOWY"
|
|
font = cv2.FONT_HERSHEY_SIMPLEX
|
|
scale = 1.5
|
|
thickness = 3
|
|
color_text = (255, 255, 255)
|
|
|
|
(text_w, text_h), _ = cv2.getTextSize(text, font, scale, thickness)
|
|
x = (w - text_w) // 2
|
|
y = (h + text_h) // 2
|
|
cv2.putText(frame, text, (x, y), font, scale, color_text, thickness, cv2.LINE_AA)
|
|
|
|
|
|
self._frame_counter += 1
|
|
return CameraFileMock(frame)
|