Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
feat: image preprocessing - center crop, resize 320x320
  • Loading branch information
juni5184 committed Jul 11, 2025
commit 6974001801f73d64c73bb9375f7f7124095cbd38
9 changes: 9 additions & 0 deletions example/go2/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
find_package(OpenCV REQUIRED)

add_executable(go2_video_client_custom go2_video_client_custom.cpp)
target_include_directories(go2_video_client_custom PRIVATE ${OpenCV_INCLUDE_DIRS})
target_link_libraries(go2_video_client_custom unitree_sdk2 ${OpenCV_LIBS})

# add_executable(go2_trajectory_follow go2_trajectory_follow.cpp)
# target_link_libraries(go2_trajectory_follow unitree_sdk2)

Expand All @@ -17,5 +23,8 @@ target_link_libraries(go2_robot_state_client unitree_sdk2)
add_executable(go2_video_client go2_video_client.cpp)
target_link_libraries(go2_video_client unitree_sdk2)

# add_executable(go2_video_client_custom go2_video_client_custom.cpp)
# target_link_libraries(go2_video_client_custom unitree_sdk2)

add_executable(go2_vui_client go2_vui_client.cpp)
target_link_libraries(go2_vui_client unitree_sdk2)
66 changes: 66 additions & 0 deletions example/go2/go2_video_client_custom.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#include <unitree/robot/go2/video/video_client.hpp>
#include <iostream>
#include <fstream>
#include <ctime>
#include <unistd.h> // for sleep
#include <opencv2/opencv.hpp> // OpenCV for image processing

int main()
{
unitree::robot::ChannelFactory::Instance()->Init(0);
unitree::robot::go2::VideoClient video_client;
video_client.SetTimeout(1.0f);
video_client.Init();

std::vector<uint8_t> image_sample;
int ret;

while (true)
{
ret = video_client.GetImageSample(image_sample);

if (ret == 0) {
std::string raw_name = "raw_image.jpg";
std::string processed_name = "last_image.jpg";

// Save raw image
std::ofstream image_file(raw_name, std::ios::binary);
if (image_file.is_open()) {
image_file.write(reinterpret_cast<const char*>(image_sample.data()), image_sample.size());
image_file.close();
std::cout << "Raw image saved as " << raw_name << std::endl;
} else {
std::cerr << "Error: Failed to save raw image." << std::endl;
continue;
}

// Load image with OpenCV
cv::Mat img = cv::imread(raw_name);
if (img.empty()) {
std::cerr << "Error: Failed to read image with OpenCV." << std::endl;
continue;
}

// Center crop
int h = img.rows;
int w = img.cols;
int crop_size = std::min(h, w);
int top = (h - crop_size) / 2;
int left = (w - crop_size) / 2;
cv::Rect roi(left, top, crop_size, crop_size);
cv::Mat cropped = img(roi);

// Resize to 320x320
cv::Mat resized;
cv::resize(cropped, resized, cv::Size(320, 320));

// Save final image
cv::imwrite(processed_name, resized);
std::cout << "Processed image saved as " << processed_name << std::endl;
}

usleep(50000); // 0.05 seconds = 50,000 microseconds
}

return 0;
}