refactor: update camera classes to improve initialization and connection handling
This commit is contained in:
@@ -6,7 +6,7 @@ class BaseCamera(ABC):
|
|||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
def connect(self) -> bool:
|
def connect(self, index: int | None = None) -> bool:
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
@abstractmethod
|
@abstractmethod
|
||||||
|
|||||||
@@ -24,11 +24,35 @@ class GPhotoCamera(BaseCamera):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
self.camera = None
|
self.camera = None
|
||||||
self.configs: List[dict] = []
|
self.configs: List[dict] = []
|
||||||
|
self.camera_list = []
|
||||||
|
|
||||||
def connect(self) -> bool:
|
def detect(self) -> list:
|
||||||
|
self.camera_list.clear()
|
||||||
|
cameras = gp.check_result(gp.gp_camera_autodetect()) # type: ignore
|
||||||
|
# cameras = gp.Camera().autodetect()
|
||||||
|
if not cameras or cameras.count() == 0: # type: ignore
|
||||||
|
return []
|
||||||
|
|
||||||
|
for i in range(cameras.count()): # type: ignore
|
||||||
|
name = cameras.get_name(i) # type: ignore
|
||||||
|
port = cameras.get_value(i) # type: ignore
|
||||||
|
self.camera_list.append({"name": name, "port": port})
|
||||||
|
return self.camera_list
|
||||||
|
|
||||||
|
def connect(self, index: int | None = None) -> bool:
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
try:
|
|
||||||
self.camera = gp.Camera() # type: ignore
|
self.camera = gp.Camera() # type: ignore
|
||||||
|
|
||||||
|
try:
|
||||||
|
if index:
|
||||||
|
port_info_list = gp.PortInfoList()
|
||||||
|
port_info_list.load()
|
||||||
|
|
||||||
|
port_address = self.camera_list[index]["port"]
|
||||||
|
port_index = port_info_list.lookup_path(port_address)
|
||||||
|
|
||||||
|
self.camera.set_port_info(port_info_list[port_index])
|
||||||
|
|
||||||
self.camera.init()
|
self.camera.init()
|
||||||
config = self.camera.get_config()
|
config = self.camera.get_config()
|
||||||
self.read_config(config)
|
self.read_config(config)
|
||||||
|
|||||||
@@ -1,11 +1,10 @@
|
|||||||
import cv2
|
import cv2
|
||||||
|
from cv2_enumerate_cameras import enumerate_cameras
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
from .base_camera import BaseCamera
|
from .base_camera import BaseCamera
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class CvCamera(BaseCamera):
|
class CvCamera(BaseCamera):
|
||||||
"""Kamera oparta na cv2.VideoCapture"""
|
"""Kamera oparta na cv2.VideoCapture"""
|
||||||
|
|
||||||
@@ -18,18 +17,33 @@ class CvCamera(BaseCamera):
|
|||||||
5: {"name": "saturation", "cv_prop": cv2.CAP_PROP_SATURATION, "default": 0.5},
|
5: {"name": "saturation", "cv_prop": cv2.CAP_PROP_SATURATION, "default": 0.5},
|
||||||
}
|
}
|
||||||
|
|
||||||
def __init__(self, device_index: int = 0) -> None:
|
def __init__(self) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
self.device_index = device_index
|
|
||||||
self.cap = None
|
self.cap = None
|
||||||
self.configs: List[dict] = []
|
self.configs: List[dict] = []
|
||||||
|
self.camera_list = []
|
||||||
|
self.camera_index = 0
|
||||||
|
|
||||||
def connect(self) -> bool:
|
def detect(self) -> list:
|
||||||
|
self.camera_list.clear()
|
||||||
|
self.camera_list = enumerate_cameras(cv2.CAP_ANY)
|
||||||
|
|
||||||
|
result = []
|
||||||
|
for camera in self.camera_list:
|
||||||
|
result.append({"name": camera.name, "port": camera.path})
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def connect(self, index: int | None = None) -> bool:
|
||||||
self.error_msg = None
|
self.error_msg = None
|
||||||
try:
|
try:
|
||||||
self.cap = cv2.VideoCapture(self.device_index)
|
if index:
|
||||||
|
self.camera_index = self.camera_list[index].index
|
||||||
|
|
||||||
|
self.cap = cv2.VideoCapture(self.camera_index)
|
||||||
|
|
||||||
if not self.cap.isOpened():
|
if not self.cap.isOpened():
|
||||||
self.error_msg = f"[CV2] Could not open camera {self.device_index}"
|
self.error_msg = f"[CV2] Could not open camera {self.camera_index}"
|
||||||
return False
|
return False
|
||||||
|
|
||||||
self.configs.clear()
|
self.configs.clear()
|
||||||
@@ -85,7 +99,8 @@ class CvCamera(BaseCamera):
|
|||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self.cap.set(config["cv_prop"], value)
|
self.cap.set(config["cv_prop"], value)
|
||||||
config["value"] = self.cap.get(config["cv_prop"]) # sprawdz co ustawiło
|
config["value"] = self.cap.get(
|
||||||
|
config["cv_prop"]) # sprawdz co ustawiło
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
self.error_msg = f"[CV2] {e}"
|
self.error_msg = f"[CV2] {e}"
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user