I am trying to talk to a C++ SDK controlled camera (brand: SVS with framegrabber) and to retrieve images from it to process in my python code. The camera uses a buffer structure. And here's the code I'm currently using to extract the image data.
def acquiring_imaging(self, stream_handle):
"""Main loop for continuously acquiring and processing images."""
while self.acquiring:
buffer_handle, result = self.wait_for_new_buffer(stream_handle, 1000000)
if result == 0: # New buffer received
buffer_info, result = self.get_buffer_info(stream_handle, buffer_handle)
if result != 0: # Failed to retrieve buffer info
continue
self.queue_buffer(stream_handle, buffer_handle)
if buffer_info.pImagePtr:
imagedata = ct.cast(buffer_info.pImagePtr,
ct.POINTER(ct.c_ubyte * buffer_info.iImageSize))
self.image = np.ndarray(buffer=imagedata.contents, dtype=np.uint8,
shape=(buffer_info.iSizeY, buffer_info.iSizeX, 1))[:, :, 0]
print(self.image)
elif result == CameraError.SV_ERROR_TIMEOUT: # Timeout
self.buffer_updated = False
continue
However, the program stop with no error reports when I try to print the self.image. But I was able to print the size of the self.image, and it seems to be all correct. Any help is appreciated!
I have also tried to save the image via a C++ function hard built into the SDK.
if not buffer_info.pImagePtr or buffer_info.iImageSize == 0:
print("ERROR: Invalid Image Pointer or Image Size is 0!")
# Define function signature
self.sv_dll.SVUtilSaveImageToFile.argtypes = [ct.POINTER(SV_BUFFER_INFO), ct.c_char_p, SV_IMAGE_FILE_TYPE]
self.sv_dll.SVUtilSaveImageToFile.restype = ct.c_int
# Convert filename to ctypes-compatible format
print(buffer_info.pImagePtr)
file_name = "C://Users//folder//graph.png"
file_name = file_name.encode("utf-8")
file_name_bytes = bytes(memoryview(file_name))
info_ref = ct.byref(buffer_info)
print(f'{info.pImagePtr}')
self.sv_dll.SVUtilSaveImageToFile(info_ref, file_name_bytes, SV_IMAGE_FILE_TYPE(SV_IMAGE_FILE_TYPE.PNG))
try:
self.sv_dll.SVUtilSaveImageToFile(info_ref, file_name_bytes, SV_IMAGE_FILE_TYPE(SV_IMAGE_FILE_TYPE.PNG))
except OSError:
print(buffer_info.pImagePtr)
However, this always give me an OS error claiming that I'm accessing the wrong memory space. Further, the info.pImagePtr changes after I call the function SVUtilSaveImageToFile. I suspect some sort of memory leak issue but I'm not sure how to fix it.