You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

camera.py 3.1 kB

10 months ago
10 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. """TODO: Add docstring."""
  2. import os
  3. import numpy as np
  4. import pyarrow as pa
  5. from dora import Node
  6. from reachy2_sdk import ReachySDK
  7. from reachy2_sdk.media.camera import CameraView
  8. def main():
  9. """TODO: Add docstring."""
  10. robot_ip = os.getenv("ROBOT_IP", "10.42.0.80")
  11. for _ in range(10):
  12. reachy = ReachySDK(robot_ip)
  13. try:
  14. reachy.cameras.teleop.get_frame(view=CameraView.LEFT)
  15. params = reachy.cameras.depth.get_parameters(view=CameraView.DEPTH)
  16. if params is not None:
  17. break
  18. except Exception as e:
  19. print(e)
  20. import time
  21. time.sleep(1)
  22. reachy.cameras.teleop.get_frame(view=CameraView.LEFT)
  23. params = reachy.cameras.depth.get_parameters(view=CameraView.DEPTH)
  24. height, width, _distortion_model, _d, k, _r, _p = params
  25. node = Node()
  26. for event in node:
  27. if event["type"] == "INPUT":
  28. if event["id"] == "tick":
  29. (image_left, _) = reachy.cameras.teleop.get_frame(view=CameraView.LEFT)
  30. if image_left is int:
  31. continue
  32. node.send_output(
  33. "image_left",
  34. pa.array(image_left.ravel()),
  35. metadata={
  36. "encoding": "bgr8",
  37. "width": image_left.shape[1],
  38. "height": image_left.shape[0],
  39. },
  40. )
  41. (image_right, _) = reachy.cameras.teleop.get_frame(
  42. view=CameraView.RIGHT,
  43. )
  44. if image_right is int:
  45. continue
  46. node.send_output(
  47. "image_right",
  48. pa.array(image_right.ravel()),
  49. metadata={
  50. "encoding": "bgr8",
  51. "width": image_right.shape[1],
  52. "height": image_right.shape[0],
  53. },
  54. )
  55. (depth_image, _) = reachy.cameras.depth.get_frame()
  56. node.send_output(
  57. "image_depth",
  58. pa.array(depth_image.ravel()),
  59. metadata={
  60. "encoding": "bgr8",
  61. "width": depth_image.shape[1],
  62. "height": depth_image.shape[0],
  63. },
  64. )
  65. (depth_frame, _) = reachy.cameras.depth.get_depth_frame()
  66. if params is not None and depth_frame is not None:
  67. depth_frame = depth_frame.ravel().astype(np.float64) / 1_000.0
  68. node.send_output(
  69. "depth",
  70. pa.array(depth_frame),
  71. metadata={
  72. "width": width,
  73. "height": height,
  74. "focal": [int(k[0, 0]), int(k[1, 1])],
  75. "resolution": [int(k[0, 2]), int(k[1, 2])],
  76. },
  77. )
  78. if __name__ == "__main__":
  79. main()