下有为,未来可期!

python引用外部函数模块的做法

外部函数名称 : 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


搜索
控制面板
您好,欢迎到访网站!
  查看权限
网站分类
友情链接

Powered By Z-BlogPHP 1.7.2

Copyright kay880.top.Some Rights Reserved.