Nalyd1002

test camera

Mar 30th, 2023
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. import pybullet as p
  2. import numpy as np
  3. import pybullet_data
  4. import time
  5.  
  6. import numpy as np
  7. from mpl_toolkits import mplot3d
  8. import matplotlib.pyplot as plt
  9.  
  10. plt.style.use('seaborn-poster')
  11.  
  12.  
  13. # Initialize the physics simulation
  14. physicsClient = p.connect(p.GUI)
  15. p.setAdditionalSearchPath(pybullet_data.getDataPath())
  16.  
  17. # Load the objects into the simulation
  18. planeId = p.loadURDF("plane.urdf")
  19. sphereId = p.loadURDF("sphere_small.urdf", [0, 1, 0])
  20. cylinderId = p.loadURDF("sphere_small.urdf", [0.5, 1, 0])
  21. robotId = p.loadURDF("r2d2.urdf", [0.5,0.5,0.5],useFixedBase=True)
  22.  
  23. # Get the positions of the objects
  24. spherePos, sphereOrn = p.getBasePositionAndOrientation(sphereId)
  25. cylinderPos, cylinderOrn = p.getBasePositionAndOrientation(cylinderId)
  26.  
  27. # Calculate the distance between the two objects
  28. distance = np.linalg.norm(np.array(spherePos) - np.array(cylinderPos))
  29.  
  30. print("The distance between the sphere and cylinder is: ", distance)
  31.  
  32.  
  33. while True:
  34. # Get the image from the camera
  35. camera_pos = [0, 1, 2]
  36. viewMatrix = p.computeViewMatrix(
  37. cameraEyePosition=camera_pos,
  38. cameraTargetPosition=[0, 1, 0],
  39. cameraUpVector=[0, 1, 0])
  40.  
  41. # Set the intrinsic parameters of the camera
  42. fov = 60
  43. aspect = 320.0 / 240.0
  44. near = 0.01
  45. far = 100
  46.  
  47. # Get the image from the camera
  48. width, height, rgbImg, depthImg, segImg = p.getCameraImage(width=320, height=240, viewMatrix=viewMatrix,
  49. projectionMatrix=p.computeProjectionMatrixFOV(fov,
  50. aspect,
  51. near, far))
  52. print("1")
  53. print(depthImg)
  54. print(len(depthImg))
  55.  
  56. # Convert the depth image to a point cloud
  57. points = np.zeros((height * width, 3))
  58. for y in range(height):
  59. for x in range(width):
  60. depth = depthImg[y, x]
  61. if depth != 0:
  62. points[y * width + x, 0] = (x - 160) * depth / 160.0
  63. points[y * width + x, 1] = (y - 120) * depth / 120.0
  64. points[y * width + x, 2] = depth
  65. print("2")
  66. print(points)
  67. print(len(points))
  68. x = np.zeros(len(points))
  69. y = np.zeros(len(points))
  70. z = np.zeros(len(points))
  71. for i in range(len(points)):
  72. x[i] = points[i,0]
  73. print(x)
  74. y[i] = points[i,1]
  75. z[i] = points[i,2]
  76. if z[i] > 0.00001:
  77. print([x[i],y[i],z[i]])
  78. print('ok')
  79.  
  80. fig = plt.figure(figsize=(10, 10))
  81. ax = plt.axes(projection='3d')
  82. ax.grid()
  83.  
  84. ax.scatter(x, y, z, c='r', s=20)
  85. ax.set_title('3D Scatter Plot')
  86.  
  87. # Set axes label
  88. ax.set_xlabel('x', labelpad=20)
  89. ax.set_ylabel('y', labelpad=20)
  90. ax.set_zlabel('z', labelpad=20)
  91.  
  92. plt.show()
  93. # Find the points closest to the sphere and cylinder
  94. spherePoint = points[np.argmin(np.linalg.norm(points - np.array(spherePos), axis=1))]
  95. cylinderPoint = points[np.argmin(np.linalg.norm(points - np.array(cylinderPos), axis=1))]
  96.  
  97. # Calculate the distance between the two objects based on the point cloud
  98. distanceFromCamera = np.linalg.norm(np.array(spherePoint)-np.array(cylinderPoint))
  99. print("The distance between the sphere and cylinder based on the camera is: ", distanceFromCamera)
  100.  
  101. p.stepSimulation()
  102. time.sleep(1. / 240.)
Advertisement
Add Comment
Please, Sign In to add comment