首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接Basler摄像机并在PyQt5图形用户界面上显示视频

连接Basler摄像机并在PyQt5图形用户界面上显示视频
EN

Stack Overflow用户
提问于 2022-04-26 08:44:59
回答 2查看 254关注 0票数 0

我有一个应用程序,像这样的1,只有一个显示器,以显示实时巴斯勒相机到它。我已经知道如何连接到Basler相机,并在它上显示视频,但视频不是很顺利。

代码语言:javascript
复制
        #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']))

然后我试着

代码语言:javascript
复制
    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摄像机连续模式并在应用程序上显示它。

EN

回答 2

Stack Overflow用户

发布于 2022-10-12 12:21:13

创建一个标签并将img发送到displayImage函数。你会得到图像的。

代码语言:javascript
复制
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)

票数 0
EN

Stack Overflow用户

发布于 2022-11-23 15:22:18

您可以使用以下代码

代码语言:javascript
复制
    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:尼龙/样品/露天

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72010939

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档