genicam / harvesters

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

How handle multiple cameras? #387

Closed HyunyoungGoh closed 11 months ago

HyunyoungGoh commented 1 year ago

Describe the Issue I have 4 cameras. 3.1M(2048 x 1536) pixels camera : 2EA 0.4M(720 x 540) pixels camera : 2EA When I connected 0.4M 2 camera, my PyQt program works fine. But with 4 camera, program has latency.

Below is my snippet. Could you give any comments for multi camera handling refers on my snippets?

Sample Code I can show a piece of code that demonstrates the reported phenomenon:

import sys
import time

from PySide6.QtCore import QThread, QRect, Signal
from PySide6.QtGui import QPixmap, QImage
from PySide6.QtWidgets import QWidget, QApplication, QGridLayout, QGraphicsView, QGraphicsScene
from cv2 import cv2
from harvesters.core import Harvester
from numpy import ndarray

class SubThread(QThread):
    def __init__(self, ia=None, gui=None, index=None):
        super().__init__()
        self.ia = ia
        self.ia.start()
        self.gui = gui
        self.index = index

    def run(self):
        ia = self.ia
        while True:
            with ia.fetch() as buffer:
                payload = buffer.payload
                component = payload.components[0]
                width = component.width
                height = component.height
                frame = component.data.reshape(height, width).copy()

            frame = cv2.resize(frame, dsize=(400, 300), interpolation=cv2.INTER_NEAREST)
            self.gui.signal_update_gpv.emit(self.index, frame)

            time.sleep(1/60)

class MyApp(QWidget):
    signal_update_gpv = Signal(int, ndarray)

    def __init__(self):
        super().__init__()

        ### Signal ###
        self.signal_update_gpv.connect(self.update_gpv)

        ### Method ###
        self.initUI()

        self.h = Harvester()
        PRODUCER_PATH = "/home/mvGenTLProducer.cti"
        self.h.add_file(PRODUCER_PATH)
        self.h.update()
        self.thr_list = []
        ## CTR, S1, S2, W3, W4
        # for sn in ['1101006', '1001069', '0300004', '1001068', '1102153']:
        for (index, sn) in enumerate(['1001069', '0300004', '1001068', '1102153']):
        # for (index, sn) in enumerate(['0300004', '1102153']):
            ia = self.h.create(search_key={'serial_number': sn})
            thr = SubThread(ia, self, index)
            thr.start()
            self.thr_list.append(thr)

    def update_gpv(self, index, frame):
        width = frame.shape[1]
        height = frame.shape[0]

        img = QImage(frame, width, height, QImage.Format_Grayscale8)
        pix = QPixmap.fromImage(img)

        scene = QGraphicsScene()
        scene.addPixmap(pix)
        self.gpv_list[index].setScene(scene)

    def initUI(self):
        grid_layout_widget = QWidget(self)
        grid_layout_widget.setGeometry(QRect(20, 20, 1000, 800))

        self.grid_layout = QGridLayout(grid_layout_widget)

        gpv1 = QGraphicsView(grid_layout_widget)
        self.grid_layout.addWidget(gpv1, 0, 0)

        gpv2 = QGraphicsView(grid_layout_widget)
        self.grid_layout.addWidget(gpv2, 0, 1)

        gpv3 = QGraphicsView(grid_layout_widget)
        self.grid_layout.addWidget(gpv3, 1, 0)

        gpv4 = QGraphicsView(grid_layout_widget)
        self.grid_layout.addWidget(gpv4, 1, 1)

        self.gpv_list = [gpv1, gpv2, gpv3, gpv4]

if __name__ == '__main__':
   app = QApplication(sys.argv)
   ex = MyApp()
   ex.show()
   sys.exit(app.exec_())

Configuration

Reproducibility

This phenomenon can be stably reproduced:

If applicable, please provide your observation about the reproducibility.

Actions You Have Taken

Additional context Add any other context about the problem here.