opencv 连接Basler相机并在PyQt5 GUI上显示视频

6ojccjat  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(159)

我有一个像1这样的应用程序,它有一个显示屏,可以实时显示Basler相机。我已经知道如何连接Basler相机并在上面显示视频,但视频不是很流畅。

#Connect to a camera
        for i in MainWindow.camera_db.all():
            if True:
                info = None
                for x in pylon.TlFactory.GetInstance().EnumerateDevices():
                    if x.GetSerialNumber() == i['id']:
                        info = x
                        break

                if info is not None:
                    camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateDevice(info))
                    camera.Open()
                    if MainWindow.viewer1 is None:
                        MainWindow.viewer1 = BaslerOpenCVViewer(camera)
                        logging.warning(f'Camera 1 - serial number: {i["id"]}-OK')
                else:
                    logging.warning('Camera with {} serial number not found'.format(i['id']))

然后我试着

def update_frame(self):
        try:
            frame = MainWindow.viewer1.get_image()
            # frame = cv2.imread('test.jpg')

            self.load_display1(frame) # take a frame and show it on MainWindow.display
            return frame
        except Exception as e:
            logging.warning(str(e))

    self.time_get_image = QtCore.QTimer(self, interval=1)
    self.time_get_image.timeout.connect(self.get_image) #call update_frame function every 1ms to get a real-time video from Basler camera but it's not work well 
    self.time_get_image.start()

是否还有其他方法可以连接到Basler相机连续模式并在应用程序上显示。

4c8rllxm

4c8rllxm1#

创建一个标签并将img发送到displayImage函数。您将获得图像。

from pypylon import pylon
import cv2

camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly)
converter = pylon.ImageFormatConverter()
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)
    # if grabResult.GrabSucceded():
    image = converter.Convert(grabResult)
    img = image.GetArray()

    self.displayImage(img)

    cv2.imshow("video", img)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    cv2.destroyAllWindows()
    cv2.waitKey()

def displayImage(self, img):
    qformat = QImage.Format_Indexed8
    if len(img.shape) == 3:
        if (img.shape[2]) == 4:
            qformat = QImage.Format_RGB888
        else:
            qformat = QImage.Format_RGB888
    img = QImage(img, img.shape[1], img.shape[0], qformat)
    img = img.rgbSwapped()
    self.ui.Camera_lbl.setPixmap(QPixmap.fromImage(img))
    self.ui.Camera_lbl.setAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignHCenter)

相关问题