Use python to open multiple IMAGINGSOURCE industrial cameras

1. Install the driver and test software (IC Capture)

This article records how to use python to open the IMAGSOURCE industrial camera under the windows system. The DFK 33UX250 camera used in the project is a USB3.0 color industrial camera. This is its official website introduction: https://www.theimagingsource.cn
insert image description here
To open the camera under the window, you need to install the corresponding driver software and install its test software, as shown in the figure below: The
insert image description here
webpage link is here: https://www .theimagingsource.cn/%E6%94%AF%E6%8C%81/%E8%BD%AF%E4%BB%B6%E4%B8%8B%E8%BD%BD-windows/The driver here is
usb33u That’s it, the test program can download the IC capture software, download the webpage link: https://www.theimagingsource.cn/%E4%BA%A7%E5%93%81/%E8%BD%AF%E4%BB%B6 /%E7%BB%88%E7%AB%AF%E7%94%A8%E6%88%B7%E8%BD%AF%E4%BB%B6/After successfully downloading and installing, insert the camera into theinsert image description here
computer For the usb3.0 port, open the IC Capturer software, observe whether the connected device can be found, and open it successfully.

2. Use the python program to open multiple 33ux250 cameras

Not much to say, the official gave a sample program on github, the address: https://github.com/TheImagingSource/IC-Imaging-Control-Samples
It contains sample programs in c#, java and other languages, find it about python An example, as shown in the figure below:
insert image description here
The example shows how to open the device, enumerate the device and so on. It mainly uses the tisgrabber.py file, which calls the underlying link library, and puts the file in the image below in the sample in your file directory to call it.
insert image description here
The following is the code I refer to the program and use after modification to open three cameras at the same time and display their models:


import cv2
import ctypes
import tisgrabber as tis
ic = ctypes.cdll.LoadLibrary("./tisgrabber_x64.dll")
tis.declareFunctions(ic)
class list_device(object):
    def __init__(self):
        ic.IC_InitLibrary(0)
        self.devicecount = ic.IC_GetDeviceCount()
        self.grabbers = []
        self.name=[]
        for i in range(0, self.devicecount):
            print("Device {}".format(tis.D(ic.IC_GetDevice(i))))
            self.uniquename = tis.D(ic.IC_GetUniqueNamefromList(i))
            print("Unique Name : {}".format(self.uniquename))
            self.g = ic.IC_CreateGrabber()
            ic.IC_OpenDevByUniqueName(self.g, tis.T(self.uniquename))
            self.name.append(self.uniquename)
            self.grabbers.append(self.g)
        print(self.name)
        print(self.grabbers)

    def select_device_demo(self,num):

        grabber=self.grabbers[num]
        if (ic.IC_IsDevValid(grabber)):
            ic.IC_SetVideoFormat(grabber, tis.T("RGB32 (1216x1024)"))
            ic.IC_SetFrameRate(grabber, ctypes.c_float(25.0))  # set FrameRate
            ic.IC_StartLive(grabber, 1)
            ic.IC_MsgBox(tis.T("Click OK to stop"), tis.T("Simple Live Video"))
            ic.IC_StopLive(grabber)
        else:
            ic.IC_MsgBox(tis.T("No device opened"), tis.T("Simple Live Video"))
        ic.IC_ReleaseGrabber(grabber)
    def all_device_demo(self):
        for grabber in self.grabbers:
            if (ic.IC_IsDevValid(grabber)):
                ic.IC_SetVideoFormat(grabber, tis.T("RGB32 (1216x1024)"))
                ic.IC_SetFrameRate(grabber, ctypes.c_float(25.0)) # set FrameRate
                ic.IC_StartLive(grabber, 1)
        ic.IC_MsgBox(tis.T("Stop'em all!"), tis.T("Live Video"))

        for grabber in self.grabbers:
            if (ic.IC_IsDevValid(grabber)):
                ic.IC_StopLive(grabber)

        for grabber in self.grabbers:
            if (ic.IC_IsDevValid(grabber)):
                ic.IC_ReleaseGrabber(grabber)

# def eval_clarity(image):
#     return cv2.Laplacian(image, cv2.CV_64F).var()
if __name__ == '__main__':

    device=list_device()
    #device.select_device_demo(2)#指定打开一台相机
    device.all_device_demo()#打开所有连接的相机

   #下面是我之前用opencv去打开相机,可以打开但相机显示图像会有问题,而且可设置的参数也少
   #后来还是使用了官方给的sdk去打开相机,将下面代码删除即可。
    # video_capture = cv2.VideoCapture(0)
    # video_capture.set(3,960)
    # video_capture.set(4,720)
    # video_capture.set(cv2.CAP_PROP_FRAME_WIDTH, 1279)
    # video_capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 1024)
    # video_capture.set(cv2.CAP_PROP_FPS,50)
    # while True:
    #
    #     ret, image=video_capture.read()
    #     if ret is True:
    #         # image = cv2.resize(image,dsize=(2448,2048))
    #         clarity = eval_clarity(image)
    #         image_gray=cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
    #         cv2.putText(image, ("clarity = %1.1f" % (clarity)), (20, 50), cv2.FONT_HERSHEY_SIMPLEX,
    #                     0.5, 255, 1)
    #         cv2.imshow("camera", image)
    #         print(image.shape)
    #         #print(image.size)
    #         #print(image.shape[:2])
    #         if cv2.waitKey(30) & 0xFF==ord('q'):
    #             break
    # video_capture.release()
    # cv2.destroyAllWindows()

Note : To open multiple cameras at the same time, you need to consider whether the usb interface of your computer has such a large bandwidth, and you cannot directly connect a usb3.0 interface to an expansion interface.
This is the official tisgrabber.py program, also placed here:

import ctypes

from enum import Enum


class SinkFormats(Enum):
    Y800 = 0
    RGB24 = 1
    RGB32 = 2
    UYVY = 3
    Y16 = 4


class FRAMEFILTER_PARAM_TYPE(Enum):
    eParamLong = 0
    eParamBoolean = 1
    eParamFloat = 2
    eParamString = 3
    eParamData = 4


ImageFileTypes = {
    
    'BMP': 0, 'JPEG': 1}

IC_SUCCESS = 1
IC_ERROR = 0
IC_NO_HANDLE = -1
IC_NO_DEVICE = -2
IC_NOT_AVAILABLE = -3
IC_NO_PROPERTYSET = -3
IC_DEFAULT_WINDOW_SIZE_SET = -3
IC_NOT_IN_LIVEMODE = -3
IC_PROPERTY_ITEM_NOT_AVAILABLE = -4
IC_PROPERTY_ELEMENT_NOT_AVAILABLE = -5
IC_PROPERTY_ELEMENT_WRONG_INTERFACE = -6
IC_INDEX_OUT_OF_RANGE = -7
IC_WRONG_XML_FORMAT = -1
IC_WRONG_INCOMPATIBLE_XML = -3
IC_NOT_ALL_PROPERTIES_RESTORED = -4
IC_DEVICE_NOT_FOUND = -5
IC_FILE_NOT_FOUND = 35


class HGRABBER(ctypes.Structure):
    '''
    This class is used to handle the pointer to the internal
    Grabber class, which contains the camera. 
    A pointer to this class is used by tisgrabber DLL.
    '''
    _fields_ = [('unused', ctypes.c_int)]


class HCODEC(ctypes.Structure):
    '''
    This class is used to handle the pointer to the internal
    codec class for AVI capture
    A pointer to this class is used by tisgrabber DLL.
    '''
    _fields_ = [('unused', ctypes.c_int)]


class FILTERPARAMETER(ctypes.Structure):
    '''
    This class implements the structure of a frame filter
    parameter used by the HFRAMEFILTER class
    '''
    _fields_ = [
        ('Name', ctypes.c_char*30),
        ('Type', ctypes.c_int)
    ]


class HFRAMEFILTER(ctypes.Structure):
    '''
    This class implements the structure of a frame filter used
    by the tisgrabber.dll.
    '''
    _fields_ = [
        ('pFilter', ctypes.c_void_p),
        ('bHasDialog', ctypes.c_int),
        ('ParameterCount', ctypes.c_int),
        ('Parameters', ctypes.POINTER(FILTERPARAMETER))
    ]


def declareFunctions(ic):
    '''
    Functions returning a HGRABBER Handle must set their restype to POINTER(HGRABBER)
    :param ic: The loaded tisgrabber*.dll
    '''
    ic.IC_ShowDeviceSelectionDialog.restype = ctypes.POINTER(HGRABBER)
    ic.IC_ReleaseGrabber.argtypes = (ctypes.POINTER(ctypes.POINTER(HGRABBER)),)

    ic.IC_LoadDeviceStateFromFile.restype = ctypes.POINTER(HGRABBER)
    ic.IC_CreateGrabber.restype = ctypes.POINTER(HGRABBER)

    ic.IC_GetPropertyValueRange.argtypes = (ctypes.POINTER(HGRABBER),
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.POINTER(ctypes.c_long),
                                ctypes.POINTER(ctypes.c_long), )

    ic.IC_GetPropertyValue.argtypes = (ctypes.POINTER(HGRABBER),
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.POINTER(ctypes.c_long), )


    ic.IC_GetPropertyAbsoluteValue.argtypes = (ctypes.POINTER(HGRABBER),
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.POINTER(ctypes.c_float),)

    ic.IC_GetPropertyAbsoluteValueRange.argtypes = (ctypes.POINTER(HGRABBER),
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.POINTER(ctypes.c_float),
                                ctypes.POINTER(ctypes.c_float),)

    ic.IC_GetPropertySwitch.argtypes = (ctypes.POINTER(HGRABBER),
                                ctypes.c_char_p,
                                ctypes.c_char_p,
                                ctypes.POINTER(ctypes.c_long), )

    ic.IC_GetImageDescription.argtypes = (ctypes.POINTER(HGRABBER),
                                    ctypes.POINTER(ctypes.c_long),
                                    ctypes.POINTER(ctypes.c_long),
                                    ctypes.POINTER(ctypes.c_int),
                                    ctypes.POINTER(ctypes.c_int),)

    ic.IC_GetImagePtr.restype = ctypes.c_void_p

    ic.IC_SetHWnd.argtypes = (ctypes.POINTER(HGRABBER), ctypes.c_int)
    # definition of the frameready callback
    ic.FRAMEREADYCALLBACK = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.POINTER(HGRABBER), ctypes.POINTER(ctypes.c_ubyte), ctypes.c_ulong, ctypes.py_object)
    ic.DEVICELOSTCALLBACK = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.POINTER(HGRABBER), ctypes.py_object)

    ic.IC_SetFrameReadyCallback.argtypes = [ctypes.POINTER(HGRABBER), ic.FRAMEREADYCALLBACK, ctypes.py_object]
    ic.IC_SetCallbacks.argtypes = [ctypes.POINTER(HGRABBER),
                                   ic.FRAMEREADYCALLBACK,
                                   ctypes.py_object,
                                   ic.DEVICELOSTCALLBACK,
                                   ctypes.py_object]

    ic.IC_Codec_Create.restype = ctypes.POINTER(HCODEC)

    ic.ENUMCODECCB = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_char_p, ctypes.py_object)
    ic.IC_enumCodecs.argtypes = (ic.ENUMCODECCB, ctypes.py_object)

    ic.IC_GetDeviceName.restype = ctypes.c_char_p
    ic.IC_GetDevice.restype = ctypes.c_char_p
    ic.IC_GetUniqueNamefromList.restype = ctypes.c_char_p

    ic.IC_CreateFrameFilter.argtypes = (ctypes.c_char_p, ctypes.POINTER(HFRAMEFILTER))


def T(instr):
    ''' Helper function
    Encodes the input string to utf-8
    :param instr: Python string to be converted
    :return: converted string
    '''
    return instr.encode("utf-8")


def D(instr):
    ''' Helper function
    Decodes instr utf-8
    :param instr: Python string to be converted
    :return: converted string
    '''
    return instr.decode('utf-8', 'ignore')


def openDevice(ic):
    ''' Helper functions
    Open a camera. If a file with a device state exists, it will be used.
    If not, the device selection dialog is shown and if a valid devices
    was selected, the device state file is created.
    :return: a HGRABBER
    '''
    try:
        hGrabber = ic.IC_LoadDeviceStateFromFile(None, T("device.xml"))
        if not ic.IC_IsDevValid(hGrabber):
            hGrabber = ic.IC_ShowDeviceSelectionDialog(None)
    except Exception as ex:
        hGrabber = ic.IC_ShowDeviceSelectionDialog(None)

    if(ic.IC_IsDevValid(hGrabber)):
        ic.IC_SaveDeviceStateToFile(hGrabber, T("device.xml"))
    return hGrabber

Guess you like

Origin blog.csdn.net/weixin_42613125/article/details/123822758