连接Basler摄像头,并在Pyqt5 GUI上显示视频

发布于 2025-01-24 13:56:40 字数 1658 浏览 4 评论 0 原文

我有一个这样的应用程序 1 带有一个显示器以将实时的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摄像机连续模式并在应用程序上显示。

I have an application like this 1 with one display to show real-time basler camera into it . I already figured out how to connect to Basler camera and show video on it but the video is not very smooth.

        #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']))

and then I tried

    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()

Is there another ways to connect to Basler camera continuous mode and show it on application.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冰之心 2025-01-31 13:56:40

您可以使用以下代码


    from pypylon import pylon
import cv2

# conecting to the first available camera
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())

# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly) 
converter = pylon.ImageFormatConverter()

# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)

    if grabResult.GrabSucceeded():
        # Access the image data
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.namedWindow('title', cv2.WINDOW_NORMAL)
        cv2.imshow('title', img)
        k = cv2.waitKey(1)
        if k == 27:
            break
    grabResult.Release()
    
# Releasing the resource    
camera.StopGrabbing()

cv2.destroyAllWindows()

从此github获取代码:

You can use the following code


    from pypylon import pylon
import cv2

# conecting to the first available camera
camera = pylon.InstantCamera(pylon.TlFactory.GetInstance().CreateFirstDevice())

# Grabing Continusely (video) with minimal delay
camera.StartGrabbing(pylon.GrabStrategy_LatestImageOnly) 
converter = pylon.ImageFormatConverter()

# converting to opencv bgr format
converter.OutputPixelFormat = pylon.PixelType_BGR8packed
converter.OutputBitAlignment = pylon.OutputBitAlignment_MsbAligned

while camera.IsGrabbing():
    grabResult = camera.RetrieveResult(5000, pylon.TimeoutHandling_ThrowException)

    if grabResult.GrabSucceeded():
        # Access the image data
        image = converter.Convert(grabResult)
        img = image.GetArray()
        cv2.namedWindow('title', cv2.WINDOW_NORMAL)
        cv2.imshow('title', img)
        k = cv2.waitKey(1)
        if k == 27:
            break
    grabResult.Release()
    
# Releasing the resource    
camera.StopGrabbing()

cv2.destroyAllWindows()

The code is taken from this github:pypylon/samples/opencv.py

策马西风 2025-01-31 13:56:40

创建标签并将IMG发送到显示图像fucnbtion。您将获得图像。

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)

create a label and send the img to displayImage fucnbtion. you will get the image.

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文