brendandburns / py-k4a

python libraries for the kinect for Azure
46 stars 11 forks source link

lacking image and capture release #4

Open amy-caali opened 4 years ago

amy-caali commented 4 years ago

I noticed this doesn't have image and capture release support, which most online examples show as part of the proper way to grab images from the kinect, e.g, https://docs.microsoft.com/en-us/azure/kinect-dk/retrieve-images. (I also ran into buffer/memory overload issues when I did not use image release.)

The attached files contain the updates needed to port k4a_image_release(img) to this Python module; I also tried to implement k4a_capture_release(capture) but am not sure if it's working. Would love further input!

files.zip

brendandburns commented 4 years ago

@amy-caali

Any chance you can send these as a PR (Pull Request) instead of an attachment?

If you've never sent a pull request before, there are pretty good instructions here:

https://thenewstack.io/getting-legit-with-git-and-github-your-first-pull-request/

Please let me know if you have any questions. I'd love to collaborate further!

Thanks!

amy-caali commented 4 years ago

Done! Here is more detail on the errors I'm still getting too.

Describe the bug When using the device (having transferred from an older Dell PC to a new Lenovo PC), I am getting these errors (that I wasn't getting on the older Dell).

[2019-10-06 18:56:48.692] [error] [t=2352] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\image\image.c (51): k4a_image_t_get_context(). Invalid k4a_image_t 04AC6DE0 [2019-10-06 18:56:48.692] [error] [t=2352] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\image\image.c (333): Invalid argument to image_dec_ref(). image_handle (04AC6DE0) is not a valid handle of type k4a_image_t

To Reproduce When I run

import k4a import faulthandler

faulthandler.enable()

dev = k4a.Device() if k4a.device_open(0, dev): serial = k4a.device_get_serialnum(dev) version = k4a.device_get_version(dev) print("Opened device {} {}\n".format(serial, version))

config = k4a.DeviceConfiguration()
config.color_format = k4a.K4A_IMAGE_FORMAT_COLOR_MJPG
config.color_resolution = k4a.K4A_COLOR_RESOLUTION_2160P
config.depth_mode = k4a.K4A_DEPTH_MODE_NFOV_UNBINNED
config.camera_fps = k4a.K4A_FRAMES_PER_SECOND_30
config.synchronized_images_only = True

if k4a.device_start_cameras(dev, config):
    capture = k4a.Capture()
    res = k4a.device_get_capture(dev, capture, 1000)
    if res == k4a.K4A_WAIT_RESULT_SUCCEEDED:
        img = k4a.capture_get_color_image(capture)
        if img:
            w = k4a.image_get_width_pixels(img)
            h = k4a.image_get_height_pixels(img)
            s = k4a.image_get_stride_bytes(img)
            print('Capture: {} x {} @ {}'.format(h, w, s / w))

            try:
                with open('test.jpg', 'wb') as fp:
                    fp.write(k4a.image_get_buffer(img))
                    #k4a.image_release(img)
                    fp.flush()
                    fp.close()
            except:
                import sys
                print("Unexpected error:", sys.exc_info()[0])
        else:
            print('get image failed!')
    else:
        print('Capture failed!')
else:
    print("Failed to start cameras!")

k4a.device_stop_cameras(dev)
k4a.device_close(dev)
print("Closed")

else: print("Failed to open device")

I usually but not always get

C:\Users\amy\caaliMICRO5\py-k4a>python example.py Opened device 000666392412 (('rgb', (1, 6, 102)), ('depth', (1, 6, 75)), ('audio', (1, 6, 14)), ('depth_sensor', (6109, 7, 0)))

Capture: 2160 x 3840 @ 0.0 Closed [2019-10-06 18:56:48.692] [error] [t=2352] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\image\image.c (51): k4a_image_t_get_context(). Invalid k4a_image_t 04AC6DE0 [2019-10-06 18:56:48.692] [error] [t=2352] D:\a\1\s\extern\Azure-Kinect-Sensor-SDK\src\image\image.c (333): Invalid argument to image_dec_ref(). image_handle (04AC6DE0) is not a valid handle of type k4a_image_t

But the device is opened and the snapshot is taken successfully.

In longer scripts (I am using Python wrapper py-k4a), this is causing the program to crash.

Expected Behavior

About 10% of the time, I instead get:

C:\Users\amy\caaliMICRO5\py-k4a>python example.py Opened device 000666392412 (('rgb', (1, 6, 102)), ('depth', (1, 6, 75)), ('audio', (1, 6, 14)), ('depth_sensor', (6109, 7, 0)))

Capture: 2160 x 3840 @ 0.0 Closed

Also with a successful image capture.

In longer scripts (I am using Python wrapper py-k4a), this is causing the program to crash.

Desktop (please complete the following information):

amy-caali commented 4 years ago

Here was the Microsoft take on this issue:

"Sorry, I am not familiar with Python. My guess is that there is a marshalling issue between the C interface and the Python call.

Try asking the contributors working on the Python interface."