basler+python+pyqt5界面实时显示相机画面

import time

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import QTimer

from pypylon import pylon, genicam
import numpy
import cv2 as cv
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        self.camera =pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
        self.camera.Open()
        Dialog.setObjectName("Dialog")
        Dialog.resize(500, 700)
        self.label_acA2440_35um = QtWidgets.QLabel(Dialog)
        self.label_acA2440_35um.setGeometry(QtCore.QRect(10, 10, 480, 498))
        self.label_acA2440_35um.setAlignment(QtCore.Qt.AlignCenter)
        self.label_acA2440_35um.setObjectName("label_acA2440_35um")
        self.horizontalSlider_exptime = QtWidgets.QSlider(Dialog)
        self.horizontalSlider_exptime.setGeometry(QtCore.QRect(220, 559, 200, 20))
        self.horizontalSlider_exptime.setMinimum(29)
        self.horizontalSlider_exptime.setMaximum(100000)
        self.horizontalSlider_exptime.setProperty("value", 5000)
        self.horizontalSlider_exptime.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_exptime.setObjectName("horizontalSlider_exptime")
        self.spinBox_exptime = QtWidgets.QSpinBox(Dialog)
        self.spinBox_exptime.setGeometry(QtCore.QRect(140, 560, 70, 20))
        self.spinBox_exptime.setMinimum(29)
        self.spinBox_exptime.setMaximum(100000)
        self.spinBox_exptime.setProperty("value", 5000)
        self.spinBox_exptime.setObjectName("spinBox_exptime")
        self.label_exptime = QtWidgets.QLabel(Dialog)
        self.label_exptime.setGeometry(QtCore.QRect(60, 560, 80, 20))
        self.label_exptime.setObjectName("label_exptime")
        self.horizontalSlider_fps = QtWidgets.QSlider(Dialog)
        self.horizontalSlider_fps.setGeometry(QtCore.QRect(220, 600, 200, 20))
        self.horizontalSlider_fps.setMaximum(40)
        self.horizontalSlider_fps.setProperty("value", 20)
        self.horizontalSlider_fps.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_fps.setObjectName("horizontalSlider_fps")
        self.spinBox_fps = QtWidgets.QSpinBox(Dialog)
        self.spinBox_fps.setGeometry(QtCore.QRect(140, 601, 70, 20))
        self.spinBox_fps.setMaximum(40)
        self.spinBox_fps.setSingleStep(1)
        self.spinBox_fps.setProperty("value", 20)
        self.spinBox_fps.setObjectName("spinBox_fps")
        self.label_fps = QtWidgets.QLabel(Dialog)
        self.label_fps.setGeometry(QtCore.QRect(60, 601, 80, 20))
        self.label_fps.setObjectName("label_fps")
        self.horizontalSlider_gain = QtWidgets.QSlider(Dialog)
        self.horizontalSlider_gain.setGeometry(QtCore.QRect(220, 640, 200, 20))
        self.horizontalSlider_gain.setMaximum(36)
        self.horizontalSlider_gain.setProperty("value", 0)
        self.horizontalSlider_gain.setOrientation(QtCore.Qt.Horizontal)
        self.horizontalSlider_gain.setObjectName("horizontalSlider_gain")
        self.spinBox_gain = QtWidgets.QSpinBox(Dialog)
        self.spinBox_gain.setGeometry(QtCore.QRect(140, 641, 70, 20))
        self.spinBox_gain.setMaximum(36)
        self.spinBox_gain.setProperty("value", 0)
        self.spinBox_gain.setObjectName("spinBox_gain")
        self.label_gain = QtWidgets.QLabel(Dialog)
        self.label_gain.setGeometry(QtCore.QRect(60, 641, 80, 20))
        self.label_gain.setObjectName("label_gain")
        self.retranslateUi(Dialog)                                                          self.spinBox_exptime.valueChanged['int'].connect(self.horizontalSlider_exptime.setValue)   self.horizontalSlider_exptime.valueChanged['int'].connect(self.spinBox_exptime.setValue)
        self.horizontalSlider_fps.valueChanged['int'].connect(self.spinBox_fps.setValue)
        self.spinBox_fps.valueChanged['int'].connect(self.horizontalSlider_fps.setValue)
        self.horizontalSlider_gain.valueChanged['int'].connect(self.spinBox_gain.setValue)
        self.spinBox_gain.valueChanged['int'].connect(self.horizontalSlider_gain.setValue)

        self.horizontalSlider_exptime.valueChanged.connect(self.getvideo)
        self.horizontalSlider_fps.valueChanged.connect(self.getvideo)
        self.horizontalSlider_gain.valueChanged.connect(self.getvideo)
        QtCore.QMetaObject.connectSlotsByName(Dialog)


        self.timer = QTimer()
        self.timer.start(1000)  # 单位ms
        self.timer.timeout.connect(self.getvideo)

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "BaslerGUI"))
        self.label_acA2440_35um.setText(_translate("Dialog", "捕获图像"))
        self.label_exptime.setText(_translate("Dialog", "ExposureTime: "))
        self.label_fps.setText(_translate("Dialog", "FrameRate: "))
        self.label_gain.setText(_translate("Dialog", "Gain: "))

    def setvalue(self):

        self.exptime = self.horizontalSlider_exptime.value()
        self.fps = self.horizontalSlider_fps.value()
        self.gain = self.horizontalSlider_gain.value()

        self.IMAGES_TO_GRAB = 1

        self.camera.MaxNumBuffer = 5

        self.camera.Gain.SetValue(self.gain)
        self.camera.ExposureTime = self.exptime
        self.camera.AcquisitionFrameRateEnable.SetValue(True)
        self.camera.AcquisitionFrameRate = self.fps
        self.resultfps = self.camera.ResultingFrameRate.GetValue()

        self.camera.PixelFormat = "Mono8"

        self.converter = pylon.ImageFormatConverter()
        self.converter.OutputPixelFormat = pylon.PixelType_BGR8packed
        self.converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

        self.camera.StartGrabbingMax(self.IMAGES_TO_GRAB, pylon.GrabStrategy_LatestImageOnly)

    def getvideo(self):
        self.setvalue()
        try:
            while self.camera.IsGrabbing():
                result = self.camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
                if result.GrabSucceeded():
                    print(self.resultfps)
                    print("Mean Gray value:", numpy.mean(result.Array[0:20, 0:20]))
                    img_bgr = self.converter.Convert(result)

                    img_np = img_bgr.GetArray()
                    # print(img_np)
                    img = cv.cvtColor(img_np, cv.COLOR_BGR2RGB)
                    video = QImage(img.data, img.shape[1], img.shape[0], img.shape[1] * 3, QImage.Format_RGB888)
                    self.label_acA2440_35um.setPixmap(QPixmap.fromImage(video))
                    # 自适应label大小
                    self.label_acA2440_35um.setScaledContents(True)

                    result.Release()

                else:
                    print("Error: ", result.GetErrorCode(), result.GetErrorDescription())

        except genicam.GenericException as e:
            # Error handling.
            print("An exception occurred.", e.GetDescription())


if __name__ == '__main__':
    import sys

    app = QtWidgets.QApplication(sys.argv)
    baslergui = QtWidgets.QDialog()
    baslergui.move(10, 10)
    ui = Ui_Dialog()
    ui.setupUi(baslergui)
    baslergui.show()
    sys.exit(app.exec_())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值