Skip to content

Commit

Permalink
Merge pull request #158 from inverted-ai/drive_with_pedestrian_updates
Browse files Browse the repository at this point in the history
python client tests for drive with pedestrians, cpp client agent attribute bug fixes
  • Loading branch information
rf-ivtdai authored Dec 13, 2023
2 parents 35566f4 + 678cac8 commit 4081563
Show file tree
Hide file tree
Showing 17 changed files with 383 additions and 59 deletions.
2 changes: 2 additions & 0 deletions invertedai_cpp/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ RUN curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-
COPY . /usr/src/myapp
WORKDIR /usr/src/myapp

RUN chmod 755 ./run_all_tests.sh

CMD ["/bin/bash"]
31 changes: 31 additions & 0 deletions invertedai_cpp/examples/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,35 @@ cc_binary(
"@boost//:beast",
"@opencv",
],
)

cc_binary(
name = "initialize_tests",
srcs = ["initialize_tests.cc"],
data = [
"initialize_body.json",
"initialize_sampling_with_types.json",
"initialize_with_states_and_attributes.json",
],
deps = [
"//invertedai:api",
"@boost//:beast",
"@opencv",
],
)

cc_binary(
name = "drive_tests",
srcs = ["drive_tests.cc"],
data = [
"drive_body.json",
"initialize_body.json",
"initialize_sampling_with_types.json",
"initialize_with_states_and_attributes.json",
],
deps = [
"//invertedai:api",
"@boost//:beast",
"@opencv",
],
)
12 changes: 12 additions & 0 deletions invertedai_cpp/examples/drive_body.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,18 @@
7.58
]
],
"agent_attributes": [
[
5.10,
4.65,
1.23
],
[
4.79,
4.58,
1.17
]
],
"recurrent_states": null,
"get_birdview": true,
"get_infractions": true,
Expand Down
73 changes: 73 additions & 0 deletions invertedai_cpp/examples/drive_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

#include "../invertedai/api.h"

using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
using json = nlohmann::json; // from <json.hpp>

int main(int argc, char **argv) {
// Set up drive scenarios from initialize
const char* tests[] =
{
"examples/initialize_body.json",
"examples/initialize_with_states_and_attributes.json",
"examples/initialize_sampling_with_types.json"
};

try {
const int timestep = std::stoi(argv[1]);
const std::string api_key(argv[2]);

net::io_context ioc;
ssl::context ctx(ssl::context::tlsv12_client);
// configure connection setting
invertedai::Session session(ioc, ctx);
session.set_api_key(api_key);
session.connect();
int i = 0;
for (const char *test : tests) {
invertedai::InitializeRequest init_req(invertedai::read_file(test));
invertedai::InitializeResponse init_res = invertedai::initialize(init_req, &session);
auto image = cv::imdecode(init_res.birdview(), cv::IMREAD_COLOR);
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
int frame_width = image.rows;
int frame_height = image.cols;
std::string drive_video_name = "drive_test_" + std::to_string(i) + ".avi";
cv::VideoWriter video(
drive_video_name,
cv::VideoWriter::fourcc('M', 'J', 'P', 'G'),
10,
cv::Size(frame_width, frame_height)
);
i += 1;
invertedai::DriveRequest drive_req(invertedai::read_file("examples/drive_body.json"));
drive_req.set_location(init_req.location());
drive_req.update(init_res);

for (int t = 0; t < timestep; t++) {
// step the simulation by driving the agents
invertedai::DriveResponse drive_res = invertedai::drive(drive_req, &session);
// use opencv to decode and save the bird's eye view image of the
// simulation
auto image = cv::imdecode(drive_res.birdview(), cv::IMREAD_COLOR);
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
video.write(image);
drive_req.update(drive_res);
std::cout << "Remaining iterations: " << timestep - t << std::endl;
}
video.release();
}
} catch (std::exception const &e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
6 changes: 3 additions & 3 deletions invertedai_cpp/examples/initialize_body.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"location": "iai:ubc_roundabout",
"location": "carla:Town03",
"num_agents_to_spawn": 10,
"states_history": null,
"agent_attributes": null,
"traffic_light_state_history": null,
"get_birdview": false,
"get_infractions": false,
"get_birdview": true,
"get_infractions": true,
"random_seed": null,
"location_of_interest": null
}
17 changes: 17 additions & 0 deletions invertedai_cpp/examples/initialize_sampling_with_types.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"location": "carla:Town04",
"num_agents_to_spawn": 10,
"states_history": null,
"agent_attributes": [
["car"],
["pedestrian"],
[],
["pedestrian"],
["pedestrian"]
],
"traffic_light_state_history": null,
"get_birdview": true,
"get_infractions": true,
"random_seed": null,
"location_of_interest": null
}
49 changes: 49 additions & 0 deletions invertedai_cpp/examples/initialize_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <opencv2/highgui.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/imgproc.hpp>

#include "../invertedai/api.h"

using tcp = net::ip::tcp; // from <boost/asio/ip/tcp.hpp>
using json = nlohmann::json; // from <json.hpp>

int main(int argc, char **argv) {
// list of test scenarios to run
const char* tests[] =
{
"examples/initialize_body.json",
"examples/initialize_with_states_and_attributes.json",
"examples/initialize_sampling_with_types.json"
};

try {
const std::string api_key(argv[1]);

net::io_context ioc;
ssl::context ctx(ssl::context::tlsv12_client);
// configure connection setting
invertedai::Session session(ioc, ctx);
session.set_api_key(api_key);
session.connect();
int i = 0;
for (const char *test : tests) {
invertedai::InitializeRequest init_req(invertedai::read_file(test));
invertedai::InitializeResponse init_res = invertedai::initialize(init_req, &session);
auto image = cv::imdecode(init_res.birdview(), cv::IMREAD_COLOR);
cv::cvtColor(image, image, cv::COLOR_BGR2RGB);
std::string img_name = "initialize_test_" + std::to_string(i) + ".png";
cv::imwrite(img_name, image);
i += 1;
}
} catch (std::exception const &e) {
std::cerr << "Error: " << e.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
81 changes: 81 additions & 0 deletions invertedai_cpp/examples/initialize_with_states_and_attributes.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
{
"location": "canada:ubc_roundabout",
"num_agents_to_spawn": 10,
"states_history": [
[
[
-11.38,
25.95,
-1.09,
9.42
],
[
48.3,
13.65,
0.11,
0.95
],
[
-5.81,
-37.0,
0.15,
0.47
],
[
-11.75,
24.38,
-2.29,
0.98
],
[
36.69,
6.91,
-3.07,
6.18
]
]
],
"agent_attributes": [
[
5.15,
2.43,
1.96,
"car"
],
[
1.2,
1.51,
null,
"pedestrian"
],
[
5.11,
2.17,
1.94,
"car"
],
[
1.16,
1.41,
null,
"pedestrian"
],
[
5.18,
2.22,
1.97,
"car"
],
[

],
[
"pedestrian"
]
],
"traffic_light_state_history": null,
"get_birdview": true,
"get_infractions": true,
"random_seed": null,
"location_of_interest": null
}
3 changes: 3 additions & 0 deletions invertedai_cpp/invertedai/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ cc_library(
name = "data_utils",
srcs = ["data_utils.cc"],
hdrs = ["data_utils.h"],
deps = [
"//invertedai/externals:json",
],
)

cc_library(
Expand Down
15 changes: 3 additions & 12 deletions invertedai_cpp/invertedai/blame_request.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "blame_request.h"

#include "externals/json.hpp"
#include <iostream>

using json = nlohmann::json;

Expand Down Expand Up @@ -28,12 +29,7 @@ BlameRequest::BlameRequest(const std::string &body_str) {
}
this->agent_attributes_.clear();
for (const auto &element : this->body_json_["agent_attributes"]) {
AgentAttributes agent_attribute = {
element[0],
element[1],
element[2],
element[3]
};
AgentAttributes agent_attribute(element);
this->agent_attributes_.push_back(agent_attribute);
}
if (this->body_json_["traffic_light_state_history"].is_null()) {
Expand Down Expand Up @@ -85,12 +81,7 @@ void BlameRequest::refresh_body_json_() {
}
this->body_json_["agent_attributes"].clear();
for (const AgentAttributes &agent_attribute : this->agent_attributes_) {
json element = {
agent_attribute.length,
agent_attribute.width,
agent_attribute.rear_axis_offset,
agent_attribute.agent_type
};
json element = agent_attribute.toJson();
this->body_json_["agent_attributes"].push_back(element);
}
if (this->traffic_light_state_history_.has_value()) {
Expand Down
Loading

0 comments on commit 4081563

Please sign in to comment.