StudentCV / PyPylon

pylon wrapper for Python
Other
5 stars 3 forks source link

Cannot grab images from 2 cameras at a time #11

Open Anabad opened 6 years ago

Anabad commented 6 years ago

I made a class that allows me to read video streams. When grabbing from a single camera it works perfectly, however when create 2 instances of the class, the first one stops grabbing frames as soon as the second one is instanced, do you have any idea as to why this may be?

import pypylon.pylon as py
import cv2
import numpy as np

class PylonVideoReader:

    def __init__(self, deviceName=None):
        self.deviceName = deviceName
        if not self.deviceName is None:
            tlfactory = py.TlFactory.GetInstance()
            deviceInfoList = tlfactory.EnumerateDevices()
            deviceIndex = None
            for i in range(len(deviceInfoList)):
                if self.deviceName == deviceInfoList[i].GetUserDefinedName():
                    deviceIndex = i
                    break

            if deviceIndex is None:
                print ("Device: {} not found please ensure that it is "
                        "connected".format(self.deviceName))
                exit()
            else:
                #Create new camera
                self.camera = py.InstantCamera(tlfactory.CreateDevice(
                    deviceInfoList[deviceIndex]))
        else:
            #Create new camera
            self.camera = py.InstantCamera(tlfactory.CreateFirstDevice())

        # Open camera
        self.camera.Open()
        # Set max number of frame buffers
        self.camera.MaxNumBuffer = 50
        # Initialize the  image format converter
        self.formatConverter = py.ImageFormatConverter()
        # Set output pixel format to BGR8 for opencv
        self.formatConverter.OutputPixelFormat= py.PixelType_BGR8packed

        # Start grabbing process 
        self.camera.StartGrabbing(py.GrabStrategy_LatestImageOnly)
        # Grab a first image to get its size
        grabResult = self.camera.RetrieveResult(50000)
        # Stop grabbing process
        #self.camera.StopGrabbing()

        # Get dimensions of image
        self.frameWidth = grabResult.GetWidth()
        self.frameHeight = grabResult.GetHeight()

    def get(self, code):
        if code == 3:
            return this.frameWidth
        elif code == 4:
            return this.frameHeight
        else:
            print ("{} is not a known property code".format(code))

    def read(self):
        #try:

        # Start grabing process
        #self.camera.StartGrabbing(py.GrabStrategy_LatestImageOnly)
        # Grab an image
        grabResult = self.camera.RetrieveResult(200)
        # Stop grabing process
        #self.camera.StopGrabbing()
        # Get dimensions of image
        self.frameWidth = grabResult.GetWidth()
        self.frameHeight = grabResult.GetHeight()

        if grabResult.GrabSucceeded():
            # Convert Grab result from YUV422 to BGR8
            pylonImage = self.formatConverter.Convert(grabResult)
            # Convert pylon image to opencv image
            #image = np.frombuffer(bytearray(pylonImage.GetBuffer()), np.uint8)   
            image = np.asarray(bytearray(pylonImage.GetBuffer()), np.uint8)
            image = image.reshape(self.frameHeight, self.frameWidth, 3)

            return (True, image)
        #except :
        return (False, None)

    def release():
        self.camera.StopGrabbing()
        self.camera.Close()

if __name__=="__main__":

    cap = PylonVideoReader("Heatmap")
    cap2 = PylonVideoReader("Pose_Panier")

    cv2.namedWindow("Test", cv2.WINDOW_NORMAL)
    cv2.namedWindow("Test2", cv2.WINDOW_NORMAL)

    ret, image = cap.read()
    ret2, image2 = cap2.read()

    while True:
        if ret:
            cv2.imshow("Test", image)

        if ret2:
            cv2.imshow("Test2", image2)

        if cv2.waitKey(1) % 256 == ord('q'):
            break

        ret, image = cap.read()
        ret2, image2 = cap2.read()