Skip to content

Commit

Permalink
simplify python getImageForCamera API
Browse files Browse the repository at this point in the history
add point_cloud helper.
  • Loading branch information
lovettchris committed Jun 9, 2017
1 parent a680c06 commit d051de9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 9 deletions.
11 changes: 6 additions & 5 deletions PythonClient/PythonClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,14 @@ def rotateByYawRate(self, yaw_rate, duration):
return self.client.call('rotateByYawRate', yaw_rate, duration)

# camera control
# getImageForCamera returns compressed png in array of bytes
# image_type uses one of the AirSimImageType members
def setImageTypeForCamera(self, camera_id, image_type):
return self.client.call('setImageTypeForCamera', camera_id, image_type)
def getImageTypeForCamera(self, camera_id):
return self.client.call('getImageTypeForCamera', camera_id)
def getImageForCamera(self, camera_id, image_type):
return self.client.call('getImageForCamera', camera_id, image_type)
# because this method returns std::vector<uint8>, msgpack decides to encode it as a string unfortunately.
str = self.client.call('getImageForCamera', camera_id, image_type)
if (result == "" or result == "\0"):
return None
return np.fromstring(result, np.int8)

# helper method for converting getOrientation to roll/pitch/yaw
# https://en.wikipedia.org/wiki/Conversion_between_quaternions_and_Euler_angles
Expand Down
6 changes: 2 additions & 4 deletions PythonClient/camera.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ def printUsage():

while True:
# because this method returns std::vector<uint8>, msgpack decides to encode it as a string unfortunately.
result = client.getImageForCamera(0, cameraTypeMap[cameraType])
if (result == "\0"):
rawImage = client.getImageForCamera(0, cameraTypeMap[cameraType])
if (rawImage == None):
print("Camera is not returning image, please check airsim for error messages")
sys.exit(0)
else:
rawImage = np.fromstring(result, np.int8)
png = cv2.imdecode(rawImage, cv2.IMREAD_UNCHANGED)

cv2.putText(png,'FPS ' + str(fps),textOrg, fontFace, fontScale,(255,0,255),thickness)
cv2.imshow("Depth", png)

Expand Down
41 changes: 41 additions & 0 deletions PythonClient/point_cloud.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# use open cv to create point cloud from depth image.

from PythonClient import *
import cv2
import time
import sys

outputFile = "cloud.txt"
color = (0,255,0)
rgb = "%d %d %d" % color

def printUsage():
print("Usage: python point_cloud.py [cloud.txt]")

def savePointCloud(image, fileName):
f = open(fileName, "w")
for x in xrange(image.shape[0]):
for y in xrange(image.shape[1]):
x,y,z = image[x,y]
f.write("%f %f %f %s\n" % (x, y, z, rgb))
f.close()

for arg in sys.argv[1:]:
cloud.txt = arg

client = AirSimClient('127.0.0.1')

while True:
rawImage = client.getImageForCamera(0, AirSimImageType.Depth)
if (rawImage == None):
print("Camera is not returning image, please check airsim for error messages")
sys.exit(0)
else:
png = cv2.imdecode(rawImage, cv2.IMREAD_UNCHANGED)
savePointCloud(pnf, outputFile)
print("saved " + outputFile)
sys.exit(0)

key = cv2.waitKey(1) & 0xFF;
if (key == 27 or key == ord('q') or key == ord('x')):
break;

0 comments on commit d051de9

Please sign in to comment.