大恒相机Python版Qt界面(二)

 更新:20221206

        感谢评论区以及私信的问题与建议,这里总计了一下遇到的几个问题以及相应的建议(有些没经过验证,仅供参考):

1、来自ID为conan22vip的朋友:

        建议:水星黑白相机成功启动 需要注意的是OpenCV==4.2.0.32 太高版本与PyQT不兼容 还有就是遇到了一个qt.qpa.plugin:Could not load the Qt platform plugin “xcb“问题, sudo apt-get install libxcb-xinerama0之后解决了

2、来自ID为a578344103的朋友:

        建议:现在手头上有个网口连接的黑白相机,连接和打开都没有问题,但是在cv2.cvtColor中报错,原因是相机只是单通道的;注释掉后可以获取画面但是是花屏

        可能的解决方案:尝试一下:cv2.cvtColor(show, cv2.COLOR_GRAY2RGB),还有96~97行改为:showImage = QImage(show.data, show.shape[1], show.shape[0], show.shape[1] * 3,
QImage.Format_RGB888)

3、来自ID为夏至、971的朋友:

        建议:彩色相机显示为黑白

        可能的解决方案:可参考如下(这段代码来自大恒python开发文档,在回调函数中进行对应修改。修改完可能还需要参考建议2):
raw_image = cam.data_stream[0].get_image()
# 从彩色原始图像获取RGB 图像
rgb_image = raw_image.convert("RGB")
if rgb_image is None:
continue
# 从RGB 图像数据创建numpy 数组
numpy_image = rgb_image.get_numpy_array()
if numpy_image is None:
continue

4、来自weixin_58233924的朋友:

        建议:点击updateCameraList能正确显示相机信息,但点击openCamera按钮不出图。

        可能的解决方案:简单版:在DahengCamera.py的OpenCamera函数中有个Index参数。理论上只有一个相机的话该参数应该是1(我调试的时候一直是1)。试着改这个参数为0或者其他看看。进阶版:在updateCameraList函数的变量info处设断点,看看info中都包含了什么参数值。

还有一种可能(我猜测):相机处于硬触发状态,其实已经开启成功了。。。

        建议:怎么实现硬触发

        解决方案:在大恒的《水星二代USB3.0数字相机应用说明书.pdf》中的7.3 IO接口中有详细说明。举例:接橙色与蓝色可实现LINE2的硬触发(这个是我一直用的一种方式)。


    书接上回:大恒相机Python版Qt界面(一)_△ф的博客-CSDN博客_python大恒。目前,基于PyQt5的大恒相机UI界面开发已基本完成,总体界面如下:

大恒相机Python版Qt界面(二)_第1张图片

         该UI界面中,主要包括了相机使用中的几个常用功能:扫描相机型号(UpdateCameraList)、打开相机(OpenCamera)、关闭相机(CloseCamera)、开始图像采集(StartAcq)、停止图像采集(StopAcq)、修改自动曝光模式(ExposureAuto)、曝光时间(ExposureTime)、修改触发模式(TriggerMode)、修改触发源(TriggerSource)、修改自动增益模式(GainAuto)、修改增益数值(GainValue)。

        由于最近事情挺多的(其实是不想写繁琐的说明了),上一章已经简单介绍了一些基本思路,这里就单纯把代码贴出来,供有需要的人作参考。

        项目结构如下:

大恒相机Python版Qt界面(二)_第2张图片

 MainWindow.py是关于相机UI界面的一个文件,网上有很多关于如何将.ui文件转换为.py文件的攻略,不了解的可以搜索以下。若不需要修改UI界面,可直接用这个.py文件。代码如下:

# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'MainWindow.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again.  Do not edit this file unless you know what you are doing.


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(932, 651)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.graphicsView = QtWidgets.QGraphicsView(self.centralwidget)
        self.graphicsView.setGeometry(QtCore.QRect(10, 40, 683, 512))
        self.graphicsView.setDragMode(QtWidgets.QGraphicsView.ScrollHandDrag)
        self.graphicsView.setObjectName("graphicsView")
        self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox.setGeometry(QtCore.QRect(700, 0, 221, 171))
        self.groupBox.setObjectName("groupBox")
        self.comboBox_CameraList = QtWidgets.QComboBox(self.groupBox)
        self.comboBox_CameraList.setGeometry(QtCore.QRect(10, 70, 201, 31))
        self.comboBox_CameraList.setObjectName("comboBox_CameraList")
        self.pushButton_UpdateCameraList = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_UpdateCameraList.setGeometry(QtCore.QRect(10, 30, 201, 31))
        self.pushButton_UpdateCameraList.setObjectName("pushButton_UpdateCameraList")
        self.pushButton_OpenCamera = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_OpenCamera.setGeometry(QtCore.QRect(10, 110, 101, 28))
        self.pushButton_OpenCamera.setObjectName("pushButton_OpenCamera")
        self.pushButton_CloseCamera = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_CloseCamera.setGeometry(QtCore.QRect(110, 110, 101, 28))
        self.pushButton_CloseCamera.setObjectName("pushButton_CloseCamera")
        self.pushButton_StartAcq = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_StartAcq.setGeometry(QtCore.QRect(10, 140, 101, 28))
        self.pushButton_StartAcq.setObjectName("pushButton_StartAcq")
        self.pushButton_StopAcq = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_StopAcq.setGeometry(QtCore.QRect(110, 140, 101, 28))
        self.pushButton_StopAcq.setObjectName("pushButton_StopAcq")
        self.pushButton_ZoomIn = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_ZoomIn.setGeometry(QtCore.QRect(510, 10, 92, 28))
        self.pushButton_ZoomIn.setObjectName("pushButton_ZoomIn")
        self.pushButton_ZoomOut = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton_ZoomOut.setGeometry(QtCore.QRect(600, 10, 92, 28))
        self.pushButton_ZoomOut.setObjectName("pushButton_ZoomOut")
        self.groupBox_2 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_2.setGeometry(QtCore.QRect(700, 170, 221, 111))
        self.groupBox_2.setObjectName("groupBox_2")
        self.comboBox_ExposureMode = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_ExposureMode.setGeometry(QtCore.QRect(120, 20, 91, 22))
        self.comboBox_ExposureMode.setObjectName("comboBox_ExposureMode")
        self.label = QtWidgets.QLabel(self.groupBox_2)
        self.label.setGeometry(QtCore.QRect(10, 20, 111, 21))
        self.label.setObjectName("label")
        self.comboBox_ExposureAuto = QtWidgets.QComboBox(self.groupBox_2)
        self.comboBox_ExposureAuto.setGeometry(QtCore.QRect(120, 50, 91, 22))
        self.comboBox_ExposureAuto.setObjectName("comboBox_ExposureAuto")
        self.label_2 = QtWidgets.QLabel(self.groupBox_2)
        self.label_2.setGeometry(QtCore.QRect(10, 50, 111, 21))
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.groupBox_2)
        self.label_3.setGeometry(QtCore.QRect(10, 80, 111, 21))
        self.label_3.setObjectName("label_3")
        self.doubleSpinBox_ExposureTime = QtWidgets.QDoubleSpinBox(self.groupBox_2)
        self.doubleSpinBox_ExposureTime.setGeometry(QtCore.QRect(120, 80, 91, 22))
        self.doubleSpinBox_ExposureTime.setMaximum(99999999.99)
        self.doubleSpinBox_ExposureTime.setObjectName("doubleSpinBox_ExposureTime")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(10, 560, 72, 15))
        self.label_4.setObjectName("label_4")
        self.label_AcqNum = QtWidgets.QLabel(self.centralwidget)
        self.label_AcqNum.setGeometry(QtCore.QRect(80, 560, 72, 15))
        self.label_AcqNum.setObjectName("label_AcqNum")
        self.label_5 = QtWidgets.QLabel(self.centralwidget)
        self.label_5.setGeometry(QtCore.QRect(180, 560, 41, 16))
        self.label_5.setObjectName("label_5")
        self.label_FPS = QtWidgets.QLabel(self.centralwidget)
        self.label_FPS.setGeometry(QtCore.QRect(220, 560, 41, 16))
        self.label_FPS.setObjectName("label_FPS")
        self.groupBox_3 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_3.setGeometry(QtCore.QRect(700, 280, 221, 131))
        self.groupBox_3.setObjectName("groupBox_3")
        self.label_6 = QtWidgets.QLabel(self.groupBox_3)
        self.label_6.setGeometry(QtCore.QRect(10, 20, 111, 21))
        self.label_6.setObjectName("label_6")
        self.comboBox_TriggerMode = QtWidgets.QComboBox(self.groupBox_3)
        self.comboBox_TriggerMode.setGeometry(QtCore.QRect(120, 20, 91, 22))
        self.comboBox_TriggerMode.setObjectName("comboBox_TriggerMode")
        self.comboBox_TriggerSource = QtWidgets.QComboBox(self.groupBox_3)
        self.comboBox_TriggerSource.setGeometry(QtCore.QRect(120, 50, 91, 22))
        self.comboBox_TriggerSource.setObjectName("comboBox_TriggerSource")
        self.label_7 = QtWidgets.QLabel(self.groupBox_3)
        self.label_7.setGeometry(QtCore.QRect(10, 50, 111, 21))
        self.label_7.setObjectName("label_7")
        self.pushButton_SendSoftwareCommand = QtWidgets.QPushButton(self.groupBox_3)
        self.pushButton_SendSoftwareCommand.setGeometry(QtCore.QRect(10, 80, 201, 41))
        self.pushButton_SendSoftwareCommand.setObjectName("pushButton_SendSoftwareCommand")
        self.groupBox_4 = QtWidgets.QGroupBox(self.centralwidget)
        self.groupBox_4.setGeometry(QtCore.QRect(700, 410, 221, 81))
        self.groupBox_4.setObjectName("groupBox_4")
        self.label_8 = QtWidgets.QLabel(self.groupBox_4)
        self.label_8.setGeometry(QtCore.QRect(10, 20, 111, 21))
        self.label_8.setObjectName("label_8")
        self.comboBox_GainAuto = QtWidgets.QComboBox(self.groupBox_4)
        self.comboBox_GainAuto.setGeometry(QtCore.QRect(120, 20, 91, 22))
        self.comboBox_GainAuto.setObjectName("comboBox_GainAuto")
        self.doubleSpinBox_GainValue = QtWidgets.QDoubleSpinBox(self.groupBox_4)
        self.doubleSpinBox_GainValue.setGeometry(QtCore.QRect(120, 50, 91, 22))
        self.doubleSpinBox_GainValue.setMaximum(99999999.99)
        self.doubleSpinBox_GainValue.setObjectName("doubleSpinBox_GainValue")
        self.label_9 = QtWidgets.QLabel(self.groupBox_4)
        self.label_9.setGeometry(QtCore.QRect(10, 50, 111, 21))
        self.label_9.setObjectName("label_9")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(700, 490, 221, 61))
        self.pushButton.setObjectName("pushButton")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 932, 26))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        self.pushButton.clicked.connect(MainWindow.close)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.groupBox.setTitle(_translate("MainWindow", "CameraControl"))
        self.pushButton_UpdateCameraList.setText(_translate("MainWindow", "UpdateCameraList"))
        self.pushButton_OpenCamera.setText(_translate("MainWindow", "OpenCamera"))
        self.pushButton_CloseCamera.setText(_translate("MainWindow", "CloseCamera"))
        self.pushButton_StartAcq.setText(_translate("MainWindow", "StartAcq"))
        self.pushButton_StopAcq.setText(_translate("MainWindow", "StopAcq"))
        self.pushButton_ZoomIn.setText(_translate("MainWindow", "ZoomIn"))
        self.pushButton_ZoomOut.setText(_translate("MainWindow", "ZoomOut"))
        self.groupBox_2.setTitle(_translate("MainWindow", "Exposure"))
        self.label.setText(_translate("MainWindow", "ExposureMode:"))
        self.label_2.setText(_translate("MainWindow", "ExposureAuto:"))
        self.label_3.setText(_translate("MainWindow", "ExposureTime:"))
        self.label_4.setText(_translate("MainWindow", "Acq Num:"))
        self.label_AcqNum.setText(_translate("MainWindow", "0"))
        self.label_5.setText(_translate("MainWindow", "FPS:"))
        self.label_FPS.setText(_translate("MainWindow", "0"))
        self.groupBox_3.setTitle(_translate("MainWindow", "Trigger"))
        self.label_6.setText(_translate("MainWindow", "TriggerMode:"))
        self.label_7.setText(_translate("MainWindow", "TriggerSource:"))
        self.pushButton_SendSoftwareCommand.setText(_translate("MainWindow", "SendSoftwareCommand"))
        self.groupBox_4.setTitle(_translate("MainWindow", "Gain"))
        self.label_8.setText(_translate("MainWindow", "GainAuto:"))
        self.label_9.setText(_translate("MainWindow", "GainVlue:"))
        self.pushButton.setText(_translate("MainWindow", "Close"))

        DahengCamera.py文件是大恒相机的主要控制文件,代码如下:

import gxipy as gx
import time
import threading

rawImageUpdateList = []
rawImageUpdate = None
num = 0


def capture_callback(raw_image):
    if raw_image.get_status() == gx.GxFrameStatusList.INCOMPLETE:
        print("incomplete frame")
    else:
        global rawImageUpdateList, num, rawImageUpdate
        rawImageUpdate = raw_image.get_numpy_array()
        if len(rawImageUpdateList) == 0:
            rawImageUpdateList.append(rawImageUpdate)
        else:
            rawImageUpdateList.pop()
            rawImageUpdateList.append(rawImageUpdate)
        num += 1


class DahengCamera:
    def __init__(self):
        self.cam = None             #   相机对象
        self.dev_num = None
        self.dev_info_list = None
        self.device_manager = gx.DeviceManager()
        self.AcquisitionThread = None
        self.AcquisitionThreadNeedBeStop = False
        self.IsCameraOpened = False
        self.IsCameraStartAcq = False

    def UpdateCameraList(self):
        self.dev_num, self.dev_info_list = self.device_manager.update_device_list()
        if self.dev_num == 0:
            return False, '0'
        else:
            CameraNameList = []
            for info in self.dev_info_list:
                name = info['model_name']
                CameraNameList.append(name)
            return True, CameraNameList

    def OpenCamera(self, Index):
        if self.dev_num == 0:
            return False
        elif self.IsCameraOpened:
            return True
        else:
            self.cam = self.device_manager.open_device_by_index(Index)

        self.AcquisitionThread = threading.Thread(target=self.AcquisitionThreadFunc_CallBack, args=(), daemon=True)
        self.AcquisitionThread.start()
        self.IsCameraOpened = True
        self.AcquisitionThreadNeedBeStop = False

        return True

    def AcquisitionThreadFunc_CallBack(self):
        self.cam.data_stream[0].register_capture_callback(capture_callback)

        while not self.AcquisitionThreadNeedBeStop:
            time.sleep(1)

    def CloseCamera(self, Index):
        if not self.IsCameraOpened:
            return

        self.AcquisitionThreadNeedBeStop = True
        self.StopAcquisition()
        time.sleep(1)
        self.cam.data_stream[0].unregister_capture_callback()
        self.cam.close_device()

        self.IsCameraOpened = False

    def StartAcquisition(self):
        if self.IsCameraOpened and not self.IsCameraStartAcq:
            self.cam.stream_on()
            self.IsCameraStartAcq = True
        else:
            return

    def StopAcquisition(self):
        if self.IsCameraOpened and self.IsCameraStartAcq:
            self.cam.stream_off()
            self.IsCameraStartAcq = False
        else:
            return

    def GetFPS(self):
        return self.cam.CurrentAcquisitionFrameRate.get()

    def GetExposureModeRange(self):
        return self.cam.ExposureMode.get_range()

    def GetExposureMode(self):
        return self.cam.ExposureMode.get()

    def GetExposureAutoRange(self):
        return self.cam.ExposureAuto.get_range()

    def GetExposureAuto(self):
        return self.cam.ExposureAuto.get()

    def SetExposureAuto(self, ExposureAuto):
        self.cam.ExposureAuto.set(eval('gx.GxAutoEntry.' + ExposureAuto.upper()))
        # self.cam.ExposureAuto.set(Index)

    def GetExposureTime(self):
        return self.cam.ExposureTime.get()

    def SetExposureTime(self, ExposureTime):
        self.cam.ExposureTime.set(ExposureTime)

    def GetTriggerAutoRange(self):
        return self.cam.TriggerMode.get_range()

    def SetTriggerAuto(self, TriggerAuto):
        self.cam.TriggerMode.set(eval('gx.GxSwitchEntry.' + TriggerAuto.upper()))

    def GetTriggerAuto(self):
        return self.cam.TriggerMode.get()

    def GetTriggerSourceRange(self):
        return self.cam.TriggerSource.get_range()

    def SetTriggerSource(self, TriggerSource):
        self.cam.TriggerSource.set(eval('gx.GxTriggerSourceEntry.' + TriggerSource.upper()))

    def GetTriggerSource(self):
        return self.cam.TriggerSource.get()

    def SendSoftWareCommand(self):
        self.cam.TriggerSoftware.send_command()

    def GetGainAutoRange(self):
        return self.cam.GainAuto.get_range()

    def GetGainAuto(self):
        return self.cam.GainAuto.get()

    def GetGainValue(self):
        return self.cam.Gain.get()

    def SetGainAuto(self, GainAuto):
        self.cam.GainAuto.set(eval('gx.GxAutoEntry.' + GainAuto.upper()))

    def SetGainValue(self, GainValue):
        self.cam.Gain.set(GainValue)

        main.py是UI界面的控制文件,也是这个项目的主文件,直接运行这个文件即可。代码如下:

import sys

from PyQt5 import QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from UI.MainWindow import Ui_MainWindow
import cv2
import DahengCamera


class MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(MainWindow, self).__init__()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.Camera = DahengCamera.DahengCamera()
        self.TimerForShowImageInGraphicsView = QTimer()
        self.ImageWidthInGraphicsView = 600
        self.scene = QGraphicsScene()

        self.SlotInit()
        self.UpdateUI()

    """ 初始化槽信号函数"""
    def SlotInit(self):
        self.ui.pushButton_UpdateCameraList.clicked.connect(self.PB_UpdateCameraList_clicked)
        self.ui.pushButton_OpenCamera.clicked.connect(self.PB_OpenCamera_clicked)
        self.ui.pushButton_CloseCamera.clicked.connect(self.PB_CloseCamera_clicked)
        self.ui.pushButton_StartAcq.clicked.connect(self.PB_StartAcq_clicked)
        self.ui.pushButton_StopAcq.clicked.connect(self.PB_StopAcq_clicked)
        self.ui.pushButton_ZoomIn.clicked.connect(self.PB_ZoomIn_clicked)
        self.ui.pushButton_ZoomOut.clicked.connect(self.PB_ZoomOut_clicked)
        self.TimerForShowImageInGraphicsView.timeout.connect(self.SlotForShowImageInGraphicsView)
        self.ui.pushButton_SendSoftwareCommand.clicked.connect(self.SendSoftwareCommand)
        self.SlotConnect()

    def SlotConnect(self):
        self.ui.comboBox_ExposureAuto.currentIndexChanged.connect(self.SetExposureAuto)
        self.ui.doubleSpinBox_ExposureTime.valueChanged.connect(self.SetExposureTime)
        self.ui.comboBox_TriggerMode.currentIndexChanged.connect(self.SetTriggerAuto)
        self.ui.comboBox_TriggerSource.currentIndexChanged.connect(self.SetTriggerSource)
        self.ui.comboBox_GainAuto.currentIndexChanged.connect(self.SetGainAuto)
        self.ui.doubleSpinBox_GainValue.valueChanged.connect(self.SetGainValue)

    def SlotDisConnect(self):
        self.ui.comboBox_ExposureAuto.currentIndexChanged.disconnect(self.SetExposureAuto)
        self.ui.doubleSpinBox_ExposureTime.valueChanged.disconnect(self.SetExposureTime)
        self.ui.comboBox_TriggerMode.currentIndexChanged.disconnect(self.SetTriggerAuto)
        self.ui.comboBox_TriggerSource.currentIndexChanged.disconnect(self.SetTriggerSource)
        self.ui.comboBox_GainAuto.currentIndexChanged.disconnect(self.SetGainAuto)
        self.ui.doubleSpinBox_GainValue.valueChanged.disconnect(self.SetGainValue)

    """ 更新UI界面"""
    def UpdateUI(self):
        self.ui.pushButton_OpenCamera.setDisabled(self.Camera.IsCameraOpened)
        self.ui.pushButton_CloseCamera.setDisabled(not self.Camera.IsCameraOpened)
        self.ui.pushButton_StartAcq.setDisabled(not self.Camera.IsCameraOpened or self.Camera.IsCameraStartAcq)
        self.ui.pushButton_StopAcq.setDisabled(not self.Camera.IsCameraStartAcq)
        self.ui.comboBox_ExposureMode.setDisabled(not self.Camera.IsCameraOpened)
        self.ui.comboBox_ExposureAuto.setDisabled(not self.Camera.IsCameraOpened)
        self.ui.doubleSpinBox_ExposureTime.setDisabled(not self.Camera.IsCameraOpened or
                                                       not self.ui.comboBox_ExposureAuto.currentIndex() == 0)
        self.ui.pushButton_ZoomIn.setDisabled(not self.Camera.IsCameraStartAcq)
        self.ui.pushButton_ZoomOut.setDisabled(not self.Camera.IsCameraStartAcq)
        self.ui.comboBox_TriggerMode.setDisabled(not self.Camera.IsCameraOpened)
        self.ui.comboBox_TriggerSource.setDisabled(not self.Camera.IsCameraOpened or
                                                   self.ui.comboBox_TriggerMode.currentIndex() == 0)
        self.ui.pushButton_SendSoftwareCommand.setDisabled(not self.Camera.IsCameraOpened or
                                                           self.ui.comboBox_TriggerMode.currentIndex() == 0 or
                                                           not self.ui.comboBox_TriggerSource.currentIndex() == 0)
        self.ui.comboBox_GainAuto.setDisabled(not self.Camera.IsCameraOpened)
        self.ui.doubleSpinBox_GainValue.setDisabled(not self.Camera.IsCameraOpened or
                                                    not self.ui.comboBox_GainAuto.currentIndex() == 0)

    """ 更新相机参数的可选项目"""
    def UpdateCameraPara_Range(self):
        self.SlotDisConnect()

        self.ui.comboBox_ExposureMode.clear()
        for Range in self.Camera.GetExposureModeRange():
            self.ui.comboBox_ExposureMode.addItem(Range)

        self.ui.comboBox_ExposureAuto.clear()
        for Range in self.Camera.GetExposureAutoRange():
            self.ui.comboBox_ExposureAuto.addItem(Range)

        self.ui.comboBox_TriggerMode.clear()
        for Range in self.Camera.GetTriggerAutoRange():
            self.ui.comboBox_TriggerMode.addItem(Range)

        self.ui.comboBox_TriggerSource.clear()
        for Range in self.Camera.GetTriggerSourceRange():
            self.ui.comboBox_TriggerSource.addItem(Range)

        self.ui.comboBox_GainAuto.clear()
        for Range in self.Camera.GetGainAutoRange():
            self.ui.comboBox_GainAuto.addItem(Range)

        self.SlotConnect()

    """ 读取相机当前参数值"""
    def GetCameraPara(self):
        self.SlotDisConnect()

        ExposureAuto = self.Camera.GetExposureAuto()
        self.ui.comboBox_ExposureAuto.setCurrentText(ExposureAuto[1])

        ExposureTime = self.Camera.GetExposureTime()
        self.ui.doubleSpinBox_ExposureTime.setValue(ExposureTime)

        TriggerMode = self.Camera.GetTriggerAuto()
        self.ui.comboBox_TriggerMode.setCurrentText(TriggerMode[1])

        TriggerSource = self.Camera.GetTriggerSource()
        self.ui.comboBox_TriggerSource.setCurrentText(TriggerSource[1])

        GainAuto = self.Camera.GetGainAuto()
        self.ui.comboBox_GainAuto.setCurrentText(GainAuto[1])

        GainValue = self.Camera.GetGainValue()
        self.ui.doubleSpinBox_GainValue.setValue(GainValue)

        self.SlotConnect()

    """ 点击UpdateCameraList"""
    def PB_UpdateCameraList_clicked(self):
        status, CameraNameList = self.Camera.UpdateCameraList()
        if status:
            for CameraName in CameraNameList:
                self.ui.comboBox_CameraList.addItem(CameraName)

    """ 点击OpenCamera"""
    def PB_OpenCamera_clicked(self):
        if self.ui.comboBox_CameraList.count() == 0:
            return
        self.Camera.OpenCamera(int(self.ui.comboBox_CameraList.currentIndex()) + 1)
        self.UpdateCameraPara_Range()
        self.GetCameraPara()

        self.UpdateUI()

    """ 点击CloseCamera"""
    def PB_CloseCamera_clicked(self):
        self.Camera.CloseCamera(int(self.ui.comboBox_CameraList.currentIndex()) + 1)
        if self.TimerForShowImageInGraphicsView.isActive():
            self.TimerForShowImageInGraphicsView.stop()
        DahengCamera.num = 0

        self.UpdateUI()

    """ 点击StartAcq"""
    def PB_StartAcq_clicked(self):
        self.Camera.StartAcquisition()
        self.TimerForShowImageInGraphicsView.start(33)

        self.UpdateUI()

    """ 点击StopAcq"""
    def PB_StopAcq_clicked(self):
        self.Camera.StopAcquisition()
        self.TimerForShowImageInGraphicsView.stop()
        self.UpdateUI()

        DahengCamera.num = 0

    """ 点击ZoomIn"""
    def PB_ZoomIn_clicked(self):
        self.ImageWidthInGraphicsView += 100

    """ 点击ZoomOut"""
    def PB_ZoomOut_clicked(self):
        if self.ImageWidthInGraphicsView >= 200:
            self.ImageWidthInGraphicsView -= 100

    """ 图像显示回调函数"""
    def SlotForShowImageInGraphicsView(self):
        if DahengCamera.rawImageUpdate is None:
            return
        else:
            ImageShow = DahengCamera.rawImageUpdateList[0]
            ImageRatio = float(ImageShow.shape[0] / ImageShow.shape[1])
            image_width = self.ImageWidthInGraphicsView
            show = cv2.resize(ImageShow, (image_width, int(image_width * ImageRatio)))
            show = cv2.cvtColor(show, cv2.COLOR_BGR2RGB)  # 视频色彩转换回RGB,这样才是现实的颜色
            showImage = QImage(show.data, show.shape[1], show.shape[0],
                               QImage.Format_RGB888)  # 把读取到的视频数据变成QImage形式
            item = QGraphicsPixmapItem(QPixmap.fromImage(showImage))
            self.scene.clear()
            self.scene.addItem(item)
            self.scene.setSceneRect(0, 0, image_width + 1, image_width * ImageRatio + 1)
            self.ui.graphicsView.setScene(self.scene)
            self.ui.graphicsView.show()

            self.ui.label_AcqNum.setText(str(DahengCamera.num))
            self.ui.label_FPS.setText(str(self.Camera.GetFPS()))

    """ ExposureAuto值改变"""
    def SetExposureAuto(self):
        self.Camera.SetExposureAuto(self.ui.comboBox_ExposureAuto.currentText())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()
        self.UpdateUI()

    """ ExposureTime改变"""
    def SetExposureTime(self):
        self.Camera.SetExposureTime(self.ui.doubleSpinBox_ExposureTime.value())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()

    """ TriggerMode改变"""
    def SetTriggerAuto(self):
        self.Camera.SetTriggerAuto(self.ui.comboBox_TriggerMode.currentText())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()
        self.UpdateUI()

    """ TriggerSource改变"""
    def SetTriggerSource(self):
        self.Camera.SetTriggerSource(self.ui.comboBox_TriggerSource.currentText())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()
        self.UpdateUI()

    """ 发送软触发"""
    def SendSoftwareCommand(self):
        self.Camera.SendSoftWareCommand()

    """ GainAuto改变"""
    def SetGainAuto(self):
        self.Camera.SetGainAuto(self.ui.comboBox_GainAuto.currentText())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()
        self.UpdateUI()

    """ GainValue值改变"""
    def SetGainValue(self):
        self.Camera.SetGainValue(self.ui.doubleSpinBox_GainValue.value())
        self.UpdateCameraPara_Range()
        self.GetCameraPara()


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = MainWindow()
    window.show()

    sys.exit(app.exec_())

你可能感兴趣的:(python,qt,ui)