Zum Inhalt

ImageSource

This class describes an abstract image source from which images can be obtained. Concrete implementations must implement the get_image function.

get_image(self)

Abstract function to retrieve an image from the image source.

Returns:

Type Description
ndarray

The image obtained from the image source

Source code in core/cv/ImageSource.py
22
23
24
25
26
27
28
29
@abc.abstractmethod
def get_image(self) -> np.ndarray:
    """
    Abstract function to retrieve an image from the image source.
    Returns:
        The image obtained from the image source
    """
    pass

DirImageSource

A concrete implementation of an image source. In this image source, images are retrieved from a directory in the file system. They are sorted by name and each time get_image is called, the index is incremented.

__init__(self, image_directory) special

Initialization of the DirImageSource. The images in the directory are read, sorted by name and stored in a list.

Parameters:

Name Type Description Default
image_directory str

The directory to look for images in.

required
Source code in core/cv/ImageSource.py
39
40
41
42
43
44
45
46
47
48
49
50
51
def __init__(self, image_directory: str):
    """
    Initialization of the DirImageSource.
    The images in the directory are read, sorted by name and stored in a list.
    Args:
        image_directory: The directory to look for images in.
    """
    self.images = []
    for filename in sorted(os.listdir(image_directory)):
        image = cv2.imread(os.path.join(image_directory, filename))
        if image is not None:
            self.images.append(image)
    self.image_idx = 0

get_image(self)

Returns the lexicographically next image from the specified directory.

Returns:

Type Description
ndarray

The lexicographically next images from the specified directory.

Source code in core/cv/ImageSource.py
53
54
55
56
57
58
59
60
61
def get_image(self) -> np.ndarray:
    """
    Returns the lexicographically next image from the specified directory.
    Returns:
        The lexicographically next images from the specified directory.
    """
    image = self.images[self.image_idx]
    self.image_idx += 1
    return image

RoboDKImageSource

A concrete implementation of an image source. In this image source, images are retrieved from the RoboDK simulation. An artificial camera is used which renders the RoboDK Scene using the specified parameters.

__init__(self, camera_frame) special

Initialization of the RoboDKImageSource. The camera is created and opened using the specified parameters.

Parameters:

Name Type Description Default
camera_frame str

The name of the camera frame.

required
Source code in core/cv/ImageSource.py
79
80
81
82
83
84
85
86
87
88
89
90
def __init__(self, camera_frame: str):
    """
    Initialization of the RoboDKImageSource.
    The camera is created and opened using the specified parameters.
    Args:
        camera_frame: The name of the camera frame.
    """
    self.link = RoboDKInterface.link
    self.camref = self.link.Item(camera_frame, robolink.ITEM_TYPE_FRAME)

    self.link.Cam2D_Close()  # close all open cameras
    self.cam_id = self.link.Cam2D_Add(self.camref, 'FOCAL_LENGHT=6 FOV=16 FAR_LENGHT=1600 SIZE=1024x960')

get_image(self)

Returns the current image from the artificial camera in the RoboDK scene.

Returns:

Type Description
ndarray

The current image form the artificial camera in the RoboDK scene.

Source code in core/cv/ImageSource.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
def get_image(self) -> np.ndarray:
    """
    Returns the current image from the artificial camera in the RoboDK scene.
    Returns:
        The current image form the artificial camera in the RoboDK scene.
    """
    # only writing frames to files is supported, not to RAM --> saving and reading necessary
    self.link.Cam2D_Snapshot(RoboDKImageSource.IMAGE_FILENAME, self.cam_id)
    image = cv2.imread(RoboDKImageSource.IMAGE_FILENAME)
    os.remove(RoboDKImageSource.IMAGE_FILENAME)
    return image

PyUEyeImageSource

A concrete implementation of an image source. In this image source, images are retrieved from the real uEye camera in the robotics lab. The communication is done via the pyueye python package.

__init__(self, camera_id) special

Initialization for PyUEyeImageSource. The connection to the camera is established and parameters, such as the exposure time are set.

Parameters:

Name Type Description Default
camera_id int

The ID of the camera to connect to.

required
Source code in core/cv/ImageSource.py
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
def __init__(self, camera_id: int):
    """
    Initialization for PyUEyeImageSource.
    The connection to the camera is established and parameters, such as the exposure time are set.
    Args:
        camera_id: The ID of the camera to connect to.
    """
    self.ueye = __import__('pyueye').ueye
    self.camera_handle = self.ueye.HIDS(camera_id)
    self.bits_per_pixel = self.ueye.INT(24)
    self.pitch = self.ueye.INT()

    ret = self.ueye.is_InitCamera(self.camera_handle, None)
    if ret != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_InitCamera ERROR")

    cInfo = self.ueye.CAMINFO()
    nRet = self.ueye.is_GetCameraInfo(self.camera_handle, cInfo)
    if nRet != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_GetCameraInfo ERROR")

    self.sensor_info = self.ueye.SENSORINFO()
    ret = self.ueye.is_GetSensorInfo(self.camera_handle, self.sensor_info)
    if ret != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_GetSensorInfo ERROR")

    ret = self.ueye.is_ResetToDefault(self.camera_handle)
    if ret != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_ResetToDefault ERROR")

    # Set display mode to DIB
    ret = self.ueye.is_SetDisplayMode(self.camera_handle, self.ueye.IS_SET_DM_DIB)

    if ret != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_setDisplayMode ERROR")

    # Set the right color mode
    self.color_mode = self.ueye.INT()
    # Set the right color mode
    self.ueye.is_GetColorDepth(self.camera_handle, self.bits_per_pixel, self.color_mode)

    self.bytes_per_pixel = self.bits_per_pixel // 8
    logger.debug("\tm_nColorMode: \t\t", self.color_mode)
    logger.debug("\tnBitsPerPixel: \t\t", self.bits_per_pixel)
    logger.debug("\tbytes_per_pixel: \t\t", self.bytes_per_pixel)
    logger.debug("\n")

    n_pixelClock = self.ueye.UINT(5)
    nRet = self.ueye.is_PixelClock(self.camera_handle, self.ueye.IS_PIXELCLOCK_CMD_SET, n_pixelClock, self.ueye.sizeof(n_pixelClock))
    if nRet != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_PixelClock ERROR")

    exposure_ms = self.ueye.DOUBLE(20)
    nRet = self.ueye.is_Exposure(self.camera_handle, self.ueye.IS_EXPOSURE_CMD_SET_EXPOSURE, exposure_ms, self.ueye.sizeof(exposure_ms))
    if nRet != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_Exposure ERROR")

    # get image size
    image_rect = self.ueye.IS_RECT()
    ret = self.ueye.is_AOI(self.camera_handle, self.ueye.IS_AOI_IMAGE_GET_AOI, image_rect, self.ueye.sizeof(image_rect))
    if ret != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_AOI ERROR")

    self.width = image_rect.s32Width
    self.height = image_rect.s32Height

    # Prints out some information about the camera and the sensor
    logger.debug("Camera model:\t\t", self.sensor_info.strSensorName.decode('utf-8'))
    logger.debug("Camera serial no.:\t", cInfo.SerNo.decode('utf-8'))
    logger.debug("Maximum image width:\t", self.width)
    logger.debug("Maximum image height:\t", self.height)
    logger.debug("\n")

    # Allocates an image memory for an image having its dimensions defined by width and height and its color depth
    # defined by nBitsPerPixel
    self.image_memory = self.ueye.c_mem_p()
    self.MemID = self.ueye.int()
    nRet = self.ueye.is_AllocImageMem(self.camera_handle, self.width, self.height, self.bits_per_pixel,
                                 self.image_memory, self.MemID)
    if nRet != self.ueye.IS_SUCCESS:
        raise RuntimeError("is_AllocImageMem ERROR")
    else:
        # Makes the specified image memory the active memory
        nRet = self.ueye.is_SetImageMem(self.camera_handle, self.image_memory, self.MemID)
        if nRet != self.ueye.IS_SUCCESS:
            raise RuntimeError("is_SetImageMem ERROR")
        else:
            # Set the desired color mode
            nRet = self.ueye.is_SetColorMode(self.camera_handle, self.color_mode)

get_image(self)

Returns the current image of the uEye camera.

Returns:

Type Description
ndarray

The current image of the uEye camera.

Source code in core/cv/ImageSource.py
209
210
211
212
213
214
215
216
217
218
219
220
def get_image(self) -> np.ndarray:
    """
    Returns the current image of the uEye camera.
    Returns:
        The current image of the uEye camera.
    """
    time.sleep(0.5)
    image_data = self.ueye.get_data(self.image_memory, self.width, self.height, self.bits_per_pixel, self.pitch, copy=False).copy()
    time.sleep(0.5)
    frame = np.reshape(image_data, (self.height.value, self.width.value, -1))[:, :, :3]
    frame = cv2.rotate(frame, cv2.ROTATE_180)
    return frame