1
1
// Copyright (C) 2020 Intel Corporation
2
2
// SPDX-License-Identifier: Apache-2.0
3
3
//
4
+ #include " utils/images_capture.h"
4
5
5
- #include < utils/images_capture .h>
6
+ #include < string .h> // for size_t, strcmp
6
7
7
8
#ifdef _WIN32
8
- #include " w_dirent.hpp"
9
+ # include " w_dirent.hpp"
9
10
#else
10
- #include < dirent.h>
11
+ # include < dirent.h> // for closedir, dirent, opendir, readdir, DIR
11
12
#endif
12
13
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
14
21
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...
19
24
20
25
class InvalidInput : public std ::runtime_error {
21
26
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) {}
24
28
};
25
29
26
30
class OpenError : public std ::runtime_error {
27
31
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) {}
30
33
};
31
34
32
35
class ImreadWrapper : public ImagesCapture {
33
36
cv::Mat img;
34
37
bool canRead;
35
38
36
39
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 } {
38
41
auto startTime = std::chrono::steady_clock::now ();
39
42
40
43
std::ifstream file (input.c_str ());
41
44
if (!file.good ())
42
45
throw InvalidInput (" Can't find the image by " + input);
43
46
44
47
img = cv::imread (input);
45
- if (!img.data )
48
+ if (!img.data )
46
49
throw OpenError (" Can't open the image from " + input);
47
50
else
48
51
readerMetrics.update (startTime);
49
52
}
50
53
51
- double fps () const override {return 1.0 ;}
54
+ double fps () const override {
55
+ return 1.0 ;
56
+ }
52
57
53
- std::string getType () const override {return " IMAGE" ;}
58
+ std::string getType () const override {
59
+ return " IMAGE" ;
60
+ }
54
61
55
62
cv::Mat read () override {
56
- if (loop) return img.clone ();
63
+ if (loop)
64
+ return img.clone ();
57
65
if (canRead) {
58
66
canRead = false ;
59
67
return img.clone ();
@@ -71,12 +79,17 @@ class DirReader : public ImagesCapture {
71
79
const std::string input;
72
80
73
81
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 ());
77
90
if (!dir)
78
91
throw InvalidInput (" Can't find the dir by " + input);
79
- while (struct dirent * ent = readdir (dir))
92
+ while (struct dirent * ent = readdir (dir))
80
93
if (strcmp (ent->d_name , " ." ) && strcmp (ent->d_name , " .." ))
81
94
names.emplace_back (ent->d_name );
82
95
closedir (dir);
@@ -88,16 +101,21 @@ class DirReader : public ImagesCapture {
88
101
cv::Mat img = cv::imread (input + ' /' + names[fileId]);
89
102
if (img.data ) {
90
103
++readImgs;
91
- if (readImgs - 1 >= initialImageId) return ;
104
+ if (readImgs - 1 >= initialImageId)
105
+ return ;
92
106
}
93
107
++fileId;
94
108
}
95
109
throw OpenError (" Can't read the first image from " + input);
96
110
}
97
111
98
- double fps () const override {return 1.0 ;}
112
+ double fps () const override {
113
+ return 1.0 ;
114
+ }
99
115
100
- std::string getType () const override {return " DIR" ;}
116
+ std::string getType () const override {
117
+ return " DIR" ;
118
+ }
101
119
102
120
cv::Mat read () override {
103
121
auto startTime = std::chrono::steady_clock::now ();
@@ -141,8 +159,12 @@ class VideoCapWrapper : public ImagesCapture {
141
159
size_t readLengthLimit;
142
160
143
161
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)} {
146
168
if (0 == readLengthLimit) {
147
169
throw std::runtime_error (" readLengthLimit must be positive" );
148
170
}
@@ -155,9 +177,13 @@ class VideoCapWrapper : public ImagesCapture {
155
177
throw InvalidInput (" Can't open the video from " + input);
156
178
}
157
179
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
+ }
159
183
160
- std::string getType () const override {return " VIDEO" ;}
184
+ std::string getType () const override {
185
+ return " VIDEO" ;
186
+ }
161
187
162
188
cv::Mat read () override {
163
189
auto startTime = std::chrono::steady_clock::now ();
@@ -202,9 +228,14 @@ class CameraCapWrapper : public ImagesCapture {
202
228
size_t readLengthLimit;
203
229
204
230
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 } {
208
239
if (0 == readLengthLimit) {
209
240
throw std::runtime_error (" readLengthLimit must be positive" );
210
241
}
@@ -219,14 +250,18 @@ class CameraCapWrapper : public ImagesCapture {
219
250
return ;
220
251
}
221
252
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); }
225
256
}
226
257
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
+ }
228
261
229
- std::string getType () const override {return " CAMERA" ;}
262
+ std::string getType () const override {
263
+ return " CAMERA" ;
264
+ }
230
265
231
266
cv::Mat read () override {
232
267
auto startTime = std::chrono::steady_clock::now ();
@@ -248,29 +283,43 @@ class CameraCapWrapper : public ImagesCapture {
248
283
}
249
284
};
250
285
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" };
254
294
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
+ }
258
300
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
+ }
262
306
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
+ }
266
312
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
+ }
270
319
271
320
std::vector<std::string> errorMessages = openErrors.empty () ? invalidInputs : openErrors;
272
321
std::string errorsInfo;
273
- for (const auto & message: errorMessages) {
322
+ for (const auto & message : errorMessages) {
274
323
errorsInfo.append (message + " \n " );
275
324
}
276
325
throw std::runtime_error (errorsInfo);
0 commit comments