Skip to content

Commit 4b03d05

Browse files
authored
Merge pull request #29 from ichiro-its/feature/create-record-image-service
[Feature] Create Record Image Service
2 parents 1a96543 + 752068f commit 4b03d05

File tree

6 files changed

+133
-4
lines changed

6 files changed

+133
-4
lines changed

CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ add_library(${PROJECT_NAME} SHARED
4040
"src/${PROJECT_NAME}/config/grpc/call_data_get_image.cpp"
4141
"src/${PROJECT_NAME}/config/grpc/call_data_save_capture_setting.cpp"
4242
"src/${PROJECT_NAME}/config/grpc/call_data_set_capture_setting.cpp"
43+
"src/${PROJECT_NAME}/config/grpc/call_data_record_image.cpp"
4344
"src/${PROJECT_NAME}/utility/capture_setting.cpp"
4445
"src/${PROJECT_NAME}/utility/interface.cpp"
4546
"src/${PROJECT_NAME}/node/shisen_cpp_node.cpp"

include/shisen_cpp/camera/node/camera_node.hpp

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ class CameraNode
4343
void update();
4444
void on_mat_captured(cv::Mat mat);
4545
void on_camera_config(int width, int height);
46+
void save_image(cv::Mat mat);
4647
CaptureSetting on_configure_capture_setting(const CaptureSetting & capture_setting);
4748
void configure_capture_setting(const CaptureSetting & capture_setting = CaptureSetting());
4849
void load_configuration(const std::string & path);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright (c) 2024 ICHIRO ITS
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
#ifndef SHISEN_CPP__CONFIG__GRPC__CALL_DATA_RECORD_IMAGE_HPP__
22+
#define SHISEN_CPP__CONFIG__GRPC__CALL_DATA_RECORD_IMAGE_HPP__
23+
24+
#include <shisen_cpp/camera/node/camera_node.hpp>
25+
#include <shisen_cpp/config/grpc/call_data.hpp>
26+
27+
namespace shisen_cpp
28+
{
29+
class CallDataRecordImage
30+
: CallData<shisen_interfaces::proto::Empty, shisen_interfaces::proto::Empty>
31+
{
32+
public:
33+
CallDataRecordImage(
34+
shisen_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
35+
const std::string & path, const std::shared_ptr<camera::CameraNode>& camera_node);
36+
37+
protected:
38+
void AddNextToCompletionQueue() override;
39+
void WaitForRequest() override;
40+
void HandleRequest() override;
41+
std::shared_ptr<camera::CameraNode> camera_node_;
42+
};
43+
} // namespace shisen_cpp
44+
45+
#endif // SHISEN_CPP__CONFIG__GRPC__CALL_DATA_RECORD_IMAGE_HPP__

src/shisen_cpp/camera/node/camera_node.cpp

+30-4
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1919
// THE SOFTWARE.
2020

21-
#include <fstream>
22-
#include <nlohmann/json.hpp>
23-
#include <fstream>
24-
#include <memory>
21+
2522
#include <nlohmann/json.hpp>
23+
#include <opencv2/imgcodecs.hpp>
2624
#include <shisen_cpp/camera/node/camera_node.hpp>
2725
#include <jitsuyo/config.hpp>
2826

27+
#include <chrono>
2928
#include <fstream>
3029
#include <memory>
30+
#include <sstream>
3131
#include <string>
3232

3333
namespace shisen_cpp::camera
@@ -69,6 +69,32 @@ void CameraNode::on_camera_config(int width, int height)
6969
camera_config_publisher->publish(camera_config_provider->get_camera_config());
7070
}
7171

72+
void CameraNode::save_image(cv::Mat mat)
73+
{
74+
if (!std::filesystem::exists("image")) {
75+
if (!std::filesystem::create_directory("image")) {
76+
RCLCPP_ERROR(node->get_logger(), "Error creating `image` directory!");
77+
return;
78+
}
79+
}
80+
81+
auto now = std::chrono::system_clock::now();
82+
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
83+
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
84+
85+
std::stringstream ss;
86+
ss << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d_%H-%M-%S");
87+
ss << '-' << std::setfill('0') << std::setw(3) << now_ms.count();
88+
std::string timestamp = ss.str();
89+
90+
std::string filename = "image/" + timestamp + ".jpg";
91+
92+
bool result = cv::imwrite(filename, mat);
93+
if (!result) {
94+
RCLCPP_ERROR(node->get_logger(), "Failed to save image!");
95+
}
96+
}
97+
7298
cv::Mat CameraNode::get_mat()
7399
{
74100
update();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
// Copyright (c) 2024 ICHIRO ITS
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a copy
4+
// of this software and associated documentation files (the "Software"), to deal
5+
// in the Software without restriction, including without limitation the rights
6+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
// copies of the Software, and to permit persons to whom the Software is
8+
// furnished to do so, subject to the following conditions:
9+
//
10+
// The above copyright notice and this permission notice shall be included in
11+
// all copies or substantial portions of the Software.
12+
//
13+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
16+
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
// THE SOFTWARE.
20+
21+
#include <rclcpp/rclcpp.hpp>
22+
#include <shisen_cpp/config/grpc/call_data_record_image.hpp>
23+
#include <shisen_interfaces/shisen.grpc.pb.h>
24+
#include <shisen_interfaces/shisen.pb.h>
25+
26+
namespace shisen_cpp
27+
{
28+
CallDataRecordImage::CallDataRecordImage(
29+
shisen_interfaces::proto::Config::AsyncService * service, grpc::ServerCompletionQueue * cq,
30+
const std::string & path, const std::shared_ptr<camera::CameraNode>& camera_node)
31+
: CallData(service, cq, path), camera_node_(camera_node)
32+
{
33+
Proceed();
34+
}
35+
36+
void CallDataRecordImage::AddNextToCompletionQueue()
37+
{
38+
new CallDataRecordImage(service_, cq_, path_, camera_node_);
39+
}
40+
41+
void CallDataRecordImage::WaitForRequest()
42+
{
43+
service_->RequestRecordImage(&ctx_, &request_, &responder_, cq_, cq_, this);
44+
}
45+
46+
void CallDataRecordImage::HandleRequest()
47+
{
48+
try {
49+
camera_node_->save_image(camera_node_->get_mat());
50+
} catch(const std::exception& e) {
51+
RCLCPP_ERROR(rclcpp::get_logger("Record Image"), e.what());
52+
}
53+
}
54+
} // namespace shisen_cpp

src/shisen_cpp/config/grpc/config.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <shisen_cpp/config/grpc/call_data_get_image.hpp>
2525
#include <shisen_cpp/config/grpc/call_data_save_capture_setting.hpp>
2626
#include <shisen_cpp/config/grpc/call_data_set_capture_setting.hpp>
27+
#include <shisen_cpp/config/grpc/call_data_record_image.hpp>
2728
#include <shisen_cpp/config/grpc/config.hpp>
2829
#include <jitsuyo/config.hpp>
2930

@@ -72,6 +73,7 @@ void ConfigGrpc::Run(const std::string & path, std::shared_ptr<camera::CameraNod
7273
new CallDataSaveCaptureSetting(&service_, cq_.get(), path);
7374
new CallDataSetCaptureSetting(&service_, cq_.get(), path, camera_node);
7475
new CallDataGetImage(&service_, cq_.get(), path, camera_node);
76+
new CallDataRecordImage(&service_, cq_.get(), path, camera_node);
7577
void * tag; // uniquely identifies a request.
7678
bool ok = true;
7779
while (true) {

0 commit comments

Comments
 (0)