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
feat: add state to current process
  • Loading branch information
taikitanaka3 committed Jul 6, 2022
commit a4669ae850f7dfd0eba10d93b4bcafadcece77e5
111 changes: 95 additions & 16 deletions common/tier4_screen_capture_rviz_plugin/src/screen_capture_panel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,57 +14,136 @@

#include "screen_capture_panel.hpp"

#include <QHBoxLayout>
#include <QTimer>
#include <rclcpp/rclcpp.hpp>

#include <ctime>
#include <filesystem>
#include <iostream>
#include <string>

void setFormatTime(QLineEdit * line, double time)
void setFormatDate(QLineEdit * line, double time)
{
char buffer[128];
time_t seconds = static_cast<time_t>(time);
strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H:%M:%S", localtime(&seconds));
line->setText(QString(buffer) + QString::number((time - seconds), 'f', 2).rightRef(3));
strftime(buffer, sizeof(buffer), "%Y-%m-%d-%H", localtime(&seconds));
line->setText(QString(buffer));
}

AutowareScreenCapturePanel::AutowareScreenCapturePanel(QWidget * parent)
: rviz_common::Panel(parent)
{
ros_time_label_ = new QLineEdit;
ros_time_label_->setReadOnly(true);
auto * v_layout = new QVBoxLayout;

QHBoxLayout * layout = new QHBoxLayout(this);
screen_capture_button_ptr_ = new QPushButton("Capture Screen Shot");
// screen capture
auto * cap_layout = new QHBoxLayout;
{
ros_time_label_ = new QLineEdit;
ros_time_label_->setReadOnly(true);
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_);
}

connect(screen_capture_button_ptr_, SIGNAL(clicked()), this, SLOT(onClickScreenCapture()));
layout->addWidget(new QLabel("ROS Time:"));
layout->addWidget(ros_time_label_);
layout->addWidget(screen_capture_button_ptr_);
setLayout(layout);
// video capture
auto * video_cap_layout = new QHBoxLayout;
{
capture_to_mp4_button_ptr_ = new QPushButton("Capture To Video");
connect(capture_to_mp4_button_ptr_, SIGNAL(clicked()), this, SLOT(onClickVideoCapture()));
capture_hz_ = new QSpinBox();
capture_hz_->setRange(1, 2);
capture_hz_->setValue(1);
capture_hz_->setSingleStep(1);
connect(capture_hz_, SIGNAL(valueChanged(int)), this, SLOT(onRateChanged()));
// video cap layout
video_cap_layout->addWidget(capture_to_mp4_button_ptr_);
video_cap_layout->addWidget(capture_hz_);
video_cap_layout->addWidget(new QLabel(" [Hz]"));
}

// consider layout
{
v_layout->addLayout(cap_layout);
v_layout->addLayout(video_cap_layout);
setLayout(v_layout);
}
QTimer * timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &AutowareScreenCapturePanel::update);
timer->start(60);
timer->start(30);
capture_timer_ = new QTimer(this);
connect(capture_timer_, &QTimer::timeout, this, &AutowareScreenCapturePanel::onTimer);
state_ = State::WAITING_FOR_CAPTURE;
}

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");
return;
}

void convertToVideo() {}

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());
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;
capture_timer_->stop();
capture_to_mp4_button_ptr_->setText("writing to video");
capture_to_mp4_button_ptr_->setStyleSheet("background-color: #FFFF00;");
convertToVideo();
break;
case State::WRITING:
skip_capture_ = true;
capture_to_mp4_button_ptr_->setText("waiting for capture");
capture_to_mp4_button_ptr_->setStyleSheet("background-color: #00FF00;");
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";
std::cerr << "file" << file << std::endl;
getDisplayContext()->getViewManager()->getRenderPanel()->getRenderWindow()->captureScreenShot(
file);
counter_++;
}

void AutowareScreenCapturePanel::update()
{
setFormatTime(
setFormatDate(
ros_time_label_, rviz_ros_node_.lock()->get_raw_node()->get_clock()->now().seconds());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@
#ifndef SCREEN_CAPTURE_PANEL_HPP_
#define SCREEN_CAPTURE_PANEL_HPP_

// Qt
#include <QHBoxLayout>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QSpinBox>
#include <QTimer>

// rviz
#include <rviz_common/display_context.hpp>
#include <rviz_common/panel.hpp>
#include <rviz_common/render_panel.hpp>
Expand All @@ -37,13 +43,26 @@ class AutowareScreenCapturePanel : public rviz_common::Panel
explicit AutowareScreenCapturePanel(QWidget * parent = nullptr);
void update();
void onInitialize() override;
void createWallTimer();
void onTimer();

public Q_SLOTS:
void onClickScreenCapture();
void onClickCaptureToVideo();
void onClickVideoCapture();
void onRateChanged();

private:
QLineEdit * 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 };
State state_;
bool skip_capture_ = {true};
std::string root_folder_;
size_t counter_;

protected:
rviz_common::ros_integration::RosNodeAbstractionIface::WeakPtr rviz_ros_node_;
Expand Down