genicam / harvesters

Image Acquisition Library for GenICam-based Machine Vision System
Apache License 2.0
501 stars 86 forks source link

Cannot start 2 cameras simultaneously. #377

Open MasIgor opened 1 year ago

MasIgor commented 1 year ago

Hi all!

I have two cameras, that I would like to get a image of. (below my code) if I start the first, get the image, stop it and then start the second it works, but if I start them both and try to get the images of both, it just hangs and does not continue working. what am I doing wrong?

any help appreciated.

below my not working code.

import cv2
from harvesters.core import Harvester
from genicam.gentl import TimeoutException
import numpy as np
import matplotlib.pyplot as plt

# Harvesters
h = Harvester()
h.add_file('C:\\Program Files\\MATRIX VISION\\mvIMPACT Acquire\\bin\\x64\\mvGenTLProducer.cti')
h.update()
print(len(h.device_info_list))
print(h.device_info_list[0])

print(h.device_info_list[1])

ia1 = h.create(0)
ia2 = h.create(1)

ia1.remote_device.node_map.ExposureTime.value = 3000
ia1.remote_device.node_map.Gain.value = 20
ia1.remote_device.node_map.BalanceRatioSelector.value = "Red"
ia1.remote_device.node_map.BalanceRatio.value = 1.35
ia1.remote_device.node_map.BalanceRatioSelector.value = "Green"
ia1.remote_device.node_map.BalanceRatio.value = 1
ia1.remote_device.node_map.BalanceRatioSelector.value = "Blue"
ia1.remote_device.node_map.BalanceRatio.value = 2.1
ia1.remote_device.node_map.TriggerMode.value = 'Off'
ia1.remote_device.node_map.PixelFormat.value = 'BayerRG8'

ia2.remote_device.node_map.ExposureTime.value = 300
ia2.remote_device.node_map.Gain.value = 150
ia2.remote_device.node_map.BalanceRatioSelector.value = "Red"
ia2.remote_device.node_map.BalanceRatio.value = 1.35
ia2.remote_device.node_map.BalanceRatioSelector.value = "Green"
ia2.remote_device.node_map.BalanceRatio.value = 1
ia2.remote_device.node_map.BalanceRatioSelector.value = "Blue"
ia2.remote_device.node_map.BalanceRatio.value = 2.1
ia2.remote_device.node_map.TriggerMode.value = 'Off'
ia2.remote_device.node_map.PixelFormat.value = 'BayerRG8'

ia1.start(run_as_thread=True)
ia2.start(run_as_thread=True)

################# new test
try:
    buffer1 = ia1.fetch(timeout=3)
    if buffer1 :
        component = buffer1.payload.components[0]
        img1 = np.ndarray(buffer=component.data.copy(), dtype=np.uint8,
                          shape=(component.height, component.width, 1))
        img1 = cv2.cvtColor(img1, cv2.COLOR_BayerBG2BGR)
except TimeoutException as e:
    print("timeout cam 1")

try:
    buffer2 = ia2.fetch(timeout=3)
    if buffer2:
        component = buffer2.payload.components[0]
        img2 = np.ndarray(buffer=component.data.copy(), dtype=np.uint8,
                          shape=(component.height, component.width, 1))
        img2 = cv2.cvtColor(img2, cv2.COLOR_BayerBG2BGR)
except TimeoutException as e:
    print("timeout cam 2")
ia2.stop()
ia1.stop()

cv2.imshow('image', img1)
cv2.waitKey(3000)
cv2.imshow('image', img2)
cv2.waitKey(3000)
cv2.destroyAllWindows()
MathijsNL commented 1 year ago

Most likely a bandwith problem. Starting two cameras takes up a lot of bandwith. Could you try lowering the framerate and increase the reservedbandwith setting to at least 50% and see if that makes a difference?

MasIgor commented 1 year ago

@MathijsNL sorry, have been removed from the project until today. why do you think it is a bandwith problem? on other projects we start 4 cameras.. also its a 2.5 mpx camera. I will try it anyways.. how can I increase the reserved bandwidth?

Thank you for your help!

MathijsNL commented 1 year ago

Hi @tanzerlana,

Let's assume you are using a 1920 x 1200 camera with 8 bit per pixel the camera will consume 2.304.000 bytes per frame. A typical camera of this resolution using GigE will have about 40FPS. 40 x 2.304.000 = 92.160.000 bytes per second.

Gigabit network has a maximum of 125.000.000 bytes. That is why I think you are having a bandwidth problem. If you would set the framerate to 20FPS per camera it might work, assuming it is indeed a bandwidth problem.

# Either decrease FPS
ia1.remote_device.node_map.AcquisitionFrameRate.value = 20
ia2.remote_device.node_map.AcquisitionFrameRate.value = 20

# Or increase BandwidthReserve (or both, up to you)
ia1.remote_device.node_map.BandwidthReserve.value = 60
ia2.remote_device.node_map.BandwidthReserve.value = 60

Please check your bandwidth usage after starting 1 camera. Even if you don't actively use the frames, the harvester will use a lot of bandwidth because it will grab all available frames at the specified framerate.

Let me know if this solves your problem.