1 #ifndef OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
2 #define OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
14 template<
typename TDatumsNoPtr>
18 explicit DatumProducer(
const std::shared_ptr<Producer>& producerSharedPtr,
19 const unsigned long long frameFirst = 0,
const unsigned long long frameStep = 1,
20 const unsigned long long frameLast = std::numeric_limits<unsigned long long>::max(),
21 const std::shared_ptr<std::pair<std::atomic<bool>,
22 std::atomic<int>>>& videoSeekSharedPtr =
nullptr);
29 const unsigned long long mNumberFramesToProcess;
30 std::shared_ptr<Producer> spProducer;
31 unsigned long long mGlobalCounter;
32 unsigned long long mFrameStep;
33 unsigned int mNumberConsecutiveEmptyFrames;
34 std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>> spVideoSeek;
36 void checkIfTooManyConsecutiveEmptyFrames(
unsigned int& numberConsecutiveEmptyFrames,
37 const bool emptyFrame)
const;
48 #include <opencv2/imgproc/imgproc.hpp>
52 template<
typename TDatumsNoPtr>
54 const unsigned long long frameFirst,
const unsigned long long frameStep,
55 const unsigned long long frameLast,
56 const std::shared_ptr<std::pair<std::atomic<bool>,
57 std::atomic<int>>>& videoSeekSharedPtr) :
58 mNumberFramesToProcess{(frameLast != std::numeric_limits<unsigned long long>::max()
59 ? frameLast - frameFirst : frameLast)},
60 spProducer{producerSharedPtr},
62 mFrameStep{frameStep},
63 mNumberConsecutiveEmptyFrames{0u},
64 spVideoSeek{videoSeekSharedPtr}
69 if (frameLast < frameFirst)
70 error(
"The desired initial frame must be lower than the last one (flags `--frame_first` vs."
71 " `--frame_last`). Current: " + std::to_string(frameFirst) +
" vs. " + std::to_string(frameLast)
72 +
".", __LINE__, __FUNCTION__, __FILE__);
73 if (frameLast != std::numeric_limits<unsigned long long>::max()
74 && frameLast > spProducer->get(CV_CAP_PROP_FRAME_COUNT)-1)
75 error(
"The desired last frame must be lower than the length of the video or the number of images."
76 " Current: " + std::to_string(frameLast) +
" vs. "
77 + std::to_string(
intRound(spProducer->get(CV_CAP_PROP_FRAME_COUNT))-1) +
".",
78 __LINE__, __FUNCTION__, __FILE__);
84 spProducer->set(CV_CAP_PROP_POS_FRAMES, (
double)frameFirst);
89 catch (
const std::exception& e)
91 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
95 template<
typename TDatumsNoPtr>
100 template<
typename TDatumsNoPtr>
105 auto datums = std::make_shared<TDatumsNoPtr>();
107 if (mNumberFramesToProcess != std::numeric_limits<unsigned long long>::max()
108 && mGlobalCounter > mNumberFramesToProcess)
110 spProducer->release();
113 const bool datumProducerRunning = spProducer->isOpened();
115 if (datumProducerRunning)
118 if (spVideoSeek !=
nullptr)
121 const auto increment = spVideoSeek->second - (spVideoSeek->first ? 1 : 0);
124 spProducer->set(CV_CAP_PROP_POS_FRAMES, spProducer->get(CV_CAP_PROP_POS_FRAMES) + increment);
126 spVideoSeek->second = 0;
128 auto nextFrameName = spProducer->getNextFrameName();
129 const auto nextFrameNumber = (
unsigned long long)spProducer->get(CV_CAP_PROP_POS_FRAMES);
130 const auto cvMats = spProducer->getFrames();
131 const auto cameraMatrices = spProducer->getCameraMatrices();
132 auto cameraExtrinsics = spProducer->getCameraExtrinsics();
133 auto cameraIntrinsics = spProducer->getCameraIntrinsics();
135 checkIfTooManyConsecutiveEmptyFrames(mNumberConsecutiveEmptyFrames, cvMats.empty() || cvMats[0].empty());
138 datums->resize(cvMats.size());
140 auto& datum = (*datums)[0];
142 std::swap(datum.name, nextFrameName);
143 datum.frameNumber = nextFrameNumber;
144 datum.cvInputData = cvMats[0];
145 if (!cameraMatrices.empty())
147 datum.cameraMatrix = cameraMatrices[0];
148 datum.cameraExtrinsics = cameraExtrinsics[0];
149 datum.cameraIntrinsics = cameraIntrinsics[0];
152 if (datum.cvInputData.channels() != 3)
154 const std::string commonMessage{
"Input images must be 3-channel BGR."};
156 if (datum.cvInputData.channels() == 1)
159 cv::cvtColor(datum.cvInputData, datum.cvInputData, CV_GRAY2BGR);
162 error(commonMessage, __LINE__, __FUNCTION__, __FILE__);
164 datum.cvOutputData = datum.cvInputData;
166 if (datums->size() > 1)
169 for (
auto i = 1u ; i < datums->size() ; i++)
171 auto& datumI = (*datums)[i];
172 datumI.name = datum.name;
173 datumI.frameNumber = datum.frameNumber;
174 datumI.cvInputData = cvMats[i];
175 datumI.cvOutputData = datumI.cvInputData;
176 if (cameraMatrices.size() > i)
178 datumI.cameraMatrix = cameraMatrices[i];
179 datumI.cameraExtrinsics = cameraExtrinsics[i];
180 datumI.cameraIntrinsics = cameraIntrinsics[i];
185 if (!datumProducerRunning || (*datums)[0].cvInputData.empty())
188 if (datums !=
nullptr)
189 mGlobalCounter += mFrameStep;
193 return std::make_pair(datumProducerRunning, datums);
195 catch (
const std::exception& e)
197 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
198 return std::make_pair(
false, std::make_shared<TDatumsNoPtr>());
202 template<
typename TDatumsNoPtr>
204 const bool emptyFrame)
const
206 numberConsecutiveEmptyFrames = (emptyFrame ? numberConsecutiveEmptyFrames+1 : 0);
207 const auto threshold = 500u;
208 if (numberConsecutiveEmptyFrames >= threshold)
209 error(
"Detected too many (" + std::to_string(numberConsecutiveEmptyFrames) +
") empty frames in a row.",
210 __LINE__, __FUNCTION__, __FILE__);
213 extern template class DatumProducer<DATUM_BASE_NO_PTR>;
217 #endif // OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
std::pair< bool, std::shared_ptr< TDatumsNoPtr > > checkIfRunningAndGetDatum()
Definition: datumProducer.hpp:101
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
virtual ~DatumProducer()
Definition: datumProducer.hpp:96
OP_API void log(const std::string &message, const Priority priority=Priority::Max, const int line=-1, const std::string &function="", const std::string &file="")
Definition: datumProducer.hpp:15
int intRound(const T a)
Definition: fastMath.hpp:26
DatumProducer(const std::shared_ptr< Producer > &producerSharedPtr, const unsigned long long frameFirst=0, const unsigned long long frameStep=1, const unsigned long long frameLast=std::numeric_limits< unsigned long long >::max(), const std::shared_ptr< std::pair< std::atomic< bool >, std::atomic< int >>> &videoSeekSharedPtr=nullptr)
Definition: datumProducer.hpp:53
std::string string
Definition: cl2.hpp:574