148 lines
3.6 KiB
Python
148 lines
3.6 KiB
Python
import cv2
|
|
import numpy as np
|
|
|
|
GP_WIDGET_WINDOW = 0
|
|
GP_WIDGET_SECTION = 0
|
|
GP_WIDGET_TEXT = 0
|
|
GP_WIDGET_RANGE = 0
|
|
GP_WIDGET_TOGGLE = 0
|
|
GP_WIDGET_RADIO = 0
|
|
GP_WIDGET_MENU = 0
|
|
GP_WIDGET_BUTTON = 0
|
|
GP_WIDGET_DATE = 0
|
|
|
|
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 CameraListMock:
|
|
def count(self):
|
|
return 1
|
|
|
|
def get_name(self, idx):
|
|
return f"mock_name {idx}"
|
|
|
|
def get_value(self, idx):
|
|
return f"mock_value {idx}"
|
|
|
|
class MockPortInfo:
|
|
def __init__(self, address):
|
|
self.address = address
|
|
|
|
class PortInfoList:
|
|
def __init__(self):
|
|
self._ports = []
|
|
|
|
def load(self):
|
|
# Dodaj przykładowe porty
|
|
self._ports = [MockPortInfo("usb:001,002"), MockPortInfo("usb:001,003")]
|
|
|
|
def lookup_path(self, port_address):
|
|
for idx, port in enumerate(self._ports):
|
|
if port.address == port_address:
|
|
return idx
|
|
raise ValueError("Port not found")
|
|
|
|
def __getitem__(self, idx):
|
|
return self._ports[idx]
|
|
|
|
class ConfigMock:
|
|
def get_id(self):
|
|
return 0
|
|
def get_name(self):
|
|
return "name"
|
|
def get_label(self):
|
|
return "label"
|
|
def get_type(self):
|
|
return 0
|
|
def get_value(self):
|
|
return "value"
|
|
def get_choices(self):
|
|
return []
|
|
def count_children(self):
|
|
return 0
|
|
def get_child(self):
|
|
return ConfigMock()
|
|
|
|
|
|
class CameraAbilitiesList:
|
|
def __init__(self) -> None:
|
|
self.abilities = []
|
|
def load(self):
|
|
return
|
|
def lookup_model(self, name):
|
|
return 1
|
|
def get_abilities(self, abilities_index):
|
|
return 0
|
|
|
|
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)
|
|
|
|
def set_port_info(self, obj):
|
|
return False
|
|
|
|
def get_config(self):
|
|
return ConfigMock()
|
|
|
|
def set_single_config(self, name, widget):
|
|
return True
|
|
|
|
def gp_camera_autodetect():
|
|
return CameraListMock()
|
|
|
|
def check_result(obj):
|
|
return obj |