PNGParallelRecordingStrategy.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * ArmarX is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation.
7  *
8  * ArmarX is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program. If not, see <http://www.gnu.org/licenses/>.
15  *
16  * @package visionx::imrec
17  * @author Christian R. G. Dreher <christian.dreher@student.kit.edu>
18  * @date 2018
19  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
20  * GNU General Public License
21  */
22 
23 
25 
26 
27 // STD/STL
28 #include <filesystem>
29 #include <thread>
30 #include <vector>
31 
32 // OpenCV 2
33 #include <opencv2/opencv.hpp>
34 
35 // ArmarX
37 
39 
41  m_pool_max_size{1}
42 {
43  // pass
44 }
45 
47  const std::filesystem::path& filePath,
48  const std::string& file_name,
49  unsigned int png_compression,
50  unsigned int thread_pool_size) :
51  visionx::imrec::AbstractSequencedRecordingStrategy(filePath / file_name, ".png"),
52  m_png_compression{png_compression},
53  m_pool_max_size{thread_pool_size > 0 ? thread_pool_size : 1}
54 {
55  ARMARX_CHECK_GREATER_EQUAL(png_compression, 1) << "Compression cannot be lower than 1.";
56  ARMARX_CHECK_LESS_EQUAL(png_compression, 9) << "Compression cannot be greater than 9.";
57 }
58 
60 {
61  // pass
62 }
63 
64 void
66  const cv::Mat& image,
67  const std::filesystem::path& path)
68 {
69  std::filesystem::path snapshotPath(path);
70 
71  // Make sure that path does exist, ensure PNG extension
72  ARMARX_CHECK_EXPRESSION(std::filesystem::exists(path.parent_path()))
73  << "Cannot take snapshot, path '" + path.parent_path().string() + "' does ot exist";
74 
75  if (snapshotPath.extension() != ".png")
76  {
77  snapshotPath += ".png";
78  }
79 
80  const int snapshot_compression = 9;
81  std::vector<int> params{cv::IMWRITE_PNG_COMPRESSION,
82  snapshot_compression,
83  cv::IMWRITE_PNG_STRATEGY,
84  cv::IMWRITE_PNG_STRATEGY_RLE};
85  cv::imwrite(snapshotPath.string(), image, params);
86 }
87 
88 void
90  const CByteImage& image,
91  const std::filesystem::path& path)
92 {
93  // Covert to OpenCV image and run the CV method
94  cv::Mat cv_image;
95  visionx::imrec::convert(image, cv_image);
96  recordSnapshot(cv_image, path);
97 }
98 
99 void
101 {
103  writeMetadataLine("png_compression", "unsigned int", std::to_string(m_png_compression));
104  m_pool_current_size.store(0);
105 }
106 
107 void
109 {
110  std::unique_lock l{m_pool_mutex};
111  m_pool_cv.wait(l, [&] { return m_pool_current_size.load() == 0; });
112 
114 }
115 
116 void
118  const cv::Mat& frame,
119  const std::chrono::microseconds timestamp)
120 {
121  std::scoped_lock l{m_record_frame_mutex};
122  const auto& [sequence_number, frame_name] = writeMetadataFrame(frame, timestamp);
123  const std::filesystem::path path = deriveFramePath(sequence_number, frame_name);
124 
125  {
126  std::unique_lock l{m_pool_mutex};
127  m_pool_cv.wait(l, [&] { return m_pool_current_size.load() < m_pool_max_size; });
128  }
129 
130  // TODO: Use a boost::asio::thread_pool instead after switching to boost 1.66.
131  std::thread([this, path, frame_cp = frame.clone()] { recordFrameAsync(path, frame_cp); })
132  .detach();
133 }
134 
135 void
137  const std::filesystem::path path,
138  const cv::Mat& frame)
139 {
140  m_pool_current_size++;
141 
142  std::vector<int> params{cv::IMWRITE_PNG_COMPRESSION,
143  static_cast<int>(m_png_compression),
144  cv::IMWRITE_PNG_STRATEGY,
145  cv::IMWRITE_PNG_STRATEGY_RLE};
146  cv::imwrite(path.string(), frame, params);
147 
148  m_pool_current_size--;
149  m_pool_cv.notify_all();
150 }
visionx
ArmarX headers.
Definition: OpenPoseStressTest.h:38
visionx::imrec::strats::PNGParallelRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: PNGParallelRecordingStrategy.cpp:100
visionx::imrec::strats::PNGParallelRecordingStrategy::recordSnapshot
static void recordSnapshot(const cv::Mat &image, const std::filesystem::path &path)
Definition: PNGParallelRecordingStrategy.cpp:65
visionx::imrec::strats::PNGParallelRecordingStrategy::PNGParallelRecordingStrategy
PNGParallelRecordingStrategy()
Definition: PNGParallelRecordingStrategy.cpp:40
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:111
visionx::imrec::AbstractSequencedRecordingStrategy
An object of this class behaves likee a normal recording, but is in fact a sequence of images.
Definition: AbstractSequencedRecordingStrategy.h:40
visionx::imrec::convert
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition: helper.cpp:40
visionx::imrec::strats::PNGParallelRecordingStrategy::~PNGParallelRecordingStrategy
~PNGParallelRecordingStrategy() override
Definition: PNGParallelRecordingStrategy.cpp:59
visionx::imrec::strats::PNGParallelRecordingStrategy::recordFrame
void recordFrame(const cv::Mat &frame, std::chrono::microseconds timestamp) override
Adds the given frame to the recording.
Definition: PNGParallelRecordingStrategy.cpp:117
ARMARX_CHECK_GREATER_EQUAL
#define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs)
This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be false it will...
Definition: ExpressionException.h:123
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
ARMARX_CHECK_LESS_EQUAL
#define ARMARX_CHECK_LESS_EQUAL(lhs, rhs)
This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be false it will th...
Definition: ExpressionException.h:109
PNGParallelRecordingStrategy.h
ExpressionException.h
visionx::imrec::strats::PNGParallelRecordingStrategy::stopRecording
void stopRecording() override
Stops the recording.
Definition: PNGParallelRecordingStrategy.cpp:108
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
visionx::imrec::strats::PNGParallelRecordingStrategy::recordFrameAsync
void recordFrameAsync(std::filesystem::path path, const cv::Mat &frame)
Definition: PNGParallelRecordingStrategy.cpp:136
helper.h
visionx::imrec::AbstractSequencedRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AbstractSequencedRecordingStrategy.cpp:65