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

feat: add screen capture package #1177

Merged
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
Prev Previous commit
Next Next commit
style: refactor codes
  • Loading branch information
taikitanaka3 committed Jul 6, 2022
commit 4fc42f2174013c186e8f3d44c5c0893723901569
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,12 @@
#include <ctime>
#include <filesystem>
#include <iostream>
#include <string>

void setFormatDate(QLineEdit * line, double time)
void setFormatDate(QLabel * line, double time)
{
char buffer[128];
time_t seconds = static_cast<time_t>(time);
strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H", localtime(&seconds));
strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H-%m-%S", localtime(&seconds));
line->setText(QString(buffer));
}

Expand All @@ -37,13 +36,12 @@ AutowareScreenCapturePanel::AutowareScreenCapturePanel(QWidget * parent)
// screen capture
auto * cap_layout = new QHBoxLayout;
{
ros_time_label_ = new QLineEdit;
ros_time_label_->setReadOnly(true);
ros_time_label_ = new QLabel;
screen_capture_button_ptr_ = new QPushButton("Capture Screen Shot");
connect(screen_capture_button_ptr_, SIGNAL(clicked()), this, SLOT(onClickScreenCapture()));
cap_layout->addWidget(screen_capture_button_ptr_);
cap_layout->addWidget(new QLabel("ROS Time:"));
cap_layout->addWidget(ros_time_label_);
cap_layout->addWidget(new QLabel(" [Key] "));
}

// video capture
Expand All @@ -55,7 +53,7 @@ AutowareScreenCapturePanel::AutowareScreenCapturePanel(QWidget * parent)
capture_hz_->setRange(1, 2);
capture_hz_->setValue(1);
capture_hz_->setSingleStep(1);
connect(capture_hz_, SIGNAL(valueChanged(int)), this, SLOT(onRateChanged()));
connect(capture_hz_, SIGNAL(valueChanged(int)), this, SLOT(onRateChanged(int)));
// video cap layout
video_cap_layout->addWidget(capture_to_mp4_button_ptr_);
video_cap_layout->addWidget(capture_hz_);
Expand All @@ -70,7 +68,7 @@ AutowareScreenCapturePanel::AutowareScreenCapturePanel(QWidget * parent)
}
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &AutowareScreenCapturePanel::update);
timer->start(30);
timer->start(1000);
capture_timer_ = new QTimer(this);
connect(capture_timer_, &QTimer::timeout, this, &AutowareScreenCapturePanel::onTimer);
state_ = State::WAITING_FOR_CAPTURE;
Expand All @@ -81,15 +79,8 @@ void AutowareScreenCapturePanel::onInitialize()
rviz_ros_node_ = getDisplayContext()->getRosNodeAbstraction();
}

void AutowareScreenCapturePanel::onRateChanged()
{
// convert rate from Hz to milliseconds
const auto period = std::chrono::milliseconds(static_cast<int64_t>(1e3 / capture_hz_->value()));
}

void AutowareScreenCapturePanel::onClickScreenCapture()
{
if (skip_capture_) return;
const std::string time_text = ros_time_label_->text().toStdString();
getDisplayContext()->getViewManager()->getRenderPanel()->getRenderWindow()->captureScreenShot(
time_text + ".png");
Expand All @@ -103,38 +94,39 @@ void AutowareScreenCapturePanel::onClickVideoCapture()
const int clock = static_cast<int>(1e3 / capture_hz_->value());
switch (state_) {
case State::WAITING_FOR_CAPTURE:
skip_capture_ = false;
counter_ = 0;
std::filesystem::create_directory(ros_time_label_->text().toStdString());
// initialize setting
{
counter_ = 0;
root_folder_ = ros_time_label_->text().toStdString();
std::filesystem::create_directory(root_folder_);
}
capture_to_mp4_button_ptr_->setText("capturing rviz screen");
capture_to_mp4_button_ptr_->setStyleSheet("background-color: #FF0000;");
capture_timer_->start(clock);
state_ = State::CAPTURING;
break;
case State::CAPTURING:
skip_capture_ = true;
state_ = State::WRITING;
case State::CAPTURING: {
capture_timer_->stop();
}
capture_to_mp4_button_ptr_->setText("writing to video");
capture_to_mp4_button_ptr_->setStyleSheet("background-color: #FFFF00;");
convertToVideo();
state_ = State::FINALIZED;
break;
case State::WRITING:
skip_capture_ = true;
case State::FINALIZED:
capture_to_mp4_button_ptr_->setText("waiting for capture");
capture_to_mp4_button_ptr_->setStyleSheet("background-color: #00FF00;");
state_ = State::WAITING_FOR_CAPTURE;
break;
}
return;
}

void AutowareScreenCapturePanel::onTimer()
{
if (skip_capture_) return;
const std::string time_text = ros_time_label_->text().toStdString();
std::stringstream count_text;
count_text << std::setw(4) << std::setfill('0') << counter_;
const std::string file = time_text + "/" + count_text.str() + ".png";
count_text << std::setw(6) << std::setfill('0') << counter_;
const std::string file = root_folder_ + "/" + count_text.str() + ".png";
std::cerr << "file" << file << std::endl;
getDisplayContext()->getViewManager()->getRenderPanel()->getRenderWindow()->captureScreenShot(
file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include <rviz_rendering/render_window.hpp>

#include <memory>
#include <string>

class QLineEdit;

Expand All @@ -50,17 +51,16 @@ public Q_SLOTS:
void onClickScreenCapture();
void onClickCaptureToVideo();
void onClickVideoCapture();
void onRateChanged();
// void onRateChanged(){};

private:
QLineEdit * ros_time_label_;
QLabel * ros_time_label_;
QPushButton * screen_capture_button_ptr_;
QPushButton * capture_to_mp4_button_ptr_;
QSpinBox * capture_hz_;
QTimer * capture_timer_;
enum class State { WAITING_FOR_CAPTURE, CAPTURING, WRITING };
enum class State { WAITING_FOR_CAPTURE, CAPTURING, FINALIZED };
State state_;
bool skip_capture_ = {true};
std::string root_folder_;
size_t counter_;

Expand Down