1
+ // ================================================================
2
+ // Created by Gregory Kramida on 9/23/19.
3
+ // Copyright (c) 2019 Gregory Kramida
4
+ // Licensed under the Apache License, Version 2.0 (the "License");
5
+ // you may not use this file except in compliance with the License.
6
+ // You may obtain a copy of the License at
7
+
8
+ // http://www.apache.org/licenses/LICENSE-2.0
9
+
10
+ // Unless required by applicable law or agreed to in writing, software
11
+ // distributed under the License is distributed on an "AS IS" BASIS,
12
+ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ // See the License for the specific language governing permissions and
14
+ // limitations under the License.
15
+ // ================================================================
16
+
17
+ #include " GenericFileFetch.h"
18
+ #include " common/OpenCV_CrossPlatform.h"
19
+
20
+ #include < opencv2/core.hpp>
21
+ #include < boost/filesystem/operations.hpp>
22
+ #include < exception>
23
+ #include < regex>
24
+ #include < iterator>
25
+ #include < common/logging.h>
26
+
27
+
28
+ namespace fs = boost::filesystem;
29
+
30
+ surfelwarp::GenericFileFetch::GenericFileFetch (const fs::path& data_path, std::string extension, bool force_no_masks) :
31
+ m_mask_buffer_ix(SIZE_MAX), m_use_masks(false )
32
+ {
33
+ std::vector<path> sorted_paths;
34
+ if (extension.length () > 0 && extension[0 ] != ' .' ) {
35
+ extension = " ." + extension;
36
+ }
37
+ std::copy (fs::directory_iterator (data_path), fs::directory_iterator (), std::back_inserter (sorted_paths));
38
+ std::sort (sorted_paths.begin (), sorted_paths.end ());
39
+ bool unexpected_mask_frame_number = false ;
40
+ for (auto & path : sorted_paths) {
41
+ if (FilenameIndicatesDepthImage (path.filename ().string (), extension)) {
42
+ int frame_number = GetFrameNumber (path);
43
+ if (frame_number != m_depth_image_paths.size ()) {
44
+ throw std::runtime_error (" Unexpected depth frame number encountered" );
45
+ }
46
+ m_depth_image_paths.push_back (path);
47
+ } else if (FilenameIndicatesRGBImage (path.filename ().string (), extension)) {
48
+ int frame_number = GetFrameNumber (path);
49
+ if (frame_number != m_rgb_image_paths.size ()) {
50
+ throw std::runtime_error (" Unexpected RGB frame number encountered" );
51
+ }
52
+ m_rgb_image_paths.push_back (path);
53
+ } else if (!force_no_masks && FilenameIndicatesMaskImage (path.filename ().string (), extension)) {
54
+ int frame_number = GetFrameNumber (path);
55
+ if (frame_number != m_mask_image_paths.size ()) {
56
+ unexpected_mask_frame_number = true ;
57
+ }
58
+ m_mask_image_paths.push_back (path);
59
+ }
60
+ }
61
+
62
+ if (m_depth_image_paths.size () != m_rgb_image_paths.size ()) {
63
+ LOG (FATAL) << " Presumed depth image count (" << m_depth_image_paths.size () <<
64
+ " ) doesn't equal presumed rgb image count." << m_rgb_image_paths.size ();
65
+ }
66
+
67
+ if (!force_no_masks) {
68
+ if (unexpected_mask_frame_number) {
69
+ LOG (WARNING)
70
+ << " Warning: inconsistent mask frame numbers encountered in the filenames. Proceeding without masks." ;
71
+ } else if (!m_mask_image_paths.empty ()) {
72
+ if (m_depth_image_paths.size () != m_mask_image_paths.size () && !m_mask_image_paths.empty ()) {
73
+ LOG (WARNING)
74
+ << " Warning: seems like there were some mask image files, but their number doesn't match the "
75
+ " number of depth frames. Proceeding without masks." ;
76
+ } else {
77
+ m_use_masks = true ;
78
+ }
79
+ }
80
+ }
81
+ }
82
+
83
+ bool surfelwarp::GenericFileFetch::HasSubstringFromSet (const std::string& string, const std::string* set, int set_size)
84
+ {
85
+ bool found_indicator = false ;
86
+ for (int i_target_string = 0 ; i_target_string < set_size; i_target_string++) {
87
+ if (string.find (set[i_target_string]) != std::string::npos) {
88
+ return true ;
89
+ }
90
+ }
91
+ return false ;
92
+ }
93
+
94
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesDepthImage (const path& filename, const std::string& valid_extension)
95
+ {
96
+ if (filename.extension () != valid_extension) return false ;
97
+ const std::array<std::string, 3 > possible_depth_indicators = {" depth" , " DEPTH" , " Depth" };
98
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
99
+ }
100
+
101
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesRGBImage (const path& filename, const std::string& valid_extension)
102
+ {
103
+ if (filename.extension () != valid_extension) return false ;
104
+ const std::array<std::string, 5 > possible_depth_indicators = {" color" , " COLOR" , " Color" , " rgb" , " RGB" };
105
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
106
+ }
107
+
108
+
109
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesMaskImage (const surfelwarp::GenericFileFetch::path& filename,
110
+ const std::string& valid_extension)
111
+ {
112
+ if (filename.extension () != valid_extension) return false ;
113
+ const std::array<std::string, 3 > possible_depth_indicators = {" mask" , " Mask" , " MASK" };
114
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
115
+ }
116
+
117
+ int surfelwarp::GenericFileFetch::GetFrameNumber (const surfelwarp::GenericFileFetch::path& filename)
118
+ {
119
+ const std::regex digits_regex (" \\ d+" );
120
+ std::smatch match_result;
121
+ const std::string filename_stem = filename.stem ().string ();
122
+ if (!std::regex_search (filename_stem, match_result, digits_regex)) {
123
+ throw std::runtime_error (" Could not find frame number in filename." );
124
+ };
125
+ return std::stoi (match_result.str (0 ));
126
+ }
127
+
128
+ void surfelwarp::GenericFileFetch::FetchDepthImage (size_t frame_idx, cv::Mat& depth_img)
129
+ {
130
+ path file_path = this ->m_depth_image_paths [frame_idx];
131
+ // Read the image
132
+ depth_img = cv::imread (file_path.string (), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
133
+
134
+ if (this ->m_use_masks ) {
135
+ mask_mutex.lock ();
136
+ // Apply mask to image
137
+ if (this ->m_mask_buffer_ix != frame_idx) {
138
+ m_mask_image_buffer = cv::imread (this ->m_mask_image_paths [frame_idx].string (),
139
+ CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
140
+ m_mask_buffer_ix = frame_idx;
141
+ }
142
+ cv::Mat masked;
143
+ depth_img.copyTo (masked, m_mask_image_buffer);
144
+ mask_mutex.unlock ();
145
+ depth_img = masked;
146
+ }
147
+ }
148
+
149
+ void surfelwarp::GenericFileFetch::FetchDepthImage (size_t frame_idx, void * depth_img)
150
+ {
151
+
152
+ }
153
+
154
+ void surfelwarp::GenericFileFetch::FetchRGBImage (size_t frame_idx, cv::Mat& rgb_img)
155
+ {
156
+ path file_path = this ->m_rgb_image_paths [frame_idx];
157
+ // Read the image
158
+ rgb_img = cv::imread (file_path.string (), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
159
+ if (this ->m_use_masks ) {
160
+ mask_mutex.lock ();
161
+ if (this ->m_mask_buffer_ix != frame_idx) {
162
+ m_mask_image_buffer = cv::imread (this ->m_mask_image_paths [frame_idx].string (),
163
+ CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
164
+ m_mask_buffer_ix = frame_idx;
165
+ }
166
+ cv::Mat masked;
167
+ rgb_img.copyTo (masked, m_mask_image_buffer);
168
+ mask_mutex.unlock ();
169
+ rgb_img = masked;
170
+ }
171
+ }
172
+
173
+ void surfelwarp::GenericFileFetch::FetchRGBImage (size_t frame_idx, void * rgb_img)
174
+ {
175
+
176
+ }
0 commit comments