Skip to content

Commit

Permalink
refactor save image to file into a function & enable/disable with config
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyler-Lentz committed Jun 12, 2024
1 parent 822e62a commit d064b01
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 14 deletions.
1 change: 1 addition & 0 deletions configs/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"_comment": "See CameraConfig struct in datatypes.hpp for detailed explanations",
"type": "lucid",
"save_dir": "/obcpp/images/",
"save_images_to_file": true,
"mock": {
"images_dir": "/obcpp/tests/integration/images/saliency/"
},
Expand Down
1 change: 1 addition & 0 deletions configs/dev-config.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
"_comment": "See CameraConfig struct in datatypes.hpp for detailed explanations",
"type": "mock",
"save_dir": "/workspaces/obcpp/images/",
"save_images_to_file": false,
"mock": {
"images_dir": "/workspaces/obcpp/tests/integration/images/saliency/"
},
Expand Down
7 changes: 7 additions & 0 deletions include/camera/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ struct ImageData {
cv::Mat DATA;
uint64_t TIMESTAMP;
std::optional<ImageTelemetry> TELEMETRY;

/**
* Saves the image to a file
* @param directory directory to save the image in
* @returns true/false if saving was successful
*/
bool saveToFile(std::string directory) const;
};

std::string cvMatToBase64(cv::Mat image);
Expand Down
2 changes: 2 additions & 0 deletions include/utilities/obc_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ struct CameraConfig {
std::string type;
// directory to save images to
std::string save_dir;
// whether or not to save to save_dir
bool save_images_to_file;
struct {
// directory to randomly pick images from
// for the mock camera
Expand Down
19 changes: 19 additions & 0 deletions src/camera/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,22 @@ std::optional<ImageTelemetry> queryMavlinkImageTelemetry(
.roll_deg = roll_deg
};
}

bool ImageData::saveToFile(std::string directory) const {
try {
std::filesystem::path save_dir = directory;
std::filesystem::path img_filepath =
save_dir / (std::to_string(this->TIMESTAMP) + std::string(".jpg"));
std::filesystem::path json_filepath =
save_dir / (std::to_string(this->TIMESTAMP) + std::string(".json"));
saveImageToFile(this->DATA, img_filepath);
if (this->TELEMETRY.has_value()) {
saveImageTelemetryToFile(this->TELEMETRY.value(), json_filepath);
}
} catch (std::exception& e) {
LOG_F(ERROR, "Failed to save image and telemetry to file");
return false;
}

return true;
}
18 changes: 4 additions & 14 deletions src/network/gcs_routes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,27 +237,17 @@ DEF_GCS_HANDLE(Get, camera, capture) {

std::optional<ImageData> image = cam->takePicture(1000ms, state->getMav());

if (state->config.camera.save_images_to_file) {
image->saveToFile(state->config.camera.save_dir);
}

if (!image.has_value()) {
LOG_RESPONSE(ERROR, "Failed to capture image", INTERNAL_SERVER_ERROR);
return;
}

std::optional<ImageTelemetry> telemetry = image->TELEMETRY;

try {
std::filesystem::path save_dir = state->config.camera.save_dir;
std::filesystem::path img_filepath =
save_dir / (std::to_string(image->TIMESTAMP) + std::string(".jpg"));
std::filesystem::path json_filepath =
save_dir / (std::to_string(image->TIMESTAMP) + std::string(".json"));
saveImageToFile(image->DATA, img_filepath);
if (image->TELEMETRY.has_value()) {
saveImageTelemetryToFile(image->TELEMETRY.value(), json_filepath);
}
} catch (std::exception& e) {
LOG_F(ERROR, "Failed to save image and telemetry to file");
}

ManualImage manual_image;
manual_image.set_img_b64(cvMatToBase64(image->DATA));
manual_image.set_timestamp(image->TIMESTAMP);
Expand Down
3 changes: 3 additions & 0 deletions src/ticks/fly_search.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ Tick* FlySearchTick::tick() {
if (this->curr_mission_item != curr_waypoint) {
for (int i = 0; i < this->state->config.pathing.coverage.hover.pictures_per_stop; i++) {
auto photo = this->state->getCamera()->takePicture(500ms, this->state->getMav());
if (state->config.camera.save_images_to_file) {
photo->saveToFile(state->config.camera.save_dir);
}

if (photo.has_value()) {
// Update the last photo time
Expand Down
1 change: 1 addition & 0 deletions src/utilities/obc_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ OBCConfig::OBCConfig(int argc, char* argv[]) {

SET_CONFIG_OPT(camera, type);
SET_CONFIG_OPT(camera, save_dir);
SET_CONFIG_OPT(camera, save_images_to_file);
SET_CONFIG_OPT(camera, mock, images_dir);

SET_CONFIG_OPT(camera, lucid, sensor_shutter_mode);
Expand Down

0 comments on commit d064b01

Please sign in to comment.