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
38 
39 
41  m_pool_max_size{1}
42 {
43  // pass
44 }
45 
46 
47 visionx::imrec::strats::PNGParallelRecordingStrategy::PNGParallelRecordingStrategy(const std::filesystem::path& filePath, const std::string& file_name, unsigned int png_compression, unsigned int thread_pool_size) :
48  visionx::imrec::AbstractSequencedRecordingStrategy(filePath / file_name, ".png"),
49  m_png_compression{png_compression},
50  m_pool_max_size{thread_pool_size > 0 ? thread_pool_size : 1}
51 {
52  ARMARX_CHECK_GREATER_EQUAL(png_compression, 1) << "Compression cannot be lower than 1.";
53  ARMARX_CHECK_LESS_EQUAL(png_compression, 9) << "Compression cannot be greater than 9.";
54 }
55 
56 
58 {
59  // pass
60 }
61 
62 
63 void
64 visionx::imrec::strats::PNGParallelRecordingStrategy::recordSnapshot(const cv::Mat& image, const std::filesystem::path& path)
65 {
66  std::filesystem::path snapshotPath(path);
67 
68  // Make sure that path does exist, ensure PNG extension
69  ARMARX_CHECK_EXPRESSION(std::filesystem::exists(path.parent_path())) << "Cannot take snapshot, path '" + path.parent_path().string() + "' does ot exist";
70 
71  if (snapshotPath.extension() != ".png")
72  {
73  snapshotPath += ".png";
74  }
75 
76  const int snapshot_compression = 9;
77  std::vector<int> params {cv::IMWRITE_PNG_COMPRESSION, snapshot_compression,
78  cv::IMWRITE_PNG_STRATEGY, cv::IMWRITE_PNG_STRATEGY_RLE};
79  cv::imwrite(snapshotPath.string(), image, params);
80 }
81 
82 
83 void
84 visionx::imrec::strats::PNGParallelRecordingStrategy::recordSnapshot(const CByteImage& image, const std::filesystem::path& path)
85 {
86  // Covert to OpenCV image and run the CV method
87  cv::Mat cv_image;
88  visionx::imrec::convert(image, cv_image);
89  recordSnapshot(cv_image, path);
90 }
91 
92 
93 void
95 {
97  writeMetadataLine("png_compression", "unsigned int", std::to_string(m_png_compression));
98  m_pool_current_size.store(0);
99 }
100 
101 
102 void
104 {
105  std::unique_lock l{m_pool_mutex};
106  m_pool_cv.wait(l, [&] { return m_pool_current_size.load() == 0; });
107 
109 }
110 
111 
112 void
113 visionx::imrec::strats::PNGParallelRecordingStrategy::recordFrame(const cv::Mat& frame, const std::chrono::microseconds timestamp)
114 {
115  std::scoped_lock l{m_record_frame_mutex};
116  const auto& [sequence_number, frame_name] = writeMetadataFrame(frame, timestamp);
117  const std::filesystem::path path = deriveFramePath(sequence_number, frame_name);
118 
119  {
120  std::unique_lock l{m_pool_mutex};
121  m_pool_cv.wait(l, [&] { return m_pool_current_size.load() < m_pool_max_size; });
122  }
123 
124  // TODO: Use a boost::asio::thread_pool instead after switching to boost 1.66.
125  std::thread([this, path, frame_cp = frame.clone()]
126  {
127  recordFrameAsync(path, frame_cp);
128  }).detach();
129 }
130 
131 
132 void
133 visionx::imrec::strats::PNGParallelRecordingStrategy::recordFrameAsync(const std::filesystem::path path, const cv::Mat& frame)
134 {
135  m_pool_current_size++;
136 
137  std::vector<int> params {cv::IMWRITE_PNG_COMPRESSION, static_cast<int>(m_png_compression),
138  cv::IMWRITE_PNG_STRATEGY, cv::IMWRITE_PNG_STRATEGY_RLE};
139  cv::imwrite(path.string(), frame, params);
140 
141  m_pool_current_size--;
142  m_pool_cv.notify_all();
143 }
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:94
visionx::imrec::strats::PNGParallelRecordingStrategy::recordSnapshot
static void recordSnapshot(const cv::Mat &image, const std::filesystem::path &path)
Definition: PNGParallelRecordingStrategy.cpp:64
visionx::imrec::strats::PNGParallelRecordingStrategy::PNGParallelRecordingStrategy
PNGParallelRecordingStrategy()
Definition: PNGParallelRecordingStrategy.cpp:40
visionx::imrec::AbstractRecordingStrategy::stopRecording
virtual void stopRecording()
Stops the recording.
Definition: AbstractRecordingStrategy.cpp:116
visionx::imrec::AbstractSequencedRecordingStrategy
An object of this class behaves likee a normal recording, but is in fact a sequence of images.
Definition: AbstractSequencedRecordingStrategy.h:41
visionx::imrec::convert
void convert(const CByteImage &in, cv::Mat &out)
Converts an IVT CByteImage to OpenCV's BGR Mat.
Definition: helper.cpp:41
visionx::imrec::strats::PNGParallelRecordingStrategy::~PNGParallelRecordingStrategy
~PNGParallelRecordingStrategy() override
Definition: PNGParallelRecordingStrategy.cpp:57
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:113
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:40
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:103
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:133
helper.h
visionx::imrec::AbstractSequencedRecordingStrategy::startRecording
void startRecording() override
Starts the recording manually if constructed empty.
Definition: AbstractSequencedRecordingStrategy.cpp:67