1 /** 2 * Copyright 2020 Huawei Technologies Co., Ltd 3 * 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 #ifndef MINDSPORE_LITE_MINDDATA_WRAPPER_ALBUM_OP_ANDROID_H_ 17 #define MINDSPORE_LITE_MINDDATA_WRAPPER_ALBUM_OP_ANDROID_H_ 18 19 #include <deque> 20 #include <memory> 21 #include <queue> 22 #include <string> 23 #include <algorithm> 24 #include <map> 25 #include <set> 26 #include <utility> 27 #include <vector> 28 #include <unordered_map> 29 #include "minddata/dataset/core/tensor.h" 30 31 #include "minddata/dataset/engine/data_schema.h" 32 #include "minddata/dataset/util/path.h" 33 #include "minddata/dataset/util/status.h" 34 35 namespace mindspore { 36 namespace dataset { 37 // Forward declares 38 template <typename T> 39 class Queue; 40 41 // Define row information as a list of file objects to read 42 using FolderImages = std::shared_ptr<std::pair<std::string, std::queue<std::string>>>; 43 44 /// \class AlbumOp 45 class AlbumOp { 46 public: 47 /// \brief Constructor 48 /// \param[in] file_dir - directory of Album 49 /// \param[in] do_decode - decode image files 50 /// \param[in] schema_file - schema file 51 /// \param[in] column_names - column name 52 /// \param[in] exts - set of file extensions to read, if empty, read everything under the dir 53 AlbumOp(const std::string &file_dir, bool do_decode, const std::string &schema_file, 54 const std::vector<std::string> &column_names, const std::set<std::string> &exts); 55 56 /// \brief Constructor 57 /// \param[in] file_dir - directory of Album 58 /// \param[in] do_decode - decode image files 59 /// \param[in] schema_file - schema file 60 /// \param[in] column_names - column name 61 /// \param[in] exts - set of file extensions to read, if empty, read everything under the dir 62 /// \param[in] index - the specific file index 63 AlbumOp(const std::string &file_dir, bool do_decode, const std::string &schema_file, 64 const std::vector<std::string> &column_names, const std::set<std::string> &exts, uint32_t index); 65 66 /// \brief Destructor. 67 ~AlbumOp() = default; 68 69 /// \brief Initialize AlbumOp related var, calls the function to walk all files 70 /// \return - The error code returned 71 Status PrescanEntry(); 72 73 /// \brief Initialize AlbumOp related var, calls the function to walk all files 74 /// \return - The error code returned 75 bool GetNextRow(std::unordered_map<std::string, std::shared_ptr<Tensor>> *map_row); 76 77 /// \brief Check if image ia valid.Only support JPEG/PNG/GIF/BMP 78 /// This function could be optimized to return the tensor to reduce open/closing files 79 /// \return bool - if file is bad then return false 80 bool CheckImageType(const std::string &file_name, bool *valid); 81 82 /// \brief Name of the current Op 83 /// @return op name Name()84 std::string Name() const { return "AlbumOp"; } 85 86 /// \brief disable rotate 87 // @return DisableRotate()88 void DisableRotate() { this->rotate_ = false; } 89 90 private: 91 /// \brief Load image to tensor 92 /// \param[in] image_file Image name of file 93 /// \param[in] col_num Column num in schema 94 /// \param[in,out] Tensor to push to 95 /// \return Status The error code returned 96 Status LoadImageTensor(const std::string &image_file, int32_t col_num, TensorPtr *tensor); 97 98 /// \brief Load vector of ints to tensor, append tensor to tensor 99 /// \param[in] json_obj Json object containing multi-dimensional label 100 /// \param[in] col_num Column num in schema 101 /// \param[in,out] Tensor to push to 102 /// \return Status The error code returned 103 Status LoadIntArrayTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 104 105 /// \brief Load vector of floatss to tensor, append tensor to tensor 106 /// \param[in] json_obj Json object containing array data 107 /// \param[in] col_num Column num in schema 108 /// \param[in,out] Tensor to push to 109 /// \return Status The error code returned 110 Status LoadFloatArrayTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 111 112 /// \brief Load string array into a tensor, append tensor to tensor 113 /// \param[in] json_obj Json object containing string tensor 114 /// \param[in] col_num Column num in schema 115 /// \param[in,out] Tensor to push to 116 /// \return Status The error code returned 117 Status LoadStringArrayTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 118 119 /// \brief Load string into a tensor, append tensor to tensor 120 /// \param[in] json_obj Json object containing string tensor 121 /// \param[in] col_num Column num in schema 122 /// \param[in,out] Tensor to push to 123 /// \return Status The error code returned 124 Status LoadStringTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 125 126 /// \brief Load float value to tensor 127 /// \param[in] json_obj Json object containing float 128 /// \param[in] col_num Column num in schema 129 /// \param[in,out] Tensor to push to 130 /// \return Status The error code returned 131 Status LoadFloatTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 132 133 /// \brief Load int value to tensor 134 /// \param[in] json_obj Json object containing int 135 /// \param[in] col_num Column num in schema 136 /// \param[in,out] Tensor to push to 137 /// \return Status The error code returned 138 Status LoadIntTensor(const nlohmann::json &json_obj, int32_t col_num, TensorPtr *tensor); 139 140 /// \brief Load empty tensor to tensor 141 /// \param[in] col_num Column num in schema 142 /// \param[in,out] Tensor to push to 143 /// \return Status The error code returned 144 Status LoadEmptyTensor(int32_t col_num, TensorPtr *tensor); 145 146 /// \brief Load id from file name to tensor 147 /// \param[in] file The file name to get ID from 148 /// \param[in] col_num Column num in schema 149 /// \param[in,out] Tensor to push to 150 /// \return Status The error code returned 151 Status LoadIDTensor(const std::string &file, int32_t col_num, TensorPtr *tensor); 152 153 /// \brief Load a tensor according to a json file 154 /// \param[in] row_id_type row_id - id for this tensor row 155 /// \param[in] ImageColumns file Json file location 156 /// \param[in,out] TensorRow Json content stored into a tensor row 157 /// \return Status The error code returned 158 Status LoadTensorRow(row_id_type row_id, const std::string &file, 159 std::unordered_map<std::string, std::shared_ptr<Tensor>> *map_row); 160 161 /// \brief get image exif orientation 162 /// \param[in] file file path 163 int GetOrientation(const std::string &file); 164 165 /// \brief is read column name 166 /// \param[in] column_name 167 bool IsReadColumn(const std::string &column_name); 168 169 Status LoadTensorRowByIndex(int index, const std::string &file, const nlohmann::json &js, 170 std::unordered_map<std::string, std::shared_ptr<Tensor>> *map_row); 171 172 Status LoadIntTensorRowByIndex(int index, bool is_array, const nlohmann::json &column_value, 173 std::unordered_map<std::string, std::shared_ptr<Tensor>> *map_row); 174 175 std::string folder_path_; // directory of image folder 176 bool decode_; 177 std::vector<std::string> columns_to_load_; 178 std::set<std::string> extensions_; // extensions allowed 179 std::unique_ptr<DataSchema> data_schema_; 180 std::string schema_file_; 181 int64_t row_cnt_; 182 int64_t current_cnt_; 183 int64_t buf_cnt_; 184 int64_t dirname_offset_; 185 bool sampler_; 186 int64_t sampler_index_; 187 std::vector<std::string> image_rows_; 188 bool rotate_; 189 std::vector<std::string> column_names_; 190 }; 191 } // namespace dataset 192 } // namespace mindspore 193 #endif // MINDSPORE_LITE_MINDDATA_WRAPPER_ALBUM_OP_ANDROID_H_ 194