Skip to content

Commit 5450940

Browse files
authored
demos/cpp: support OpenCV3 (#3513)
1 parent c067e22 commit 5450940

File tree

12 files changed

+102
-36
lines changed

12 files changed

+102
-36
lines changed

demos/CMakeLists.txt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,12 +137,16 @@ macro(add_demo)
137137
endif()
138138
endmacro()
139139

140-
find_package(OpenCV REQUIRED COMPONENTS core highgui videoio imgproc imgcodecs gapi)
140+
find_package(OpenCV REQUIRED COMPONENTS core highgui videoio imgproc imgcodecs)
141141
find_package(OpenVINO REQUIRED COMPONENTS Runtime)
142142

143143
add_subdirectory(thirdparty/gflags)
144144
add_subdirectory(common/cpp)
145-
add_subdirectory(common/cpp_gapi)
145+
# TODO: remove wrapping if after OpenCV3 is dropped
146+
if(OpenCV_VERSION VERSION_GREATER_EQUAL 4.5.3)
147+
find_package(OpenCV REQUIRED COMPONENTS gapi)
148+
add_subdirectory(common/cpp_gapi)
149+
endif()
146150
add_subdirectory(multi_channel_common/cpp)
147151

148152
# collect all samples subdirectories

demos/README.md

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ The Open Model Zoo demo applications are console applications that provide robus
8080
Source code of the demos can be obtained from the Open Model Zoo [GitHub repository](https://github.com/openvinotoolkit/open_model_zoo/).
8181

8282
```sh
83-
git clone https://github.com/openvinotoolkit/open_model_zoo.git
84-
cd open_model_zoo
85-
git submodule update --init --recursive
83+
git clone --recurse-submodules https://github.com/openvinotoolkit/open_model_zoo.git
8684
```
8785

8886
C++, C++ G-API and Python\* versions are located in the `cpp`, `cpp_gapi` and `python` subdirectories respectively.
@@ -166,11 +164,10 @@ You can download the [Intel pre-trained models](../models/intel/index.md) or [pu
166164

167165
## Build the Demo Applications
168166

169-
To build the demos, you need to source OpenVINO™ and OpenCV environment. You can install the OpenVINO™ toolkit using the installation package for [Intel® Distribution of OpenVINO™ toolkit](https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit-download.html) or build the open-source version available in the [OpenVINO GitHub repository](https://github.com/openvinotoolkit/openvino) using the [build instructions](https://github.com/openvinotoolkit/openvino/wiki/BuildingCode).
167+
To build the demos, you need to source OpenVINO™ environment and [get OpenCV](https://github.com/opencv/opencv/wiki/BuildOpenCV4OpenVINO). You can install the OpenVINO™ toolkit using the installation package for [Intel® Distribution of OpenVINO™ toolkit](https://www.intel.com/content/www/us/en/developer/tools/openvino-toolkit-download.html) or build the open-source version available in the [OpenVINO GitHub repository](https://github.com/openvinotoolkit/openvino) using the [build instructions](https://github.com/openvinotoolkit/openvino/wiki/BuildingCode).
170168
For the Intel® Distribution of OpenVINO™ toolkit installed to the `<INSTALL_DIR>` directory on your machine, run the following commands to download prebuilt OpenCV and set environment variables before building the demos:
171169

172170
```sh
173-
<INSTALL_DIR>/extras/scripts/download_opencv.sh
174171
source <INSTALL_DIR>/setupvars.sh
175172
```
176173

demos/common/cpp/monitors/include/monitors/presenter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Presenter {
2828
std::size_t historySize = 20);
2929
void addRemoveMonitor(MonitorType monitor);
3030
void handleKey(int key); // handles C, D, M, H keys
31-
void drawGraphs(const cv::Mat& frame);
31+
void drawGraphs(cv::Mat& frame);
3232
std::vector<std::string> reportMeans() const;
3333

3434
const int yPos;

demos/common/cpp/monitors/src/presenter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void Presenter::handleKey(int key) {
117117
}
118118
}
119119

120-
void Presenter::drawGraphs(const cv::Mat& frame) {
120+
void Presenter::drawGraphs(cv::Mat& frame) {
121121
const std::chrono::steady_clock::time_point curTimeStamp = std::chrono::steady_clock::now();
122122
if (curTimeStamp - prevTimeStamp >= std::chrono::milliseconds{1000}) {
123123
prevTimeStamp = curTimeStamp;

demos/common/cpp/utils/include/utils/ocv_common.hpp

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,16 @@ inline void putHighlightedText(const cv::Mat& frame,
150150
cv::putText(frame, message, position, fontFace, fontScale, color, thickness);
151151
}
152152

153+
// TODO: replace with Size::empty() after OpenCV3 is dropped
154+
static inline bool isSizeEmpty(const cv::Size& size) {
155+
return size.width <= 0 || size.height <= 0;
156+
}
157+
158+
// TODO: replace with Rect::empty() after OpenCV3 is dropped
159+
static inline bool isRectEmpty(const cv::Rect& rect) {
160+
return rect.width <= 0 || rect.height <= 0;
161+
}
162+
153163
class OutputTransform {
154164
public:
155165
OutputTransform() : doResize(false), scaleFactor(1) {}
@@ -240,7 +250,10 @@ class InputTransform {
240250
if (reverseInputChannels) {
241251
cv::cvtColor(result, result, cv::COLOR_BGR2RGB);
242252
}
243-
return (result - means) / stdScales;
253+
// TODO: merge the two following lines after OpenCV3 is droppped
254+
result -= means;
255+
result /= cv::Mat{stdScales};
256+
return result;
244257
}
245258

246259
private:
@@ -260,7 +273,7 @@ class LazyVideoWriter {
260273

261274
LazyVideoWriter(const std::string& filenames, double fps, unsigned lim) :
262275
nwritten{1}, filenames{filenames}, fps{fps}, lim{lim} {}
263-
void write(cv::InputArray im) {
276+
void write(const cv::Mat& im) {
264277
if (writer.isOpened() && (nwritten < lim || 0 == lim)) {
265278
writer.write(im);
266279
++nwritten;

demos/crossroad_camera_demo/cpp/main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,9 @@ int main(int argc, char* argv[]) {
179179

180180
bc_rect = bc_rect & person_rect;
181181

182-
if (!tc_rect.empty())
182+
if (!isRectEmpty(tc_rect))
183183
resPersAttrAndColor.top_color = PersonAttribsDetection::GetAvgColor(person(tc_rect));
184-
if (!bc_rect.empty())
184+
if (!isRectEmpty(bc_rect))
185185
resPersAttrAndColor.bottom_color = PersonAttribsDetection::GetAvgColor(person(bc_rect));
186186
}
187187
}

demos/mri_reconstruction_demo/cpp/main.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,9 @@ int main(int argc, char** argv) {
9191
cv::Mat kspace = cv::Mat(height, width, CV_64FC2, mri.data.ptr<double>(i)).clone();
9292

9393
kspace.setTo(0, mri.samplingMask);
94-
kspace = (kspace - cv::Scalar(mri.stats[0], mri.stats[0])) / cv::Scalar(mri.stats[1], mri.stats[1]);
94+
// TODO: merge the two following lines after OpenCV3 is droppped
95+
kspace -= cv::Scalar(mri.stats[0], mri.stats[0]);
96+
kspace /= cv::Mat{cv::Scalar(mri.stats[1], mri.stats[1])};
9597
kspace.reshape(1, 1).convertTo(inputBlob.reshape(1, 1), CV_32F);
9698
// Forward pass
9799
infReq.infer();
@@ -145,18 +147,18 @@ void callback(int sliceId, void* userdata) {
145147
cv::hconcat(std::vector<cv::Mat>({img, masked, rec}), render);
146148
cv::copyMakeBorder(render, render, kBorderSize, 0, 0, 0, cv::BORDER_CONSTANT, 255);
147149
cv::putText(render, "Original", cv::Point(0, 15), cv::FONT_HERSHEY_SIMPLEX, 0.5, 0);
148-
cv::putText(render, cv::format("Sampled (PSNR %.1f)", cv::PSNR(img, masked, 255)),
150+
cv::putText(render, cv::format("Sampled (PSNR %.1f)", cv::PSNR(img, masked)),
149151
cv::Point(width, 15), cv::FONT_HERSHEY_SIMPLEX, 0.5, 0);
150-
cv::putText(render, cv::format("Reconstructed (PSNR %.1f)", cv::PSNR(img, rec, 255)),
152+
cv::putText(render, cv::format("Reconstructed (PSNR %.1f)", cv::PSNR(img, rec)),
151153
cv::Point(width*2, 15), cv::FONT_HERSHEY_SIMPLEX, 0.5, 0);
152154

153155
cv::imshow(kWinName, render);
154156
cv::waitKey(1);
155157
}
156158

157159
cv::Mat kspaceToImage(const cv::Mat& kspace) {
158-
CV_CheckEQ(kspace.dims, 2, "");
159-
CV_CheckEQ(kspace.channels(), 2, "");
160+
assert(kspace.dims == 2);
161+
assert(kspace.channels() == 2);
160162

161163
cv::Mat img;
162164

demos/pedestrian_tracker_demo/cpp/include/cnn.hpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,9 @@ class VectorCNN : public BaseModel {
8585
public:
8686
VectorCNN(const ModelConfigTracker& config, const ov::Core& core, const std::string& deviceName);
8787

88-
void Compute(const cv::Mat& image, cv::Mat* vector, cv::Size outp_shape = cv::Size()) const;
88+
void Compute(const cv::Mat& image, cv::Mat* vector) const;
8989
void Compute(const std::vector<cv::Mat>& images,
90-
std::vector<cv::Mat>* vectors,
91-
cv::Size outp_shape = cv::Size()) const;
90+
std::vector<cv::Mat>* vectors) const;
9291

9392
int size() const {
9493
return result_size;

demos/pedestrian_tracker_demo/cpp/src/cnn.cpp

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -86,18 +86,18 @@ VectorCNN::VectorCNN(const Config& config, const ov::Core& core, const std::stri
8686
result_size = std::accumulate(std::next(output_shape.begin(), 1), output_shape.end(), 1, std::multiplies<int>());
8787
}
8888

89-
void VectorCNN::Compute(const cv::Mat& frame, cv::Mat* vector, cv::Size out_shape) const {
89+
void VectorCNN::Compute(const cv::Mat& frame, cv::Mat* vector) const {
9090
std::vector<cv::Mat> output;
91-
Compute({frame}, &output, out_shape);
91+
Compute({frame}, &output);
9292
*vector = output[0];
9393
}
9494

95-
void VectorCNN::Compute(const std::vector<cv::Mat>& images, std::vector<cv::Mat>* vectors, cv::Size out_shape) const {
95+
void VectorCNN::Compute(const std::vector<cv::Mat>& images, std::vector<cv::Mat>* vectors) const {
9696
if (images.empty()) {
9797
return;
9898
}
9999
vectors->clear();
100-
auto results_fetcher = [vectors, out_shape](const ov::Tensor& tensor, size_t batch_size) {
100+
auto results_fetcher = [vectors](const ov::Tensor& tensor, size_t batch_size) {
101101
ov::Shape shape = tensor.get_shape();
102102
std::vector<int> tensor_sizes(shape.size(), 0);
103103
for (size_t i = 0; i < tensor_sizes.size(); ++i) {
@@ -110,9 +110,6 @@ void VectorCNN::Compute(const std::vector<cv::Mat>& images, std::vector<cv::Mat>
110110
CV_32F,
111111
reinterpret_cast<void*>((out_tensor.ptr<float>(0) + b * out_tensor.size[1])));
112112
vectors->emplace_back();
113-
if (out_shape != cv::Size()) {
114-
tensor_wrapper = tensor_wrapper.reshape(1, {out_shape.height, out_shape.width});
115-
}
116113
tensor_wrapper.copyTo(vectors->back());
117114
}
118115
};

demos/pedestrian_tracker_demo/cpp/src/tracker.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ bool PedestrianTracker::EraseTrackIfBBoxIsOutOfFrame(size_t track_id) {
337337
if (tracks_.find(track_id) == tracks_.end())
338338
return true;
339339
auto c = Center(tracks_.at(track_id).predicted_rect);
340-
if (!prev_frame_size_.empty() &&
340+
if (!isSizeEmpty(prev_frame_size_) &&
341341
(c.x < 0 || c.y < 0 || c.x > prev_frame_size_.width || c.y > prev_frame_size_.height)) {
342342
tracks_.at(track_id).lost = params_.forget_delay + 1;
343343
for (auto id : active_track_ids()) {

0 commit comments

Comments
 (0)