refactor: update detect method in camera classes to return dictionaries instead of lists
This commit is contained in:
@@ -5,6 +5,11 @@ class BaseCamera(ABC):
|
|||||||
def __init__(self) -> None:
|
def __init__(self) -> None:
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
|
|
||||||
|
@staticmethod
|
||||||
|
@abstractmethod
|
||||||
|
def detect() -> dict:
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def connect(self, index: int | None = None) -> bool:
|
def connect(self, index: int | None = None) -> bool:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|||||||
@@ -24,20 +24,21 @@ class GPhotoCamera(BaseCamera):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.camera = None
|
self.camera = None
|
||||||
self.configs: List[dict] = []
|
self.configs: List[dict] = []
|
||||||
self.camera_list = []
|
self.camera_index = 0
|
||||||
|
|
||||||
def detect(self) -> list:
|
@staticmethod
|
||||||
self.camera_list.clear()
|
def detect() -> dict:
|
||||||
cameras = gp.check_result(gp.gp_camera_autodetect()) # type: ignore
|
cameras = gp.check_result(gp.gp_camera_autodetect()) # type: ignore
|
||||||
# cameras = gp.Camera().autodetect()
|
# cameras = gp.Camera().autodetect()
|
||||||
if not cameras or cameras.count() == 0: # type: ignore
|
if not cameras or cameras.count() == 0: # type: ignore
|
||||||
return []
|
return {}
|
||||||
|
|
||||||
|
camera_list = {}
|
||||||
for i in range(cameras.count()): # type: ignore
|
for i in range(cameras.count()): # type: ignore
|
||||||
name = cameras.get_name(i) # type: ignore
|
name = cameras.get_name(i) # type: ignore
|
||||||
port = cameras.get_value(i) # type: ignore
|
port = cameras.get_value(i) # type: ignore
|
||||||
self.camera_list.append({"name": name, "port": port})
|
camera_list[i] = {"name": name, "port": port}
|
||||||
return self.camera_list
|
return camera_list
|
||||||
|
|
||||||
def connect(self, index: int | None = None) -> bool:
|
def connect(self, index: int | None = None) -> bool:
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
@@ -45,10 +46,12 @@ class GPhotoCamera(BaseCamera):
|
|||||||
|
|
||||||
try:
|
try:
|
||||||
if index:
|
if index:
|
||||||
|
self.camera_index = index
|
||||||
|
camera_list = GPhotoCamera.detect()
|
||||||
port_info_list = gp.PortInfoList()
|
port_info_list = gp.PortInfoList()
|
||||||
port_info_list.load()
|
port_info_list.load()
|
||||||
|
|
||||||
port_address = self.camera_list[index]["port"]
|
port_address = camera_list[index]["port"]
|
||||||
port_index = port_info_list.lookup_path(port_address)
|
port_index = port_info_list.lookup_path(port_address)
|
||||||
|
|
||||||
self.camera.set_port_info(port_info_list[port_index])
|
self.camera.set_port_info(port_info_list[port_index])
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ from typing import List
|
|||||||
from .base_camera import BaseCamera
|
from .base_camera import BaseCamera
|
||||||
|
|
||||||
|
|
||||||
class CvCamera(BaseCamera):
|
class OpenCvCamera(BaseCamera):
|
||||||
"""Kamera oparta na cv2.VideoCapture"""
|
"""Kamera oparta na cv2.VideoCapture"""
|
||||||
|
|
||||||
config_map = {
|
config_map = {
|
||||||
@@ -24,13 +24,28 @@ class CvCamera(BaseCamera):
|
|||||||
self.camera_list = []
|
self.camera_list = []
|
||||||
self.camera_index = 0
|
self.camera_index = 0
|
||||||
|
|
||||||
def detect(self) -> list:
|
@staticmethod
|
||||||
self.camera_list.clear()
|
def detect():
|
||||||
self.camera_list = enumerate_cameras(cv2.CAP_ANY)
|
camera_list = enumerate_cameras(cv2.CAP_ANY)
|
||||||
|
result = {}
|
||||||
|
seen_ports = set()
|
||||||
|
|
||||||
result = []
|
for camera in camera_list:
|
||||||
for camera in self.camera_list:
|
# unikamy duplikatów tego samego /dev/videoX albo tej samej ścieżki na Windows/macOS
|
||||||
result.append({"name": camera.name, "port": camera.path})
|
if camera.path in seen_ports:
|
||||||
|
continue
|
||||||
|
|
||||||
|
cap = cv2.VideoCapture(camera.index, camera.backend) # próbujemy otworzyć
|
||||||
|
ret, frame = cap.read()
|
||||||
|
cap.release()
|
||||||
|
|
||||||
|
if ret and frame is not None and frame.size > 0:
|
||||||
|
result[camera.index] = {
|
||||||
|
"name": camera.name,
|
||||||
|
"port": camera.path,
|
||||||
|
"backend": camera.backend,
|
||||||
|
}
|
||||||
|
seen_ports.add(camera.path)
|
||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
@@ -38,7 +53,7 @@ class CvCamera(BaseCamera):
|
|||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
try:
|
try:
|
||||||
if index:
|
if index:
|
||||||
self.camera_index = self.camera_list[index].index
|
self.camera_index = index
|
||||||
|
|
||||||
self.cap = cv2.VideoCapture(self.camera_index)
|
self.cap = cv2.VideoCapture(self.camera_index)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user