我正在尝试创建一个 GUI 应用程序,该应用程序使用 Spinnaker SDK 从 GigE Vision 相机(FLIR Blackfly)获取视频源并将其显示在屏幕上。这就是我到目前为止所拥有的。它一直有效,直到我尝试拖动或调整窗口大小,此时它崩溃而没有错误或调试消息。帮助解决这个问题将不胜感激。谢谢!
import PySpin
import cv2
import sys
from PyQt5.QtWidgets import QWidget, QLabel, QApplication
from PyQt5.QtCore import QThread, Qt, pyqtSignal, pyqtSlot
from PyQt5.QtGui import QImage, QPixmap
class Thread(QThread):
changePixmap = pyqtSignal(QImage)
def init_blackfly(self, cam_list, serial, ROI_y, ROI_x):
cam = cam_list.GetBySerial(serial)
#initializing the camera to make everything work
cam.Init()
#get sensor dimensions
height = cam.HeightMax()
width = cam.WidthMax()
#calculate and apply ROI
vertical_offset = round( (height - ROI_y) / 2)
horizontal_offset = round( (width - ROI_x) / 2)
cam.Height.SetValue(ROI_y)
cam.Width.SetValue(ROI_x)
cam.OffsetY.SetValue(vertical_offset)
cam.OffsetX.SetValue(horizontal_offset)
#set the frame rate
cam.AcquisitionFrameRateEnable.SetValue(True)
cam.AcquisitionFrameRate.SetValue(60)
#these parameters give inaccurate color, but increase dynamic range
#start by exposure-compensating the image way down to capture the bright parts of the image
cam.AutoExposureEVCompensation.SetValue(-2.0)
#compensate for the darker image by bumping the gamma
cam.GammaEnable.SetValue(True)
cam.Gamma.SetValue(0.5)
#Enabe the image processor and set it to increase the saturation. The previous two steps reduce saturation
cam.IspEnable.SetValue(True)
cam.SaturationEnable.SetValue(True)
cam.Saturation.SetValue(2)
#reduce latency by changing the way buffers are handled.
cam.TLStream.StreamBufferHandlingMode.SetValue(PySpin.StreamBufferHandlingMode_NewestOnly)
#grab new frames until stopped
cam.AcquisitionMode.SetValue(PySpin.AcquisitionMode_Continuous)
cam.BeginAcquisition()
return cam
def run(self):
system = PySpin.System.GetInstance() #get the camera interface
cam_list = system.GetCameras() #get the list of cameras
cam = self.init_blackfly(cam_list, '13371337', 768, 1024)
while True:
frame_raw = cam.GetNextImage() #image directly from camera
frame_col = frame_raw.Convert(PySpin.PixelFormat_BGR8, PySpin.HQ_LINEAR) #debayer the image
frame_QT = QImage(frame_col.GetData(), 1024, 768, 3 * 1024, QImage.Format_BGR888) #convert the image into pyqt format
self.changePixmap.emit(frame_QT)
class App(QWidget):
def __init__(self):
super().__init__()
self.title = 'PyQt5 Video'
self.left = 100
self.top = 100
self.width = 640
self.height = 480
self.initUI()
@pyqtSlot(QImage)
def setImage(self, image):
self.label.setPixmap(QPixmap.fromImage(image))
def initUI(self):
self.setWindowTitle(self.title)
self.setGeometry(self.left, self.top, self.width, self.height)
self.resize(960, 540)
# create a label
self.label = QLabel(self)
self.label.move(10, 10)
self.label.resize(1024, 768)
th = Thread(self)
th.changePixmap.connect(self.setImage)
th.start()
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())```