Skip to content

Commit

Permalink
Merge pull request #588 from frankier/caffe-0.17-opencv4
Browse files Browse the repository at this point in the history
Backport Caffe PR to NvCaffe 0.17 to support OpenCV4
  • Loading branch information
drnikolaev authored Jan 8, 2021
2 parents 17e347e + 8f90b2b commit 4867dc2
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 17 deletions.
16 changes: 14 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,9 +215,17 @@ endif
ifeq ($(USE_OPENCV), 1)
LIBRARIES += opencv_core opencv_highgui opencv_imgproc

ifeq ($(OPENCV_VERSION), 3)
ifeq ($(OPENCV_VERSION), $(filter $(OPENCV_VERSION), 3 4))
LIBRARIES += opencv_imgcodecs opencv_videoio
endif
ifeq ($(OPENCV_VERSION), 4)
ifeq ($(USE_PKG_CONFIG), 1)
INCLUDE_DIRS += $(shell pkg-config opencv4 --cflags-only-I | sed 's/-I//g')
else
INCLUDE_DIRS += /usr/include/opencv4 /usr/local/include/opencv4
INCLUDE_DIRS += /usr/include/opencv4/opencv /usr/local/include/opencv4/opencv
endif
endif

endif

Expand Down Expand Up @@ -447,7 +455,11 @@ LINKFLAGS += -pthread -fPIC $(COMMON_FLAGS) $(WARNINGS)

USE_PKG_CONFIG ?= 0
ifeq ($(USE_PKG_CONFIG), 1)
PKG_CONFIG := $(shell pkg-config opencv --libs)
ifeq ($(OPENCV_VERSION), 4)
PKG_CONFIG := $(shell pkg-config opencv4 --libs)
else
PKG_CONFIG := $(shell pkg-config opencv --libs)
endif
else
PKG_CONFIG :=
endif
Expand Down
2 changes: 1 addition & 1 deletion Makefile.config.example
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# USE_LEVELDB := 0
# USE_LMDB := 0

# Uncomment if you're using OpenCV 3
# Uncomment and set accordingly if you're using OpenCV 3/4
# OPENCV_VERSION := 3

# To customize your choice of compiler, uncomment and set the following.
Expand Down
4 changes: 2 additions & 2 deletions src/caffe/layers/video_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ void VideoDataLayer<Ftype, Btype>::DataLayerSetUp(
if (!cap_.open(video_file)) {
LOG(FATAL) << "Failed to open video: " << video_file;
}
total_frames_ = cap_.get(CV_CAP_PROP_FRAME_COUNT);
total_frames_ = cap_.get(cv::CAP_PROP_FRAME_COUNT);
processed_frames_ = 0;
// Read image to infer shape.
cap_ >> cv_img;
// Set index back to the first frame.
cap_.set(CV_CAP_PROP_POS_FRAMES, 0);
cap_.set(cv::CAP_PROP_POS_FRAMES, 0);
} else {
LOG(FATAL) << "Unknow video type!";
}
Expand Down
2 changes: 1 addition & 1 deletion src/caffe/layers/window_data_layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ bool WindowDataLayer<Ftype, Btype>::load_batch(Batch* batch,
image_database_cache_[window[WindowDataLayer<Ftype, Btype>::IMAGE_INDEX]];
DecodeDatumToCVMat(image_cached.second, true, cv_img, false);
} else {
cv_img = cv::imread(image.first, CV_LOAD_IMAGE_COLOR);
cv_img = cv::imread(image.first, cv::IMREAD_COLOR);
if (!cv_img.data) {
LOG(ERROR) << "Could not open or find file " << image.first;
return true;
Expand Down
4 changes: 2 additions & 2 deletions src/caffe/test/test_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class IOTest : public ::testing::Test {};
bool ReadImageToDatumReference(const string& filename, const int label,
const int height, const int width, const bool is_color, Datum* datum) {
cv::Mat cv_img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
int cv_read_flag = (is_color ? cv::IMREAD_COLOR :
cv::IMREAD_GRAYSCALE);

cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag);
if (!cv_img_origin.data) {
Expand Down
6 changes: 3 additions & 3 deletions src/caffe/util/bbox_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2236,7 +2236,7 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob* detections,
&baseline);
cv::rectangle(image, cv::Point(0, 0),
cv::Point(text.width, text.height + baseline),
CV_RGB(255, 255, 255), CV_FILLED);
CV_RGB(255, 255, 255), cv::FILLED);
cv::putText(image, buffer, cv::Point(0, text.height + baseline / 2.),
fontface, scale, CV_RGB(0, 0, 0), thickness, 8);
// Draw bboxes.
Expand All @@ -2262,7 +2262,7 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob* detections,
cv::rectangle(
image, bottom_left_pt + cv::Point(0, 0),
bottom_left_pt + cv::Point(text.width, -text.height-baseline),
color, CV_FILLED);
color, cv::FILLED);
cv::putText(image, buffer, bottom_left_pt - cv::Point(0, baseline),
fontface, scale, CV_RGB(0, 0, 0), thickness, 8);
}
Expand All @@ -2271,7 +2271,7 @@ void VisualizeBBox(const vector<cv::Mat>& images, const Blob* detections,
if (!save_file.empty()) {
if (!cap_out.isOpened()) {
cv::Size size(image.size().width, image.size().height);
cv::VideoWriter outputVideo(save_file, CV_FOURCC('D', 'I', 'V', 'X'),
cv::VideoWriter outputVideo(save_file, cv::VideoWriter::fourcc('D', 'I', 'V', 'X'),
30, size, true);
cap_out = outputVideo;
}
Expand Down
7 changes: 6 additions & 1 deletion src/caffe/util/im_transforms.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <opencv2/highgui/highgui.hpp>

#if CV_VERSION_MAJOR == 3
#if CV_VERSION_MAJOR == 3 || CV_VERSION_MAJOR == 4
#include <opencv2/imgcodecs/imgcodecs.hpp>
#define CV_GRAY2BGR cv::COLOR_GRAY2BGR
#define CV_BGR2GRAY cv::COLOR_BGR2GRAY
Expand All @@ -11,6 +11,11 @@
#define CV_THRESH_BINARY_INV cv::THRESH_BINARY_INV
#define CV_THRESH_OTSU cv::THRESH_OTSU
#endif
#if CV_VERSION_MAJOR == 4
#define CV_BGR2HSV cv::COLOR_BGR2HSV
#define CV_HSV2BGR cv::COLOR_HSV2BGR
#define CV_BGR2Lab cv::COLOR_BGR2Lab
#endif

#include <algorithm>
#include <numeric>
Expand Down
10 changes: 5 additions & 5 deletions src/caffe/util/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ cv::Mat ReadImageToCVMat(const string& filename,
}
if (cv_img_origin.data) {
if (is_color && cv_img_origin.channels() < 3) {
cv::cvtColor(cv_img_origin, cv_img, CV_GRAY2RGB);
cv::cvtColor(cv_img_origin, cv_img, cv::COLOR_GRAY2RGB);
}
if (short_side > 0) {
if (cv_img_origin.rows > cv_img_origin.cols) {
Expand All @@ -212,10 +212,10 @@ cv::Mat ReadImageToCVMat(const string& filename,
}
cv::Size sz(width, height);
if (cv_img.data) {
cv::resize(cv_img, cv_img_origin, sz, 0., 0., CV_INTER_LINEAR);
cv::resize(cv_img, cv_img_origin, sz, 0., 0., cv::INTER_LINEAR);
return cv_img_origin;
}
cv::resize(cv_img_origin, cv_img, sz, 0., 0., CV_INTER_LINEAR);
cv::resize(cv_img_origin, cv_img, sz, 0., 0., cv::INTER_LINEAR);
} else {
LOG(ERROR) << "Could not decode file " << filename;
}
Expand All @@ -226,8 +226,8 @@ cv::Mat ReadImageToCVMat(const string& filename, const int height,
const int width, const int min_dim, const int max_dim,
const bool is_color) {
cv::Mat cv_img;
int cv_read_flag = (is_color ? CV_LOAD_IMAGE_COLOR :
CV_LOAD_IMAGE_GRAYSCALE);
int cv_read_flag = (is_color ? cv::IMREAD_COLOR :
cv::IMREAD_GRAYSCALE);
cv::Mat cv_img_origin = cv::imread(filename, cv_read_flag);
if (!cv_img_origin.data) {
LOG(ERROR) << "Could not open or find file " << filename;
Expand Down

0 comments on commit 4867dc2

Please sign in to comment.