1 #ifndef OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
2 #define OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
13 template<
typename TDatumsNoPtr>
17 explicit DatumProducer(
const std::shared_ptr<Producer>& producerSharedPtr,
18 const unsigned long long frameFirst = 0,
19 const unsigned long long frameLast = std::numeric_limits<unsigned long long>::max(),
20 const std::shared_ptr<std::pair<std::atomic<bool>,
21 std::atomic<int>>>& videoSeekSharedPtr =
nullptr);
26 const unsigned long long mNumberFramesToProcess;
27 std::shared_ptr<Producer> spProducer;
28 unsigned long long mGlobalCounter;
29 unsigned int mNumberConsecutiveEmptyFrames;
30 std::shared_ptr<std::pair<std::atomic<bool>, std::atomic<int>>> spVideoSeek;
32 void checkIfTooManyConsecutiveEmptyFrames(
unsigned int& numberConsecutiveEmptyFrames,
33 const bool emptyFrame)
const;
44 #include <opencv2/imgproc/imgproc.hpp>
48 template<
typename TDatumsNoPtr>
50 const unsigned long long frameFirst,
const unsigned long long frameLast,
51 const std::shared_ptr<std::pair<std::atomic<bool>,
52 std::atomic<int>>>& videoSeekSharedPtr) :
53 mNumberFramesToProcess{(frameLast != std::numeric_limits<unsigned long long>::max()
54 ? frameLast - frameFirst : frameLast)},
55 spProducer{producerSharedPtr},
57 mNumberConsecutiveEmptyFrames{0u},
58 spVideoSeek{videoSeekSharedPtr}
63 spProducer->set(CV_CAP_PROP_POS_FRAMES, (
double)frameFirst);
65 catch (
const std::exception& e)
67 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
71 template<
typename TDatumsNoPtr>
76 auto datums = std::make_shared<TDatumsNoPtr>();
78 if (mNumberFramesToProcess != std::numeric_limits<unsigned long long>::max()
79 && mGlobalCounter > mNumberFramesToProcess)
81 spProducer->release();
84 const bool datumProducerRunning = spProducer->isOpened();
86 if (datumProducerRunning)
89 if (spVideoSeek !=
nullptr)
92 const auto increment = spVideoSeek->second + (spVideoSeek->first ? 1 : 0);
96 spProducer->set(CV_CAP_PROP_POS_FRAMES, spProducer->get(CV_CAP_PROP_POS_FRAMES) + increment);
97 spVideoSeek->second = 0;
100 auto nextFrameName = spProducer->getNextFrameName();
101 auto cvMats = spProducer->getFrames();
102 auto cameraMatrices = spProducer->getCameraMatrices();
104 checkIfTooManyConsecutiveEmptyFrames(mNumberConsecutiveEmptyFrames, cvMats.empty() || cvMats[0].empty());
107 datums->resize(cvMats.size());
109 auto& datum = (*datums)[0];
111 std::swap(datum.name, nextFrameName);
112 datum.cvInputData = cvMats[0];
113 if (!cameraMatrices.empty())
114 datum.cameraMatrix = cameraMatrices[0];
116 if (datum.cvInputData.channels() != 3)
118 const std::string commonMessage{
"Input images must be 3-channel BGR."};
120 if (datum.cvInputData.channels() == 1)
123 cv::cvtColor(datum.cvInputData, datum.cvInputData, CV_GRAY2BGR);
126 error(commonMessage, __LINE__, __FUNCTION__, __FILE__);
128 datum.cvOutputData = datum.cvInputData;
130 if (datums->size() > 1)
133 for (
auto i = 1u ; i < datums->size() ; i++)
135 auto& datumI = (*datums)[i];
136 datumI.name = datum.name;
137 datumI.cvInputData = cvMats[i];
138 datumI.cvOutputData = datumI.cvInputData;
139 if (cameraMatrices.size() > i)
140 datumI.cameraMatrix = cameraMatrices[i];
144 if (!datumProducerRunning || (*datums)[0].cvInputData.empty())
147 if (datums !=
nullptr)
152 return std::make_pair(datumProducerRunning, datums);
154 catch (
const std::exception& e)
156 error(e.what(), __LINE__, __FUNCTION__, __FILE__);
157 return std::make_pair(
false, std::make_shared<TDatumsNoPtr>());
161 template<
typename TDatumsNoPtr>
163 const bool emptyFrame)
const
165 numberConsecutiveEmptyFrames = (emptyFrame ? numberConsecutiveEmptyFrames+1 : 0);
166 const auto threshold = 500u;
167 if (numberConsecutiveEmptyFrames >= threshold)
168 error(
"Detected too many (" + std::to_string(numberConsecutiveEmptyFrames) +
") empty frames in a row.",
169 __LINE__, __FUNCTION__, __FILE__);
172 extern template class DatumProducer<DATUM_BASE_NO_PTR>;
176 #endif // OPENPOSE_PRODUCER_DATUM_PRODUCER_HPP
std::pair< bool, std::shared_ptr< TDatumsNoPtr > > checkIfRunningAndGetDatum()
Definition: datumProducer.hpp:72
OP_API void error(const std::string &message, const int line=-1, const std::string &function="", const std::string &file="")
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:14
DatumProducer(const std::shared_ptr< Producer > &producerSharedPtr, const unsigned long long frameFirst=0, 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:49
std::string string
Definition: cl2.hpp:574