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)
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_depth_image_paths.size () != m_mask_image_paths.size () && !m_mask_image_paths.empty ()) {
72
+ LOG (WARNING) << " Warning: seems like there were some mask image files, but their number doesn't match the "
73
+ " number of depth frames. Proceeding without masks." ;
74
+ } else {
75
+ m_use_masks = true ;
76
+ }
77
+ }
78
+ }
79
+
80
+ bool surfelwarp::GenericFileFetch::HasSubstringFromSet (const std::string& string, const std::string* set, int set_size)
81
+ {
82
+ bool found_indicator = false ;
83
+ for (int i_target_string = 0 ; i_target_string < set_size; i_target_string++) {
84
+ if (string.find (set[i_target_string]) != std::string::npos) {
85
+ return true ;
86
+ }
87
+ }
88
+ return false ;
89
+ }
90
+
91
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesDepthImage (const path& filename, const std::string& valid_extension)
92
+ {
93
+ if (filename.extension () != valid_extension) return false ;
94
+ const std::array<std::string, 3 > possible_depth_indicators = {" depth" , " DEPTH" , " Depth" };
95
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
96
+ }
97
+
98
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesRGBImage (const path& filename, const std::string& valid_extension)
99
+ {
100
+ if (filename.extension () != valid_extension) return false ;
101
+ const std::array<std::string, 5 > possible_depth_indicators = {" color" , " COLOR" , " Color" , " rgb" , " RGB" };
102
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
103
+ }
104
+
105
+
106
+ bool surfelwarp::GenericFileFetch::FilenameIndicatesMaskImage (const surfelwarp::GenericFileFetch::path& filename,
107
+ const std::string& valid_extension)
108
+ {
109
+ if (filename.extension () != valid_extension) return false ;
110
+ const std::array<std::string, 3 > possible_depth_indicators = {" mask" , " Mask" , " MASK" };
111
+ return HasSubstringFromSet (filename.string (), possible_depth_indicators.data (), possible_depth_indicators.size ());
112
+ }
113
+
114
+ int surfelwarp::GenericFileFetch::GetFrameNumber (const surfelwarp::GenericFileFetch::path& filename)
115
+ {
116
+ const std::regex digits_regex (" \\ d+" );
117
+ std::smatch match_result;
118
+ const std::string filename_stem = filename.stem ().string ();
119
+ if (!std::regex_search (filename_stem, match_result, digits_regex)) {
120
+ throw std::runtime_error (" Could not find frame number in filename." );
121
+ };
122
+ return std::stoi (match_result.str (0 ));
123
+ }
124
+
125
+ void surfelwarp::GenericFileFetch::FetchDepthImage (size_t frame_idx, cv::Mat& depth_img)
126
+ {
127
+ path file_path = this ->m_depth_image_paths [frame_idx];
128
+ // Read the image
129
+ depth_img = cv::imread (file_path.string (), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
130
+
131
+ if (this ->m_use_masks ) {
132
+ mask_mutex.lock ();
133
+ // Apply mask to image
134
+ if (this ->m_mask_buffer_ix != frame_idx) {
135
+ m_mask_image_buffer = cv::imread (this ->m_mask_image_paths [frame_idx].string (),
136
+ CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
137
+ m_mask_buffer_ix = frame_idx;
138
+ }
139
+ cv::Mat masked;
140
+ depth_img.copyTo (masked,m_mask_image_buffer);
141
+ mask_mutex.unlock ();
142
+ depth_img = masked;
143
+ }
144
+ }
145
+
146
+ void surfelwarp::GenericFileFetch::FetchDepthImage (size_t frame_idx, void * depth_img)
147
+ {
148
+
149
+ }
150
+
151
+ void surfelwarp::GenericFileFetch::FetchRGBImage (size_t frame_idx, cv::Mat& rgb_img)
152
+ {
153
+ path file_path = this ->m_rgb_image_paths [frame_idx];
154
+ // Read the image
155
+ rgb_img = cv::imread (file_path.string (), CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
156
+ if (this ->m_use_masks ) {
157
+ mask_mutex.lock ();
158
+ if (this ->m_mask_buffer_ix != frame_idx) {
159
+ m_mask_image_buffer = cv::imread (this ->m_mask_image_paths [frame_idx].string (),
160
+ CV_ANYCOLOR | CV_ANYDEPTH); // NOLINT(hicpp-signed-bitwise)
161
+ m_mask_buffer_ix = frame_idx;
162
+ }
163
+ cv::Mat masked;
164
+ rgb_img.copyTo (masked,m_mask_image_buffer);
165
+ mask_mutex.unlock ();
166
+ rgb_img = masked;
167
+ }
168
+ }
169
+
170
+ void surfelwarp::GenericFileFetch::FetchRGBImage (size_t frame_idx, void * rgb_img)
171
+ {
172
+
173
+ }
0 commit comments