Skip to content

Commit 302c578

Browse files
committed
fix common images capture to make it build
1 parent e192838 commit 302c578

File tree

2 files changed

+123
-64
lines changed

2 files changed

+123
-64
lines changed

demos/common/cpp/utils/include/utils/images_capture.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
// SPDX-License-Identifier: Apache-2.0
33
//
44

5-
#include <memory>
6-
#include <string>
5+
#pragma once
6+
#include <stddef.h> // for size_t
77

8-
#include <opencv2/core/mat.hpp>
9-
#include <opencv2/videoio.hpp>
10-
#include "utils/performance_metrics.hpp"
8+
#include <limits> // for numeric_limits
9+
#include <memory> // for unique_ptr
10+
#include <string> // for string
1111

12-
enum class read_type {efficient, safe};
12+
#include <opencv2/core.hpp> // for Mat, Size
13+
14+
#include "utils/performance_metrics.hpp" // for PerformanceMetrics
15+
16+
enum class read_type { efficient, safe };
1317

1418
class ImagesCapture {
1519
public:
@@ -19,7 +23,9 @@ class ImagesCapture {
1923
virtual double fps() const = 0;
2024
virtual cv::Mat read() = 0;
2125
virtual std::string getType() const = 0;
22-
const PerformanceMetrics& getMetrics() { return readerMetrics; }
26+
const PerformanceMetrics& getMetrics() {
27+
return readerMetrics;
28+
}
2329
virtual ~ImagesCapture() = default;
2430

2531
protected:
@@ -34,9 +40,13 @@ class ImagesCapture {
3440
// } catch (const std::out_of_range&) {
3541
// return cv::VideoCapture(input);
3642
// }
37-
// Some VideoCapture backends continue owning the video buffer under cv::Mat. safe_copy forses to return a copy from read()
43+
// Some VideoCapture backends continue owning the video buffer under cv::Mat. safe_copy forses to return a copy from
44+
// read()
3845
// https://github.com/opencv/opencv/blob/46e1560678dba83d25d309d8fbce01c40f21b7be/modules/gapi/include/opencv2/gapi/streaming/cap.hpp#L72-L76
39-
std::unique_ptr<ImagesCapture> openImagesCapture(const std::string &input,
40-
bool loop, read_type type=read_type::efficient, size_t initialImageId=0,
41-
size_t readLengthLimit=std::numeric_limits<size_t>::max(), // General option
42-
cv::Size cameraResolution={1280, 720});
46+
std::unique_ptr<ImagesCapture> openImagesCapture(
47+
const std::string& input,
48+
bool loop,
49+
read_type type = read_type::efficient,
50+
size_t initialImageId = 0,
51+
size_t readLengthLimit = std::numeric_limits<size_t>::max(), // General option
52+
cv::Size cameraResolution = {1280, 720});

demos/common/cpp/utils/src/images_capture.cpp

Lines changed: 101 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,67 @@
11
// Copyright (C) 2020 Intel Corporation
22
// SPDX-License-Identifier: Apache-2.0
33
//
4+
#include "utils/images_capture.h"
45

5-
#include <utils/images_capture.h>
6+
#include <string.h> // for size_t, strcmp
67

78
#ifdef _WIN32
8-
#include "w_dirent.hpp"
9+
# include "w_dirent.hpp"
910
#else
10-
#include <dirent.h>
11+
# include <dirent.h> // for closedir, dirent, opendir, readdir, DIR
1112
#endif
1213

13-
#include <opencv2/imgcodecs.hpp>
14+
#include <algorithm> // for max, sort
15+
#include <chrono> // for steady_clock
16+
#include <fstream> // for ifstream
17+
#include <memory> // for allocator, unique_ptr
18+
#include <stdexcept> // for runtime_error, invalid_argument, out_of_range
19+
#include <string> // for string, operator+, operator<, basic_string, char_traits, swap, stoi
20+
#include <vector> // for vector
1421

15-
#include <stdexcept>
16-
#include <string>
17-
#include <memory>
18-
#include <fstream>
22+
#include <opencv2/imgcodecs.hpp> // for imread
23+
#include <opencv2/videoio.hpp> // for VideoCapture, CAP_PROP_FPS, CAP_PROP_POS_FRAMES, CAP_PROP_AUTOFOCUS, CAP_PR...
1924

2025
class InvalidInput : public std::runtime_error {
2126
public:
22-
explicit InvalidInput(const std::string& message) noexcept
23-
: std::runtime_error(message) {}
27+
explicit InvalidInput(const std::string& message) noexcept : std::runtime_error(message) {}
2428
};
2529

2630
class OpenError : public std::runtime_error {
2731
public:
28-
explicit OpenError(const std::string& message) noexcept
29-
: std::runtime_error(message) {}
32+
explicit OpenError(const std::string& message) noexcept : std::runtime_error(message) {}
3033
};
3134

3235
class ImreadWrapper : public ImagesCapture {
3336
cv::Mat img;
3437
bool canRead;
3538

3639
public:
37-
ImreadWrapper(const std::string &input, bool loop) : ImagesCapture{loop}, canRead{true} {
40+
ImreadWrapper(const std::string& input, bool loop) : ImagesCapture{loop}, canRead{true} {
3841
auto startTime = std::chrono::steady_clock::now();
3942

4043
std::ifstream file(input.c_str());
4144
if (!file.good())
4245
throw InvalidInput("Can't find the image by " + input);
4346

4447
img = cv::imread(input);
45-
if(!img.data)
48+
if (!img.data)
4649
throw OpenError("Can't open the image from " + input);
4750
else
4851
readerMetrics.update(startTime);
4952
}
5053

51-
double fps() const override {return 1.0;}
54+
double fps() const override {
55+
return 1.0;
56+
}
5257

53-
std::string getType() const override {return "IMAGE";}
58+
std::string getType() const override {
59+
return "IMAGE";
60+
}
5461

5562
cv::Mat read() override {
56-
if (loop) return img.clone();
63+
if (loop)
64+
return img.clone();
5765
if (canRead) {
5866
canRead = false;
5967
return img.clone();
@@ -71,12 +79,17 @@ class DirReader : public ImagesCapture {
7179
const std::string input;
7280

7381
public:
74-
DirReader(const std::string &input, bool loop, size_t initialImageId, size_t readLengthLimit) : ImagesCapture{loop},
75-
fileId{0}, nextImgId{0}, initialImageId{initialImageId}, readLengthLimit{readLengthLimit}, input{input} {
76-
DIR *dir = opendir(input.c_str());
82+
DirReader(const std::string& input, bool loop, size_t initialImageId, size_t readLengthLimit)
83+
: ImagesCapture{loop},
84+
fileId{0},
85+
nextImgId{0},
86+
initialImageId{initialImageId},
87+
readLengthLimit{readLengthLimit},
88+
input{input} {
89+
DIR* dir = opendir(input.c_str());
7790
if (!dir)
7891
throw InvalidInput("Can't find the dir by " + input);
79-
while (struct dirent *ent = readdir(dir))
92+
while (struct dirent* ent = readdir(dir))
8093
if (strcmp(ent->d_name, ".") && strcmp(ent->d_name, ".."))
8194
names.emplace_back(ent->d_name);
8295
closedir(dir);
@@ -88,16 +101,21 @@ class DirReader : public ImagesCapture {
88101
cv::Mat img = cv::imread(input + '/' + names[fileId]);
89102
if (img.data) {
90103
++readImgs;
91-
if (readImgs - 1 >= initialImageId) return;
104+
if (readImgs - 1 >= initialImageId)
105+
return;
92106
}
93107
++fileId;
94108
}
95109
throw OpenError("Can't read the first image from " + input);
96110
}
97111

98-
double fps() const override {return 1.0;}
112+
double fps() const override {
113+
return 1.0;
114+
}
99115

100-
std::string getType() const override {return "DIR";}
116+
std::string getType() const override {
117+
return "DIR";
118+
}
101119

102120
cv::Mat read() override {
103121
auto startTime = std::chrono::steady_clock::now();
@@ -141,8 +159,12 @@ class VideoCapWrapper : public ImagesCapture {
141159
size_t readLengthLimit;
142160

143161
public:
144-
VideoCapWrapper(const std::string &input, bool loop, read_type type, size_t initialImageId, size_t readLengthLimit)
145-
: ImagesCapture{loop}, first_read{true}, type{type}, nextImgId{0}, initialImageId{static_cast<double>(initialImageId)} {
162+
VideoCapWrapper(const std::string& input, bool loop, read_type type, size_t initialImageId, size_t readLengthLimit)
163+
: ImagesCapture{loop},
164+
first_read{true},
165+
type{type},
166+
nextImgId{0},
167+
initialImageId{static_cast<double>(initialImageId)} {
146168
if (0 == readLengthLimit) {
147169
throw std::runtime_error("readLengthLimit must be positive");
148170
}
@@ -155,9 +177,13 @@ class VideoCapWrapper : public ImagesCapture {
155177
throw InvalidInput("Can't open the video from " + input);
156178
}
157179

158-
double fps() const override {return cap.get(cv::CAP_PROP_FPS);}
180+
double fps() const override {
181+
return cap.get(cv::CAP_PROP_FPS);
182+
}
159183

160-
std::string getType() const override {return "VIDEO";}
184+
std::string getType() const override {
185+
return "VIDEO";
186+
}
161187

162188
cv::Mat read() override {
163189
auto startTime = std::chrono::steady_clock::now();
@@ -202,9 +228,14 @@ class CameraCapWrapper : public ImagesCapture {
202228
size_t readLengthLimit;
203229

204230
public:
205-
CameraCapWrapper(const std::string &input, bool loop, read_type type,
206-
size_t readLengthLimit, cv::Size cameraResolution)
207-
: ImagesCapture{loop}, type{type}, nextImgId{0} {
231+
CameraCapWrapper(const std::string& input,
232+
bool loop,
233+
read_type type,
234+
size_t readLengthLimit,
235+
cv::Size cameraResolution)
236+
: ImagesCapture{loop},
237+
type{type},
238+
nextImgId{0} {
208239
if (0 == readLengthLimit) {
209240
throw std::runtime_error("readLengthLimit must be positive");
210241
}
@@ -219,14 +250,18 @@ class CameraCapWrapper : public ImagesCapture {
219250
return;
220251
}
221252
throw OpenError("Can't open the camera from " + input);
222-
}
223-
catch (const std::invalid_argument&) { throw InvalidInput("Can't find the camera " + input); }
224-
catch (const std::out_of_range&) { throw InvalidInput("Can't find the camera " + input); }
253+
} catch (const std::invalid_argument&) {
254+
throw InvalidInput("Can't find the camera " + input);
255+
} catch (const std::out_of_range&) { throw InvalidInput("Can't find the camera " + input); }
225256
}
226257

227-
double fps() const override {return cap.get(cv::CAP_PROP_FPS) > 0 ? cap.get(cv::CAP_PROP_FPS) : 30;}
258+
double fps() const override {
259+
return cap.get(cv::CAP_PROP_FPS) > 0 ? cap.get(cv::CAP_PROP_FPS) : 30;
260+
}
228261

229-
std::string getType() const override {return "CAMERA";}
262+
std::string getType() const override {
263+
return "CAMERA";
264+
}
230265

231266
cv::Mat read() override {
232267
auto startTime = std::chrono::steady_clock::now();
@@ -248,29 +283,43 @@ class CameraCapWrapper : public ImagesCapture {
248283
}
249284
};
250285

251-
std::unique_ptr<ImagesCapture> openImagesCapture(const std::string &input, bool loop,
252-
read_type type, size_t initialImageId, size_t readLengthLimit, cv::Size cameraResolution) {
253-
if (readLengthLimit == 0) throw std::runtime_error{"Read length limit must be positive"};
286+
std::unique_ptr<ImagesCapture> openImagesCapture(const std::string& input,
287+
bool loop,
288+
read_type type,
289+
size_t initialImageId,
290+
size_t readLengthLimit,
291+
cv::Size cameraResolution) {
292+
if (readLengthLimit == 0)
293+
throw std::runtime_error{"Read length limit must be positive"};
254294
std::vector<std::string> invalidInputs, openErrors;
255-
try { return std::unique_ptr<ImagesCapture>(new ImreadWrapper{input, loop}); }
256-
catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); }
257-
catch (const OpenError& e) { openErrors.push_back(e.what()); }
295+
try {
296+
return std::unique_ptr<ImagesCapture>(new ImreadWrapper{input, loop});
297+
} catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); } catch (const OpenError& e) {
298+
openErrors.push_back(e.what());
299+
}
258300

259-
try { return std::unique_ptr<ImagesCapture>(new DirReader{input, loop, initialImageId, readLengthLimit}); }
260-
catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); }
261-
catch (const OpenError& e) { openErrors.push_back(e.what()); }
301+
try {
302+
return std::unique_ptr<ImagesCapture>(new DirReader{input, loop, initialImageId, readLengthLimit});
303+
} catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); } catch (const OpenError& e) {
304+
openErrors.push_back(e.what());
305+
}
262306

263-
try { return std::unique_ptr<ImagesCapture>(new VideoCapWrapper{input, loop, type, initialImageId, readLengthLimit}); }
264-
catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); }
265-
catch (const OpenError& e) { openErrors.push_back(e.what()); }
307+
try {
308+
return std::unique_ptr<ImagesCapture>(new VideoCapWrapper{input, loop, type, initialImageId, readLengthLimit});
309+
} catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); } catch (const OpenError& e) {
310+
openErrors.push_back(e.what());
311+
}
266312

267-
try { return std::unique_ptr<ImagesCapture>(new CameraCapWrapper{input, loop, type, readLengthLimit, cameraResolution}); }
268-
catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); }
269-
catch (const OpenError& e) { openErrors.push_back(e.what()); }
313+
try {
314+
return std::unique_ptr<ImagesCapture>(
315+
new CameraCapWrapper{input, loop, type, readLengthLimit, cameraResolution});
316+
} catch (const InvalidInput& e) { invalidInputs.push_back(e.what()); } catch (const OpenError& e) {
317+
openErrors.push_back(e.what());
318+
}
270319

271320
std::vector<std::string> errorMessages = openErrors.empty() ? invalidInputs : openErrors;
272321
std::string errorsInfo;
273-
for (const auto& message: errorMessages) {
322+
for (const auto& message : errorMessages) {
274323
errorsInfo.append(message + "\n");
275324
}
276325
throw std::runtime_error(errorsInfo);

0 commit comments

Comments
 (0)