From c7773b73a775dfebb151d62bd4cc9b4d8be07b3f Mon Sep 17 00:00:00 2001 From: xy18574557330 <3909057723@qq.com> Date: Tue, 27 Oct 2026 19:22:46 +0800 Subject: [PATCH] fix(PythonClient): use np.frombuffer so byte payloads work on NumPy 2 np.fromstring lost its binary mode in NumPy 2.0, so string_to_uint8_array() and string_to_float_array() raise ValueError on numpy>=2 and any caller decoding simGetImages / depth payloads fails. Parse with np.frombuffer and keep the writable-array behaviour. Also close the PFM file explicitly in write_pfm() and add offline unit tests for the client helpers (no simulator or OpenCV needed). --- PythonClient/README.md | 10 +++ PythonClient/airsim/utils.py | 23 +++---- PythonClient/tests/test_utils.py | 112 +++++++++++++++++++++++++++++++ 3 files changed, 132 insertions(+), 13 deletions(-) create mode 100644 PythonClient/tests/test_utils.py diff --git a/PythonClient/README.md b/PythonClient/README.md index b9e32423d..f8ffdc781 100644 --- a/PythonClient/README.md +++ b/PythonClient/README.md @@ -13,6 +13,16 @@ pip install msgpack-rpc-python Some examples also requires opencv. +## Tests + +The client helpers have offline unit tests that need neither Unreal Engine nor a +running simulator: + +```bash +cd PythonClient +python -m unittest discover -s tests -v +``` + ## More Info More information on AirSim Python APIs can be found at: diff --git a/PythonClient/airsim/utils.py b/PythonClient/airsim/utils.py index 7f866d7d0..bfbdfc22f 100644 --- a/PythonClient/airsim/utils.py +++ b/PythonClient/airsim/utils.py @@ -12,10 +12,12 @@ def string_to_uint8_array(bstr): - return np.fromstring(bstr, np.uint8) - + # np.fromstring(..., dtype) lost its binary mode in NumPy 2.0, so parse the + # raw buffer instead. copy() keeps the previous writable-array behaviour. + return np.frombuffer(bstr, dtype=np.uint8).copy() + def string_to_float_array(bstr): - return np.fromstring(bstr, np.float32) + return np.frombuffer(bstr, dtype=np.float32).copy() def list_to_2d_float_array(flst, width, height): return np.reshape(np.asarray(flst, np.float32), (height, width)) @@ -169,8 +171,6 @@ def read_pfm(file): def write_pfm(file, image, scale=1): """ Write a pfm file """ - file = open(file, 'wb') - color = None if image.dtype.name != 'float32': @@ -183,19 +183,16 @@ def write_pfm(file, image, scale=1): else: raise Exception('Image must have H x W x 3, H x W x 1 or H x W dimensions.') - file.write('PF\n'.encode('utf-8') if color else 'Pf\n'.encode('utf-8')) - temp_str = '%d %d\n' % (image.shape[1], image.shape[0]) - file.write(temp_str.encode('utf-8')) - endian = image.dtype.byteorder if endian == '<' or endian == '=' and sys.byteorder == 'little': scale = -scale - temp_str = '%f\n' % scale - file.write(temp_str.encode('utf-8')) - - image.tofile(file) + with open(file, 'wb') as afile: + afile.write('PF\n'.encode('utf-8') if color else 'Pf\n'.encode('utf-8')) + afile.write(('%d %d\n' % (image.shape[1], image.shape[0])).encode('utf-8')) + afile.write(('%f\n' % scale).encode('utf-8')) + image.tofile(afile) def write_png(filename, image): diff --git a/PythonClient/tests/test_utils.py b/PythonClient/tests/test_utils.py new file mode 100644 index 000000000..aaf08eb0d --- /dev/null +++ b/PythonClient/tests/test_utils.py @@ -0,0 +1,112 @@ +"""Offline unit tests for the AirSim Python client helpers. + +These tests do not need Unreal Engine, a running simulator or OpenCV: they only +exercise the pure NumPy utility functions in ``airsim.utils``. Run them with:: + + cd PythonClient + python -m unittest discover -s tests -v +""" + +import math +import os +import struct +import tempfile +import unittest + +import numpy as np + +from airsim import utils + + +class StringArrayTests(unittest.TestCase): + def test_uint8_array_from_bytes(self): + array = utils.string_to_uint8_array(b'\x00\x7f\xff') + self.assertEqual(array.dtype, np.uint8) + self.assertEqual(array.tolist(), [0, 127, 255]) + # Callers historically received a writable array from np.fromstring. + self.assertTrue(array.flags.writeable) + + def test_float_array_from_bytes(self): + payload = struct.pack('<4f', 1.5, -2.25, 0.0, 42.0) + array = utils.string_to_float_array(payload) + self.assertEqual(array.dtype, np.float32) + np.testing.assert_allclose(array, [1.5, -2.25, 0.0, 42.0], rtol=0, atol=1e-6) + self.assertTrue(array.flags.writeable) + + def test_empty_payload(self): + self.assertEqual(utils.string_to_uint8_array(b'').size, 0) + self.assertEqual(utils.string_to_float_array(b'').size, 0) + + def test_list_to_2d_float_array(self): + array = utils.list_to_2d_float_array([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], 3, 2) + self.assertEqual(array.shape, (2, 3)) + self.assertEqual(array.dtype, np.float32) + np.testing.assert_allclose(array[1], [4.0, 5.0, 6.0]) + + def test_get_pfm_array_uses_response_shape(self): + class Response: + width = 2 + height = 2 + image_data_float = [0.5, 1.5, 2.5, 3.5] + + array = utils.get_pfm_array(Response()) + self.assertEqual(array.shape, (2, 2)) + np.testing.assert_allclose(array, [[0.5, 1.5], [2.5, 3.5]]) + + +class QuaternionTests(unittest.TestCase): + def test_round_trip_keeps_angles(self): + for pitch, roll, yaw in ( + (0.0, 0.0, 0.0), + (0.1, -0.2, 0.3), + (math.radians(20.0), math.radians(-15.0), math.radians(170.0)), + ): + quaternion = utils.to_quaternion(pitch, roll, yaw) + norm = math.sqrt( + quaternion.w_val ** 2 + + quaternion.x_val ** 2 + + quaternion.y_val ** 2 + + quaternion.z_val ** 2 + ) + self.assertAlmostEqual(norm, 1.0, places=9) + out_pitch, out_roll, out_yaw = utils.to_eularian_angles(quaternion) + self.assertAlmostEqual(out_pitch, pitch, places=6) + self.assertAlmostEqual(out_roll, roll, places=6) + self.assertAlmostEqual(math.cos(out_yaw), math.cos(yaw), places=6) + self.assertAlmostEqual(math.sin(out_yaw), math.sin(yaw), places=6) + + +class PfmTests(unittest.TestCase): + def test_grayscale_round_trip(self): + image = np.arange(6, dtype=np.float32).reshape(2, 3) / 4.0 + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'gray.pfm') + utils.write_pfm(path, image) + loaded, scale = utils.read_pfm(path) + self.assertEqual(loaded.shape, image.shape) + np.testing.assert_allclose(loaded, image, rtol=0, atol=1e-6) + self.assertAlmostEqual(scale, 1.0, places=6) + + def test_color_round_trip(self): + image = np.arange(2 * 2 * 3, dtype=np.float32).reshape(2, 2, 3) / 8.0 + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, 'color.pfm') + utils.write_pfm(path, image, scale=2.0) + loaded, scale = utils.read_pfm(path) + self.assertEqual(loaded.shape, image.shape) + np.testing.assert_allclose(loaded, image, rtol=0, atol=1e-6) + self.assertAlmostEqual(scale, 2.0, places=6) + + def test_write_pfm_rejects_bad_dtype(self): + with tempfile.TemporaryDirectory() as tmp: + with self.assertRaises(Exception): + utils.write_pfm(os.path.join(tmp, 'bad.pfm'), np.zeros((2, 2), np.float64)) + + def test_write_pfm_rejects_bad_shape(self): + with tempfile.TemporaryDirectory() as tmp: + with self.assertRaises(Exception): + utils.write_pfm(os.path.join(tmp, 'bad.pfm'), np.zeros((2, 2, 2), np.float32)) + + +if __name__ == '__main__': + unittest.main()