外部函数名称 : camera_module.py
import cv2 class CameraModule: def __init__(self): self.camera = None self.camera_index = 0 def get_camera_list(self): camera_list = [] for i in range(10): # Check up to 10 possible camera devices cap = cv2.VideoCapture(i) if cap.isOpened(): camera_name = f"Camera {i}" camera_list.append(camera_name) cap.release() return camera_list def start_camera(self, index): if self.camera is None: self.camera = cv2.VideoCapture(index) if self.camera.isOpened(): return True return False def stop_camera(self): if self.camera is not None and self.camera.isOpened(): self.camera.release() self.camera = None return True return False
主函数引用包
头文件加入
from camera_module import CameraModule # Import the CameraModule
调用class内的函数
self.stop_button.clicked.connect(self.stop_camera)
# Create an instance of CameraModule self.camera_module = CameraModule()
self.camera_module.stop_camera() # Ensure the camera is stopped before changing self.camera_module.camera_index = index