Connect Basler camera and show video on PyQt5 GUI

1.2k Views Asked by At

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.

2

There are 2 best solutions below

1
On

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

0
On

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)