Skip to content

Commit cb00ac4

Browse files
authored
OpenCV4 upgrade with CUDA (#174)
1 parent 49119ad commit cb00ac4

File tree

34 files changed

+213
-83
lines changed

34 files changed

+213
-83
lines changed

cpp/DarknetDetection/CMakeLists.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ set(CMAKE_CXX_STANDARD 11)
3131

3232
include(../ComponentSetup.cmake)
3333

34-
find_package(OpenCV 3.4.7 EXACT REQUIRED PATHS /opt/opencv-3.4.7 COMPONENTS opencv_core opencv_highgui)
34+
find_package(OpenCV 4.5.0 EXACT REQUIRED PATHS /opt/opencv-4.5.0 COMPONENTS opencv_core opencv_highgui)
3535

3636

3737
find_package(mpfComponentInterface REQUIRED)
@@ -42,7 +42,7 @@ find_package(Qt4 REQUIRED)
4242
get_plugin_build_location(DarknetDetection pluginLocation)
4343

4444
if (DEFINED ENV{MPF_DISABLE_CUDA_BUILD})
45-
set(DARKNET_BUILD_CUDA OFF)
45+
set(DARKNET_BUILD_CUDA OFF)
4646
message(
4747
"The CUDA version of Darknet will not be built because the MPF_DISABLE_CUDA_BUILD environment variable was set.")
4848
else()
@@ -86,4 +86,4 @@ endif()
8686
add_executable(sample_darknet_detector sample_darknet_detector.cpp)
8787
target_link_libraries(sample_darknet_detector mpfDarknetDetection)
8888

89-
add_subdirectory(test)
89+
add_subdirectory(test)

cpp/DarknetDetection/Dockerfile

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,9 @@ RUN mkdir /weights && curl --location https://pjreddie.com/media/files/yolov3.we
3636
# Build Component
3737
FROM ${BUILD_REGISTRY}openmpf_cpp_component_build:${BUILD_TAG} as build_component
3838

39-
RUN yum-config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64 \
40-
&& VERSION=9-0-9.0.176-1 \
41-
&& yum install -y --nogpgcheck cuda-minimal-build-$VERSION cuda-cublas-dev-$VERSION cuda-curand-dev-$VERSION \
39+
RUN yum install --nogpgcheck -y cuda-curand-dev-10-2 \
4240
&& yum clean all \
43-
&& rm -rf /var/cache/yum/* \
44-
&& ln -s cuda-9.0 /usr/local/cuda \
45-
&& echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/cuda.conf
41+
&& rm -rf /var/cache/yum/*;
4642

4743
COPY . .
4844

@@ -53,18 +49,7 @@ RUN if [ "${RUN_TESTS,,}" == true ]; then cd $BUILD_DIR/test && ./DarknetDetecti
5349

5450

5551
####################################################
56-
FROM ${BUILD_REGISTRY}openmpf_cpp_executor:${BUILD_TAG}
57-
58-
RUN yum-config-manager --add-repo http://developer.download.nvidia.com/compute/cuda/repos/rhel7/x86_64 \
59-
&& yum install -y --nogpgcheck cuda-cudart-9-0-9.0.176-1 \
60-
&& yum clean all \
61-
&& rm -rf /var/cache/yum/* \
62-
&& ln -s cuda-9.0 /usr/local/cuda \
63-
&& echo '/usr/local/cuda/lib64' >> /etc/ld.so.conf.d/cuda.conf
64-
65-
# Environment variables required by nvidia runtime.
66-
ENV NVIDIA_VISIBLE_DEVICES all
67-
ENV NVIDIA_DRIVER_CAPABILITIES compute,utility
52+
FROM ${BUILD_REGISTRY}openmpf_cpp_executor:${BUILD_TAG} as darknet_component
6853

6954
COPY --from=download_weights /weights $PLUGINS_DIR/DarknetDetection/models
7055

@@ -83,3 +68,21 @@ LABEL org.label-schema.license="Apache 2.0" \
8368
org.label-schema.vcs-url="https://github.com/openmpf/openmpf-components" \
8469
org.label-schema.vendor="MITRE"
8570

71+
72+
####################################################
73+
FROM darknet_component as sample_executable
74+
75+
COPY --from=build_component $BUILD_DIR/sample_darknet_detector /opt/mpf/
76+
77+
COPY --from=build_component $BUILD_DIR/test/data /opt/mpf/test-data
78+
79+
RUN ln -s /opt/mpf/plugins /opt/mpf/plugin
80+
81+
ENV LD_LIBRARY_PATH $PLUGINS_DIR/DarknetDetection/lib
82+
83+
ENTRYPOINT ["/opt/mpf/sample_darknet_detector"]
84+
85+
86+
####################################################
87+
# Set final image when no --target specified.
88+
FROM darknet_component

cpp/DarknetDetection/darknet_lib/CMakeLists.txt

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,6 @@ set(DARKNET_LIB_SRC_FILES
7878
src/yolo_layer.c
7979
src/iseg_layer.c)
8080

81-
find_package(OpenCV 3.4.7 EXACT REQUIRED PATHS /opt/opencv-3.4.7 COMPONENTS opencv_core opencv_highgui)
82-
8381
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
8482

8583
# Darknet fails to compile when all optimizations are disabled, which is the default for debug builds.
@@ -91,9 +89,8 @@ else()
9189
endif()
9290

9391
add_library(darknet_lib ${DARKNET_LIB_SRC_FILES})
94-
target_compile_definitions(darknet_lib PUBLIC -DOPENCV)
9592
target_include_directories(darknet_lib PUBLIC include)
96-
target_link_libraries(darknet_lib m pthread ${OpenCV_LIBS})
93+
target_link_libraries(darknet_lib m pthread)
9794

9895

9996
if (DARKNET_BUILD_CUDA)
@@ -125,9 +122,9 @@ if (DARKNET_BUILD_CUDA)
125122

126123
CUDA_ADD_LIBRARY(darknet_lib_cuda ${DARKNET_LIB_SRC_FILES} ${DARKNET_CUDA_SRC_FILES})
127124
CUDA_ADD_CUBLAS_TO_TARGET(darknet_lib_cuda)
128-
target_compile_definitions(darknet_lib_cuda PUBLIC -DOPENCV -DGPU)
125+
target_compile_definitions(darknet_lib_cuda PUBLIC -DGPU)
129126

130127
target_include_directories(darknet_lib_cuda PUBLIC include ${CUDA_INCLUDE_DIRS})
131-
target_link_libraries(darknet_lib_cuda m pthread ${OpenCV_LIBS} ${CUDA_curand_LIBRARY})
128+
target_link_libraries(darknet_lib_cuda m pthread ${CUDA_curand_LIBRARY})
132129
endif()
133130

cpp/DarknetDetection/darknet_wrapper/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ project(darknet-wrapper)
3030
set(CMAKE_CXX_STANDARD 11)
3131

3232

33-
find_package(OpenCV 3.4.7 EXACT REQUIRED PATHS /opt/opencv-3.4.7 COMPONENTS opencv_core opencv_highgui)
33+
find_package(OpenCV 4.5.0 EXACT REQUIRED PATHS /opt/opencv-4.5.0 COMPONENTS opencv_core opencv_highgui)
3434

3535
find_package(mpfComponentInterface REQUIRED)
3636
find_package(mpfDetectionComponentApi REQUIRED)

cpp/DarknetDetection/sample_darknet_detector.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#include <QCoreApplication>
2929
#include <iomanip>
3030
#include <chrono>
31+
#include <opencv2/core/cuda.hpp>
32+
3133

3234
#include "DarknetDetection.h"
3335

@@ -37,9 +39,26 @@ using namespace MPF::COMPONENT;
3739
void print_tracks(const std::vector<MPFVideoTrack> &tracks);
3840

3941
int main(int argc, char* argv[]) {
42+
if (argc == 2) {
43+
std::string arg1 = argv[1];
44+
if (arg1 == "gpu-info") {
45+
int cuda_device_count = cv::cuda::getCudaEnabledDeviceCount();
46+
std::cout << "Cuda device count: " << cuda_device_count << std::endl;
47+
if (cuda_device_count > 0) {
48+
for (int i = 0; i < cuda_device_count; i++) {
49+
std::cout << "==== Device #" << i << " ====" << std::endl;
50+
cv::cuda::printCudaDeviceInfo(i);
51+
std::cout << "=================================" << std::endl;
52+
}
53+
}
54+
return EXIT_SUCCESS;
55+
}
56+
}
57+
4058
if (argc < 3) {
4159
std::cout << "Usage: " << argv[0] << " <uri> <model_name> [gpu_index]" << std::endl;
4260
std::cout << "Usage: " << argv[0] << " <uri> <model_name> <start_frame> <end_frame> [gpu_index] [queue_capacity]" << std::endl;
61+
std::cout << "Usage: " << argv[0] << " gpu-info" << std::endl;
4362
return 1;
4463
}
4564

cpp/DarknetDetection/test/test_darknet_detection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727

2828
#include <gtest/gtest.h>
29-
#include <opencv2/imgcodecs.hpp>
30-
#include <opencv/cv.hpp>
29+
30+
#include <opencv2/core.hpp>
3131

3232
#include <MPFDetectionComponent.h>
3333
#include <MPFVideoCapture.h>

cpp/DlibFaceDetection/CMakeLists.txt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,14 @@ project(dlib-face-detection)
3131
set(CMAKE_CXX_STANDARD 11)
3232

3333
set(USE_AVX_INSTRUCTIONS ON)
34-
set(DLIB_DIR /apps/source/dlib-sources/dlib-18.18/dlib)
34+
set(DLIB_USE_CUDA OFF)
35+
set(DLIB_DIR /apps/source/dlib-sources/dlib-19.20/dlib)
3536
include(${DLIB_DIR}/cmake)
3637
set_property(TARGET dlib PROPERTY POSITION_INDEPENDENT_CODE ON)
3738

3839
include(../ComponentSetup.cmake)
3940

40-
find_package(OpenCV 3.4.7 EXACT REQUIRED PATHS /opt/opencv-3.4.7 COMPONENTS opencv_highgui)
41+
find_package(OpenCV 4.5.0 EXACT REQUIRED PATHS /opt/opencv-4.5.0 COMPONENTS opencv_highgui)
4142
find_package(mpfComponentInterface REQUIRED)
4243
find_package(mpfDetectionComponentApi REQUIRED)
4344
find_package(mpfComponentUtils REQUIRED)
@@ -56,4 +57,4 @@ configure_mpf_component(DlibFaceDetection TARGETS mpfDlibFaceDetection)
5657
add_subdirectory(test)
5758

5859
add_executable(sample_dlib_face_detector sample_dlib_face_detector.cpp)
59-
target_link_libraries(sample_dlib_face_detector mpfDlibFaceDetection)
60+
target_link_libraries(sample_dlib_face_detector mpfDlibFaceDetection)

cpp/DlibFaceDetection/DlibFaceDetection.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ vector<MPFImageLocation> DlibFaceDetection::GetDetectionsFromImageData(const MPF
667667
if (verbosity > 0) {
668668
// Draw a rectangle onto the input image for each detection
669669
if (imshow_on) {
670-
namedWindow("original image", CV_WINDOW_AUTOSIZE);
670+
namedWindow("original image", cv::WINDOW_AUTOSIZE);
671671
imshow("original image", image);
672672
waitKey(5);
673673
}
@@ -679,7 +679,7 @@ vector<MPFImageLocation> DlibFaceDetection::GetDetectionsFromImageData(const MPF
679679
cv::rectangle(image, object, CV_RGB(0, 0, 0), 2);
680680
}
681681
if (imshow_on) {
682-
namedWindow("new image", CV_WINDOW_AUTOSIZE);
682+
namedWindow("new image", cv::WINDOW_AUTOSIZE);
683683
imshow("new image", image);
684684
//0 waits indefinitely for input, which could cause problems when run as a component
685685
waitKey(5);

cpp/DlibFaceDetection/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ RUN yum install --assumeyes bzip2 \
3535
&& rm --recursive /var/cache/yum/*
3636

3737
RUN mkdir --parents /apps/source/dlib-sources; \
38-
curl --location 'http://dlib.net/files/dlib-18.18.tar.bz2' \
38+
curl --location 'http://dlib.net/files/dlib-19.20.tar.bz2' \
3939
| tar --extract --bzip2 --directory /apps/source/dlib-sources
4040

4141

cpp/KeywordTagging/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ message("Package in ${PACKAGE_DIR}")
3535
find_package(mpfComponentInterface REQUIRED)
3636
find_package(mpfDetectionComponentApi REQUIRED)
3737
find_package(mpfComponentUtils REQUIRED)
38-
find_package(OpenCV 3.4.7 EXACT REQUIRED PATHS /opt/opencv-3.4.7 COMPONENTS opencv_core)
38+
find_package(OpenCV 4.5.0 EXACT REQUIRED PATHS /opt/opencv-4.5.0 COMPONENTS opencv_core)
39+
3940
find_package(Qt4 REQUIRED)
4041

4142
find_package(Boost 1.53.0 COMPONENTS regex locale filesystem)

0 commit comments

Comments
 (0)