Skip to content

Commit 8cd1bd1

Browse files
authored
Ubuntu transition (#108)
1 parent 4f05d4c commit 8cd1bd1

26 files changed

Lines changed: 150 additions & 173 deletions

CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
cmake_minimum_required(VERSION 3.6)
2929
project(openmpf-cpp-component-sdk)
3030

31-
set(CMAKE_CXX_STANDARD 11)
31+
set(CMAKE_CXX_STANDARD 17)
3232

3333
# Configure install location for SDK libs
3434
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT) # prefix path not provided on command line
@@ -72,4 +72,4 @@ include_directories(detection/utils/include)
7272
add_subdirectory(detection/testUtils)
7373
include_directories(detection/testUtils/include)
7474

75-
add_subdirectory(mpf-cmake-helpers)
75+
add_subdirectory(mpf-cmake-helpers)

detection/api/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
cmake_minimum_required(VERSION 3.6)
2929
project(openmpf-cpp-component-api)
3030

31-
set(CMAKE_CXX_STANDARD 11)
31+
set(CMAKE_CXX_STANDARD 17)
3232

3333
find_package(OpenCV 4.5.0 EXACT REQUIRED PATHS /opt/opencv-4.5.0
3434
COMPONENTS opencv_core opencv_imgcodecs opencv_imgproc opencv_videoio)

detection/api/include/MPFAsyncVideoCapture.h

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#define OPENMPF_CPP_COMPONENT_SDK_MPFASYNCVIDEOCAPTURE_H
2929

3030
#include <future>
31+
#include <optional>
3132
#include <string>
3233

3334
#include <opencv2/core.hpp>
@@ -36,7 +37,7 @@
3637
#include "MPFVideoCapture.h"
3738
#include "BlockingQueue.h"
3839

39-
namespace MPF { namespace COMPONENT {
40+
namespace MPF::COMPONENT {
4041

4142
struct MPFFrame {
4243
public:
@@ -46,11 +47,7 @@ namespace MPF { namespace COMPONENT {
4647
int index;
4748
cv::Mat data;
4849

49-
explicit MPFFrame(int index = -1, cv::Mat data = {});
50-
51-
bool isValid() const;
52-
53-
explicit operator bool() const;
50+
MPFFrame(int index, cv::Mat data);
5451
};
5552

5653

@@ -71,7 +68,7 @@ namespace MPF { namespace COMPONENT {
7168

7269
~MPFAsyncVideoCapture();
7370

74-
MPFFrame Read();
71+
std::optional<MPFFrame> Read();
7572

7673
void ReverseTransform(MPFVideoTrack &videoTrack) const;
7774

@@ -91,7 +88,7 @@ namespace MPF { namespace COMPONENT {
9188

9289

9390
private:
94-
BlockingQueue<MPFFrame> frameQueue_;
91+
BlockingQueue<std::optional<MPFFrame>> frameQueue_;
9592

9693
// Fields for properties of the video that don't change as it is being read.
9794
// We can't just query the underlying video capture on the fly because it is being used by
@@ -108,7 +105,7 @@ namespace MPF { namespace COMPONENT {
108105

109106
MPFAsyncVideoCapture(MPFVideoCapture&& videoCapture, int frameQueueSize);
110107
};
111-
}}
108+
}
112109

113110

114111

detection/api/include/detectionComponentUtils.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ namespace DetectionComponentUtils {
9595
double NormalizeAngle(double angle);
9696

9797
bool RotationAnglesEqual(double a1, double a2, double epsilon = 0.1);
98+
99+
std::string GetAppDir(const char * argv0);
98100
}
99101

100102

detection/api/src/KeyFrameFilter.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* limitations under the License. *
2525
******************************************************************************/
2626

27+
#include <iostream>
2728
#include <thread>
2829
#include <cstdio>
2930

@@ -114,4 +115,4 @@ namespace MPF { namespace COMPONENT {
114115
keyFrames.shrink_to_fit();
115116
return keyFrames;
116117
}
117-
}}
118+
}}

detection/api/src/MPFAsyncVideoCapture.cpp

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -34,37 +34,28 @@
3434
using 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+
}

detection/api/src/detectionComponentUtils.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,20 @@
2424
* limitations under the License. *
2525
******************************************************************************/
2626

27+
#include "detectionComponentUtils.h"
28+
2729
#include <algorithm>
2830
#include <cctype>
2931
#include <cmath>
3032
#include <exception>
33+
#include <filesystem>
3134

3235
#include <opencv2/core.hpp>
3336

3437
#include "MPFInvalidPropertyException.h"
3538

36-
#include "detectionComponentUtils.h"
37-
3839

3940
using std::exception;
40-
using std::pair;
4141
using std::string;
4242

4343
using MPF::COMPONENT::MPFDetectionDataType;
@@ -135,5 +135,30 @@ namespace DetectionComponentUtils {
135135
return (a1_dist + a2_dist) < epsilon;
136136
}
137137
}
138+
139+
std::string GetAppDir(const char * const argv0) {
140+
namespace fs = std::filesystem;
141+
try {
142+
return fs::canonical("/proc/self/exe").parent_path();
143+
}
144+
catch (const fs::filesystem_error&) {
145+
}
146+
147+
try {
148+
if (fs::path argv0Path(argv0); argv0Path.has_parent_path()) {
149+
return argv0Path.parent_path();
150+
}
151+
}
152+
catch (const fs::filesystem_error&) {
153+
}
154+
155+
try {
156+
return fs::current_path();
157+
}
158+
catch (const fs::filesystem_error&) {
159+
}
160+
161+
return ".";
162+
}
138163
}
139164

detection/api/src/frame_transformers/AffineFrameTransformer.cpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,17 @@ namespace MPF { namespace COMPONENT {
4444
cv::Mat_<double> GetAllCorners(const std::vector<MPFRotatedRect> &regions) {
4545
// Matrix containing each region's 4 corners. First row is x coordinate and second row is y coordinate.
4646
cv::Mat_<double> corners(2, 4 * regions.size());
47-
auto xIter = corners.begin();
48-
auto yIter = corners.row(1).begin();
49-
47+
int cornerIdx = 0;
5048
for (const auto& region : regions) {
5149
for (const auto& corner : region.GetCorners()) {
52-
*(xIter++) = corner.x;
53-
*(yIter++) = corner.y;
50+
corners(0, cornerIdx) = corner.x;
51+
corners(1, cornerIdx) = corner.y;
52+
cornerIdx++;
5453
}
5554
}
5655
return corners;
5756
}
5857

59-
6058
cv::Rect2d GetMappedBoundingRect(
6159
const std::vector<MPFRotatedRect> &regions,
6260
const cv::Matx33d &frameRotMat) {

detection/api/src/frame_transformers/FrameTransformerFactory.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
#include "frame_transformers/FrameTransformerFactory.h"
2828

29+
#include <iostream>
2930
#include <map>
3031
#include <stdexcept>
3132
#include <string>

detection/api/test/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
cmake_minimum_required(VERSION 3.6)
2929

3030
project(detection-api-tests)
31-
set(CMAKE_CXX_STANDARD 11)
31+
set(CMAKE_CXX_STANDARD 17)
3232

3333
find_package(GTest)
3434
if (${GTEST_FOUND})
@@ -44,4 +44,4 @@ if (${GTEST_FOUND})
4444
file(GLOB TEST_IMAGES test_imgs/*)
4545
file(COPY ${TEST_IMAGES} DESTINATION test/test_imgs)
4646

47-
endif()
47+
endif()

0 commit comments

Comments
 (0)