Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add command line flag to rotate image by 180 degrees #50

Merged
merged 1 commit into from
Nov 2, 2024
Merged
Changes from all commits
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
29 changes: 19 additions & 10 deletions src/ws_server.cu
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ using json = nlohmann::json;

DEFINE_int32(camera_idx, 0, "Camera index");
DEFINE_string(cal_file, "", "path name to calibration file");
DEFINE_bool(rotate_img, true,
"Rotates image by 180 degrees prior to detecting apriltags");

enum ExposureMode { AUTO = 0, MANUAL = 1 };

Expand Down Expand Up @@ -77,7 +79,7 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
settings_changed_ = true;
}
if (j["type"] == "rotate") {
rotate_img_ = j["value"].get<bool>();
gui_rotate_img_ = j["value"].get<bool>();
settings_changed_ = true;
}

Expand Down Expand Up @@ -173,10 +175,10 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
*bgr_img = rotatedImage.clone();
}

void startReadAndSendThread(const int camera_idx,
const std::string& cal_file) {
read_thread_ =
std::thread(&AprilTagHandler::readAndSend, this, camera_idx, cal_file);
void startReadAndSendThread(const int camera_idx, const std::string& cal_file,
const bool rotate_img) {
read_thread_ = std::thread(&AprilTagHandler::readAndSend, this, camera_idx,
cal_file, rotate_img);
}

void joinReadAndSendThread() {
Expand All @@ -202,7 +204,8 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
std::cout << "Contrast: " << cap.get(cv::CAP_PROP_CONTRAST) << std::endl;
}

void readAndSend(const int camera_idx, const std::string& cal_file) {
void readAndSend(const int camera_idx, const std::string& cal_file,
const bool rotate_img) {
std::cout << "Enabling video capture" << std::endl;
bool camera_started = false;
cv::VideoCapture cap;
Expand Down Expand Up @@ -280,8 +283,12 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
info.cx = cam.cx;
info.cy = cam.cy;

int frame_counter = 0;
// Set the value of the gui rotate image variable to the value
// that is passed in on the command line. The user can change it
// later on from the gui.
gui_rotate_img_ = rotate_img;

int frame_counter = 0;
cv::Mat bgr_img, yuyv_img;
while (running_) {
// Handle settings changes.
Expand All @@ -303,7 +310,7 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
frame_counter++;

auto overallstart = std::chrono::high_resolution_clock::now();
if (rotate_img_) {
if (gui_rotate_img_) {
rotateImage(&bgr_img, 180.0);
}
cv::cvtColor(bgr_img, yuyv_img, cv::COLOR_BGR2YUV_YUYV);
Expand Down Expand Up @@ -414,7 +421,7 @@ class AprilTagHandler : public seasocks::WebSocket::Handler {
std::atomic<int> exposure_{50};
std::atomic<int> exposure_mode_{0};
std::atomic<bool> settings_changed_{false};
std::atomic<bool> rotate_img_{false};
std::atomic<bool> gui_rotate_img_{false};
std::thread read_thread_;
};

Expand Down Expand Up @@ -442,7 +449,9 @@ int main(int argc, char* argv[]) {
auto handler = std::make_shared<AprilTagHandler>(server);
server->addWebSocketHandler("/ws", handler);

handler->startReadAndSendThread(FLAGS_camera_idx, FLAGS_cal_file);
handler
->startReadAndSendThread(FLAGS_camera_idx, FLAGS_cal_file,
FLAGS_rotate_img);
server->serve("", 8080);
handler->stop();
handler->joinReadAndSendThread();
Expand Down
Loading