Closed ghost closed 4 years ago
The discussion linked to below may give you useful insights if you have not seen it already.
def test(): print('tested')
rs.context().set_devices_changed_callback(test())
This is not working for camera connection and disconnection, test() function is only called when at the initialization of set_devices_changed_callback.
There are a couple errors in your python code there. First of all, by not saving the context object, you're allowing it to be destroyed, at which point even if you had registered the callback correctly, it would not longer be listening.
Furthermore, by using set_devices_changed_callback(test())
, you are feeding set_devices_changed_callback()
the results of running the test()
function (in this case None
), not the function itself.
The correct way to handle this is:
ctx = rs.context()
ctx.set_devices_changed_callback(test) # Note test isn't called
I tried in that way but it is still not calling the test function at the time of disconnection or connection
It might be a problem with the function signature?
This is the simple code i wrote to test this callback:
rs_context = rs.context()
def test(): print('dsfds')
rs_context.set_devices_changed_callback(test)
But this connection disconnect callback is not working
Note: I am using pyrealsense2 and For connection and disconnection i mean, when i am connecting camera and disconnecting the camera from USB
Can anyone tell me what is the problem here, why callback is not working at the time of USB connection or disconnection.
Is the problem in the code ?
Please try this code.
import pyrealsense2 as rs2
import time
current_devices = []
def on_devices_changed(info):
global current_devices
devs = info.get_new_devices()
print("on_devices_changed called new_devices:", devs.size())
for dev in devs:
current_devices.append(dev)
for dev in current_devices:
print("current:", dev, " added:", info.was_added(dev), " removed:", info.was_removed(dev))
def main():
ctx = rs2.context()
ctx.set_devices_changed_callback(on_devices_changed)
print("Please connect/disconnect your RealSense camera.")
time.sleep(10)
print("Done.")
if __name__ == '__main__':
main()
@itachi134999 Did you get chance to try? Looking forward to your update. Thanks!
@itachi134999 Could You please update? Thanks!
I think your issue has been solved. If not, please reopen this ticket, and add more details. Thanks.
For the real time, i am calling get all device query in every API calls which is taking around 500ms. Do librealsense have a mechanism to detect the connection and disconnection of the D series camera from USB ?