0

I have come across a python wrapper for the EDSDK library. It exposed an API with multiple different functions that can easily be called from my own code. The provided example file works without an issue taking a picture and saving it as a file in my project folder.

To speed this process up instead of saving and loading an image from disk, i want to save the image into a python compatible data structure. There are functions that supposedly load an image from the camera into a buffer on my system, and i can go from there.

Sadly all my attempts have ended in my camera or code crashing.

Here is a section of my code:

edsdk.InitializeSDK()
cam_list = edsdk.GetCameraList()
cam = edsdk.GetChildAtIndex(cam_list, 0) # select first cam
edsdk.OpenSession(cam)

# set callback property on cam  
edsdk.SetObjectEventHandler(cam, ObjectEvent.All, callback_object)

# save to host
edsdk.SetPropertyData(cam, PropID.SaveTo, 0, SaveTo.Host)

edsdk.SendCommand(cam, CameraCommand.TakePicture, 0)

# wait for process to finish
time.sleep(4)
if os.name == "nt":
    pythoncom.PumpWaitingMessages()
edsdk.TerminateSDK()

With the callback object

def callback_object(event: ObjectEvent, object_handle: EdsObject) -> int:

   dir_item_info = edsdk.GetDirectoryItemInfo(object_handle)

   # create new Stream
   out = edsdk.CreateMemoryStream(dir_item_info["size"])

   # download to stream      
   edsdk.Download(object_handle, dir_item_info["size"], out)
   edsdk.DownloadComplete(object_handle)

Accessing this stream from outside the callback object, or defining a new stream led to either the EDSDK crashing or my camera not responding.

Is there a way to get this idea to work, or will i just have to settle on loading from disk?

1 Answer 1

0

Your question is two months old, so I'm not sure if it is still relevant. I've ran into the same question, and this is what is working for me:

Above your 'TakePicture' command you can declare a bytes object and use the SDK to create a memorystream that uses this bytes object:

image_data = bytes(6000*4000*3) #TODO: determine proper size
out_stream = edsdk.CreateMemoryStreamFromPointer(image_data)
im = np.zeros((4000, 6000, 3), dtype=np.uint8) # this is where your image will end up

And then for the callback:

def callback_object(event: ObjectEvent, object_handle: EdsObject) -> int:
    dir_item_info = edsdk.GetDirectoryItemInfo(object_handle)

    edsdk.Download(object_handle, dir_item_info["size"], out_stream)
    edsdk.DownloadComplete(object_handle)

    return 0

Now, when you want to read the image somewhere else in your code, for instance in an opencv window:

 im = cv2.imdecode(np.frombuffer(image_data, np.uint8), cv2.IMREAD_ANYCOLOR)
 cv2.imshow('image', im)

This method also works for the EVF image.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.