Moved ToImage() and added a use case in man_contr

This commit is contained in:
Marc Garcia Puig 2020-04-29 18:28:37 +02:00
parent a55c71ad4f
commit 9f5f4b9a2e
3 changed files with 28 additions and 32 deletions

View File

@ -9,6 +9,7 @@
#include "carla/Debug.h" #include "carla/Debug.h"
#include "carla/sensor/data/Array.h" #include "carla/sensor/data/Array.h"
#include "carla/sensor/data/DVSEvent.h" #include "carla/sensor/data/DVSEvent.h"
#include "carla/sensor/data/Color.h"
#include "carla/sensor/s11n/DVSEventArraySerializer.h" #include "carla/sensor/s11n/DVSEventArraySerializer.h"
namespace carla { namespace carla {
@ -16,7 +17,7 @@ namespace sensor {
namespace data { namespace data {
/// An array of DVS Events in an image structure HxW /// An array of DVS Events in an image structure HxW
class DVSEventArray : public Array<DVSEvent> { class DVSEventArray : public Array<DVSEvent> {
using Super = Array<DVSEvent>; using Super = Array<DVSEvent>;
protected: protected:
@ -51,6 +52,22 @@ namespace data {
auto GetFOVAngle() const { auto GetFOVAngle() const {
return GetHeader().fov_angle; return GetHeader().fov_angle;
} }
std::vector<Color> ToImage() const {
std::vector<Color> img(GetHeight() * GetWidth());
for (const auto &event : *this) {
size_t index = (GetWidth() * event.y) + event.x;
if (event.pol == true) {
// Blue is positive
img[index].b = 255u;
} else {
// Red is negative
img[index].r = 255u;
}
}
return img;
}
}; };
} // namespace data } // namespace data

View File

@ -319,12 +319,6 @@ void export_sensor_data() {
.def(self_ns::str(self_ns::self)) .def(self_ns::str(self_ns::self))
; ;
class_<std::vector<csd::Color> >("ColorVector")
.add_property("raw_data", &GetRawDataAsBuffer< std::vector<csd::Color> >)
.def("__len__", &std::vector<csd::Color>::size)
.def(vector_indexing_suite< std::vector<csd::Color> >())
;
class_<std::vector<std::int64_t>>("IntVector") class_<std::vector<std::int64_t>>("IntVector")
.add_property("raw_data", &GetRawDataAsBuffer< std::vector<std::int64_t> >) .add_property("raw_data", &GetRawDataAsBuffer< std::vector<std::int64_t> >)
.def("__len__", &std::vector<std::int64_t>::size) .def("__len__", &std::vector<std::int64_t>::size)
@ -364,25 +358,7 @@ void export_sensor_data() {
.def("__setitem__", +[](csd::DVSEventArray &self, size_t pos, csd::DVSEvent event) { .def("__setitem__", +[](csd::DVSEventArray &self, size_t pos, csd::DVSEvent event) {
self.at(pos) = event; self.at(pos) = event;
}) })
.def("to_image", +[](const csd::DVSEventArray &self) -> std::vector<csd::Color> { .def("to_image", CALL_RETURNING_LIST(csd::DVSEventArray, ToImage))
std::vector<csd::Color> img (self.GetHeight() * self.GetWidth());
for (size_t i=0; i<self.size(); ++i)
{
const csd::DVSEvent &event = self[i];
size_t index = (self.GetWidth() * event.y) + event.x;
if (event.pol == true)
{
/** Blue is positive **/
img[index].b = 255u;
}
else
{
/** Red is negative **/
img[index].r = 255u;
}
}
return img;
})
.def("to_array", +[](const csd::DVSEventArray &self) -> std::vector<std::vector<std::int64_t>> { .def("to_array", +[](const csd::DVSEventArray &self) -> std::vector<std::vector<std::int64_t>> {
std::vector<std::vector<std::int64_t>> array (self.size()); std::vector<std::vector<std::int64_t>> array (self.size());
for (size_t i=0; i<self.size(); ++i) for (size_t i=0; i<self.size(); ++i)

View File

@ -980,15 +980,18 @@ class CameraManager(object):
lidar_data = lidar_data.astype(np.int32) lidar_data = lidar_data.astype(np.int32)
lidar_data = np.reshape(lidar_data, (-1, 2)) lidar_data = np.reshape(lidar_data, (-1, 2))
lidar_img_size = (self.hud.dim[0], self.hud.dim[1], 3) lidar_img_size = (self.hud.dim[0], self.hud.dim[1], 3)
lidar_img = np.zeros((lidar_img_size), dtype = int) lidar_img = np.zeros((lidar_img_size), dtype=np.uint8)
lidar_img[tuple(lidar_data.T)] = (255, 255, 255) lidar_img[tuple(lidar_data.T)] = (255, 255, 255)
self.surface = pygame.surfarray.make_surface(lidar_img) self.surface = pygame.surfarray.make_surface(lidar_img)
elif self.sensors[self.index][0].startswith('sensor.camera.dvs'): elif self.sensors[self.index][0].startswith('sensor.camera.dvs'):
array = np.frombuffer(image.to_image().raw_data, dtype=np.dtype("uint8")) # Example of converting the raw_data from a carla.DVSEventArray
array = np.reshape(array, (image.height, image.width, 4)) # sensor into a NumPy array and using it as an image
array = array[:, :, :3] dvs_events = np.frombuffer(image.raw_data, dtype=np.dtype([
array = array[:, :, ::-1] ('x', np.uint16), ('y', np.uint16), ('t', np.int64), ('pol', np.bool)]))
self.surface = pygame.surfarray.make_surface(array.swapaxes(0, 1)) dvs_img = np.zeros((image.height, image.width, 3), dtype=np.uint8)
# Blue is positive, red is negative
dvs_img[dvs_events[:]['y'], dvs_events[:]['x'], dvs_events[:]['pol'] * 2] = 255
self.surface = pygame.surfarray.make_surface(dvs_img.swapaxes(0, 1))
else: else:
image.convert(self.sensors[self.index][1]) image.convert(self.sensors[self.index][1])
array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8")) array = np.frombuffer(image.raw_data, dtype=np.dtype("uint8"))