Repository for M.A.I.L system's analysis server.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

mailcalibrate.py 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import pyzed.sl as sl
  2. import numpy as np
  3. import sys
  4. import os
  5. import time
  6. import json
  7. import math
  8. import json
  9. JSON_PATH = '~/sources/configuration.json'
  10. def conf_depth(camobject=sl.Camera(), headCoord=(480,540), tailCoord=(1440,540)):
  11. # Create a Camera object
  12. hc = headCoord
  13. tc = tailCoord
  14. zed = sl.Camera()
  15. # Create a InitParameters object and set configuration parameters
  16. init = sl.InitParameters()
  17. init.set_from_serial_number
  18. init.depth_mode = sl.DEPTH_MODE.PERFORMANCE # Use PERFORMANCE depth mode
  19. init.coordinate_units = sl.UNIT.METER # Use meter units (for depth measurements)
  20. init.camera_resolution = sl.RESOLUTION.HD1080
  21. # Open the camera
  22. err = zed.open(init)
  23. if err != sl.ERROR_CODE.SUCCESS:
  24. exit(1)
  25. # Create and set RuntimeParameters after opening the camera
  26. runtime_parameters = sl.RuntimeParameters()
  27. runtime_parameters.sensing_mode = sl.SENSING_MODE.STANDARD # Use STANDARD sensing mode
  28. # Setting the depth confidence parameters
  29. runtime_parameters.confidence_threshold = 100
  30. runtime_parameters.textureness_confidence_threshold = 100
  31. # Capture 150 images and depth, then stop
  32. i = 0
  33. image = sl.Mat()
  34. depth = sl.Mat()
  35. point_cloud = sl.Mat()
  36. mirror_ref = sl.Transform()
  37. mirror_ref.set_translation(sl.Translation(2.75,4.0,0))
  38. tr_np = mirror_ref.m
  39. hx = 0
  40. tx = 0
  41. distance_head_cliff = 0
  42. distance_tail_cliff = 0
  43. while i < 900:
  44. # A new image is available if grab() returns SUCCESS
  45. if zed.grab(runtime_parameters) == sl.ERROR_CODE.SUCCESS:
  46. # Retrieve left image
  47. zed.retrieve_image(image, sl.VIEW.LEFT)
  48. # Retrieve depth map. Depth is aligned on the left image
  49. zed.retrieve_measure(depth, sl.MEASURE.DEPTH)
  50. # Retrieve colored point cloud. Point cloud is aligned on the left image.
  51. zed.retrieve_measure(point_cloud, sl.MEASURE.XYZRGBA)
  52. # Get and print distance value in mm at the center of the image
  53. # We measure the distance camera - object using Euclidean distance
  54. x = round(image.get_width() / 2)
  55. y = round(image.get_height() / 2)
  56. if hx == 0 :
  57. hx = x
  58. tx = x
  59. err, point_cloud_value_center = point_cloud.get_value(x, y)
  60. distance_center = math.sqrt(point_cloud_value_center[0] * point_cloud_value_center[0] +
  61. point_cloud_value_center[1] * point_cloud_value_center[1] +
  62. point_cloud_value_center[2] * point_cloud_value_center[2])
  63. err, point_cloud_value_head = point_cloud.get_value(hx,y)
  64. distance_head = math.sqrt(point_cloud_value_head[0] * point_cloud_value_head[0] +
  65. point_cloud_value_head[1] * point_cloud_value_head[1] +
  66. point_cloud_value_head[2] * point_cloud_value_head[2])
  67. if distance_head_cliff != 0:
  68. pass
  69. elif distance_head > (0.04 + distance_center):
  70. if np.isnan(prev_distance_head) == True:
  71. pass
  72. else:
  73. distance_head_cliff = prev_distance_head
  74. hx_cliff = hx
  75. if np.isnan(distance_head) == False:
  76. prev_distance_head = distance_head
  77. err, point_cloud_value_tail = point_cloud.get_value(tx,y)
  78. distance_tail = math.sqrt(point_cloud_value_tail[0] * point_cloud_value_tail[0] +
  79. point_cloud_value_tail[1] * point_cloud_value_tail[1] +
  80. point_cloud_value_tail[2] * point_cloud_value_tail[2])
  81. if distance_tail_cliff != 0:
  82. pass
  83. elif distance_tail > (0.04 + distance_center):
  84. if np.isnan(prev_distance_tail) == True:
  85. pass
  86. else:
  87. distance_tail_cliff = prev_distance_tail
  88. tx_cliff = tx - 1
  89. if np.isnan(distance_tail) == False:
  90. prev_distance_tail = distance_tail
  91. point_cloud_np = point_cloud.get_data()
  92. point_cloud_np.dot(tr_np)
  93. hx -=1
  94. tx +=1
  95. if not np.isnan(distance_center) and not np.isinf(distance_center):
  96. print("Distance to Camera at ({}, {}) (image center): {:1.3} m".format(hx, y, distance_head))
  97. print("Distance to Camera at ({}, {}) (image center): {:1.3} m".format(x, y, distance_center))
  98. print("Distance to Camera at ({}, {}) (image center): {:1.3} m".format(tx, y, distance_tail))
  99. # Increment the loop
  100. i = i + 1
  101. else:
  102. print("Can't estimate distance at this position.")
  103. print("Your camera is probably too close to the scene, please move it backwards.\n")
  104. sys.stdout.flush()
  105. # Close the camera
  106. zed.close()
  107. return [distance_center,hx_cliff,distance_head_cliff,tx_cliff,distance_tail_cliff]
  108. def main():
  109. cameras = sl.Camera.get_device_list()
  110. if len(cameras) == 1:
  111. with open (os.path.abspath(os.path.expanduser(JSON_PATH)), "r") as json_file:
  112. conf = json.load(json_file)
  113. d_list = conf_depth(cameras[0],conf["cam1"]["headCoord"], conf["cam1"]["tailCoord"])
  114. conf["cam1"]["d_center"] = d_list[0]
  115. conf["cam1"]["d_headCoord"] = d_list[2]
  116. conf["cam1"]["d_tailCoord"] = d_list[4]
  117. #as maincam, head is left side of image
  118. if conf["mainCam"] == cameras[0][0]:
  119. conf["cam1"]["headCoord"] = [d_list[1], 540]
  120. conf["cam1"]["tailCoord"] = [d_list[3], 540]
  121. else:
  122. conf["cam1"]["headCoord"] = [d_list[3], 540]
  123. conf["cam1"]["tailCoord"] = [d_list[1], 540]
  124. with open (os.path.abspath(os.path.expanduser(JSON_PATH)), "w") as json_file:
  125. json.dump(conf,json_file, indent=4)
  126. elif len(cameras) == 2:
  127. with open (os.path.abspath(os.path.expanduser(JSON_PATH)), "r") as json_file:
  128. conf = json.load(json_file)
  129. d_list = conf_depth(cameras[1],conf["cam2"]["headCoord"], conf["cam2"]["tailCoord"])
  130. conf["cam2"]["d_center"] = d_list[0]
  131. conf["cam2"]["d_headCoord"] = d_list[2]
  132. conf["cam2"]["d_tailCoord"] = d_list[4]
  133. if conf["mainCam"] == cameras[1][0]:
  134. conf["cam2"]["headCoord"] = [d_list[1], 540]
  135. conf["cam2"]["tailCoord"] = [d_list[3], 540]
  136. else:
  137. conf["cam2"]["headCoord"] = [d_list[3], 540]
  138. conf["cam2"]["tailCoord"] = [d_list[1], 540]
  139. with open (os.path.abspath(os.path.expanduser(JSON_PATH)), "w") as json_file:
  140. json.dump(conf,json_file, indent=4)
  141. if __name__ == "__main__":
  142. main()