3434using DetectionComponentUtils::GetProperty;
3535
3636
37- namespace MPF { namespace COMPONENT {
37+ namespace MPF :: COMPONENT {
3838
3939 MPFFrame::MPFFrame (int index, cv::Mat data)
4040 : index(index)
4141 , data(std::move(data)) {
4242
4343 }
4444
45- bool MPFFrame::isValid () const {
46- return index >= 0 && !data.empty ();
47- }
48-
49- MPFFrame::operator bool () const {
50- return isValid ();
51- }
52-
53-
5445 namespace {
5546
56- void frameReader (MPFVideoCapture videoCapture, BlockingQueue<MPFFrame> &queue) {
47+ void frameReader (MPFVideoCapture videoCapture,
48+ BlockingQueue<std::optional<MPFFrame>> &queue) {
5749 try {
5850 while (true ) {
59- MPFFrame frame ( videoCapture.GetCurrentFramePosition () );
60- bool wasRead = videoCapture. Read (frame. data ) ;
61- if (wasRead && frame ) {
62- queue.push ( std::move (frame ));
51+ int frameIndex = videoCapture.GetCurrentFramePosition ();
52+ cv::Mat frameData ;
53+ if (videoCapture. Read (frameData) ) {
54+ queue.emplace ( MPFFrame (frameIndex, std::move (frameData) ));
6355 }
6456 else {
65- // Add invalid frame to indicate that the end of the video has been reached.
66- frame.data .release ();
67- queue.push (std::move (frame));
57+ // Add empty optional to indicate that the end of the video has been reached.
58+ queue.push (std::nullopt );
6859 queue.complete_adding ();
6960 return ;
7061 }
@@ -74,12 +65,10 @@ namespace MPF { namespace COMPONENT {
7465 // Other side requested early exit.
7566 }
7667 catch (...) {
77- // Make sure other side doesn't get stuck if an exception is thrown.
7868 try {
79- // If you try to read past the end of a video with cv::VideoCapture,
80- // it stops incrementing the frame count and keeps reporting
81- // (last_frame_index + 1) or equivalently the total number of frames.
82- queue.emplace (videoCapture.GetFrameCount ());
69+ // Add empty optional to make sure other side doesn't get stuck if an exception
70+ // is thrown.
71+ queue.push (std::nullopt );
8372 queue.complete_adding ();
8473 }
8574 catch (const QueueHaltedException&) {
@@ -127,7 +116,7 @@ namespace MPF { namespace COMPONENT {
127116 }
128117
129118
130- MPFFrame MPFAsyncVideoCapture::Read () {
119+ std::optional< MPFFrame> MPFAsyncVideoCapture::Read () {
131120 try {
132121 auto frame = frameQueue_.pop ();
133122 if (!frame) {
@@ -139,10 +128,7 @@ namespace MPF { namespace COMPONENT {
139128 catch (QueueHaltedException&) {
140129 // If frameReader ended with an exception it will be re-thrown here.
141130 doneReadingFuture_.get ();
142- // If you try to read past the end of a video with cv::VideoCapture,
143- // it stops incrementing the frame count and keeps reporting
144- // (last_frame_index + 1) or equivalently the total number of frames.
145- return MPFFrame (frameCount_);
131+ return std::nullopt ;
146132 }
147133 }
148134
@@ -170,4 +156,4 @@ namespace MPF { namespace COMPONENT {
170156 cv::Size MPFAsyncVideoCapture::GetOriginalFrameSize () const {
171157 return originalFrameSize_;
172158 }
173- }}
159+ }
0 commit comments