70 lines
2.3 KiB
Python
70 lines
2.3 KiB
Python
import cv2
|
|
import gphoto2 as gp
|
|
|
|
from controllers.camera_controller import CameraController
|
|
from .gphoto_adapter import GPhotoImageSource, GPhotoControlSource
|
|
from .opencv_adapter import OpenCVImageSource, OpenCVControlSource
|
|
|
|
|
|
class CameraManager:
|
|
def __init__(self):
|
|
self.devices = [] # lista wykrytych kamer
|
|
|
|
def detect_devices(self):
|
|
self.devices.clear()
|
|
|
|
# --- Wykrywanie webcamów / grabberów HDMI
|
|
for index in range(5): # sprawdź kilka indeksów
|
|
cap = cv2.VideoCapture(index)
|
|
if cap.isOpened():
|
|
self.devices.append({
|
|
"id": f"opencv:{index}",
|
|
"name": f"Webcam / HDMI Grabber #{index}",
|
|
"type": "opencv",
|
|
"index": index
|
|
})
|
|
cap.release()
|
|
|
|
# --- Wykrywanie kamer gphoto2
|
|
cameras = gp.Camera.autodetect() # type: ignore
|
|
for i, (name, addr) in enumerate(cameras):
|
|
self.devices.append({
|
|
"id": f"gphoto:{i}",
|
|
"name": f"{name} ({addr})",
|
|
"type": "gphoto",
|
|
"addr": addr
|
|
})
|
|
|
|
return self.devices
|
|
|
|
def create_controller(self, device_id, hybrid_with=None):
|
|
"""
|
|
Tworzy CameraController na podstawie id urządzenia.
|
|
Można podać hybrid_with="opencv" albo "gphoto" żeby zbudować hybrydę.
|
|
"""
|
|
device = next((d for d in self.devices if d["id"] == device_id), None)
|
|
if not device:
|
|
raise ValueError(f"Nie znaleziono urządzenia {device_id}")
|
|
|
|
# Webcam / grabber
|
|
if device["type"] == "opencv":
|
|
cap = cv2.VideoCapture(device["index"])
|
|
img = OpenCVImageSource(device["index"])
|
|
ctrl = OpenCVControlSource(cap)
|
|
return CameraController(img, ctrl)
|
|
|
|
# GPhoto camera
|
|
elif device["type"] == "gphoto":
|
|
cam = gp.Camera() # type: ignore
|
|
cam.init()
|
|
img = GPhotoImageSource(cam)
|
|
ctrl = GPhotoControlSource(cam)
|
|
return CameraController(img, ctrl)
|
|
|
|
# Hybrydowy tryb
|
|
elif device["type"] == "hybrid":
|
|
raise NotImplementedError("Tu możesz połączyć OpenCV + GPhoto w hybrydę")
|
|
|
|
else:
|
|
raise ValueError(f"Nieobsługiwany typ urządzenia: {device['type']}")
|