You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_helper.cc 5.1 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * Copyright 2020-2021 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. #include "minddata/dataset/util/json_helper.h"
  17. #include <nlohmann/json.hpp>
  18. #include "minddata/dataset/util/log_adapter.h"
  19. #include "minddata/dataset/util/path.h"
  20. #include "minddata/dataset/util/status.h"
  21. namespace mindspore {
  22. namespace dataset {
  23. // Create a numbered json file from image folder
  24. Status JsonHelper::CreateAlbum(const std::string &in_dir, const std::string &out_dir) {
  25. // in check
  26. Path base_dir = Path(in_dir);
  27. RETURN_IF_NOT_OK(RealPath(in_dir));
  28. if (!base_dir.IsDirectory() || !base_dir.Exists()) {
  29. RETURN_STATUS_UNEXPECTED("Input dir is not a directory or doesn't exist");
  30. }
  31. // check if output_dir exists and create it if it does not exist
  32. Path target_dir = Path(out_dir);
  33. RETURN_IF_NOT_OK(target_dir.CreateDirectory());
  34. // iterate over in dir and create json for all images
  35. uint64_t index = 0;
  36. auto dir_it = Path::DirIterator::OpenDirectory(&base_dir);
  37. RETURN_UNEXPECTED_IF_NULL(dir_it);
  38. while (dir_it->HasNext()) {
  39. Path v = dir_it->Next();
  40. // check if found file fits image extension
  41. // create json file in output dir with the path
  42. std::string out_file = out_dir + "/" + std::to_string(index) + ".json";
  43. RETURN_IF_NOT_OK(UpdateValue(out_file, "image", v.ToString(), out_file));
  44. index++;
  45. }
  46. return Status::OK();
  47. }
  48. Status JsonHelper::RealPath(const std::string &path) {
  49. std::string real_path;
  50. RETURN_IF_NOT_OK(Path::RealPath(path, real_path));
  51. return Status::OK();
  52. }
  53. // A print method typically used for debugging
  54. void JsonHelper::Print(std::ostream &out) const {
  55. out << " Data Helper"
  56. << "\n";
  57. }
  58. Status JsonHelper::UpdateArray(const std::string &in_file, const std::string &key,
  59. const std::vector<std::string> &value, const std::string &out_file) {
  60. try {
  61. Path in = Path(in_file);
  62. nlohmann::json js;
  63. if (in.Exists()) {
  64. RETURN_IF_NOT_OK(RealPath(in_file));
  65. std::ifstream in_stream(in_file);
  66. try {
  67. MS_LOG(INFO) << "Filename: " << in_file << ".";
  68. in_stream >> js;
  69. } catch (const std::exception &err) {
  70. in_stream.close();
  71. RETURN_STATUS_UNEXPECTED("Invalid file, failed to open json file: " + in_file +
  72. ", please delete it and try again!");
  73. }
  74. in_stream.close();
  75. }
  76. js[key] = value;
  77. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  78. if (out_file == "") {
  79. std::ofstream o(in_file, std::ofstream::trunc);
  80. o << js;
  81. o.close();
  82. } else {
  83. std::ofstream o(out_file, std::ofstream::trunc);
  84. o << js;
  85. o.close();
  86. }
  87. }
  88. // Catch any exception and convert to Status return code
  89. catch (const std::exception &err) {
  90. RETURN_STATUS_UNEXPECTED("Update json failed ");
  91. }
  92. return Status::OK();
  93. }
  94. Status JsonHelper::RemoveKey(const std::string &in_file, const std::string &key, const std::string &out_file) {
  95. try {
  96. Path in = Path(in_file);
  97. nlohmann::json js;
  98. if (in.Exists()) {
  99. RETURN_IF_NOT_OK(RealPath(in_file));
  100. std::ifstream in_stream(in_file);
  101. try {
  102. MS_LOG(INFO) << "Filename: " << in_file << ".";
  103. in_stream >> js;
  104. } catch (const std::exception &err) {
  105. in_stream.close();
  106. RETURN_STATUS_UNEXPECTED("Invalid file, failed to open json file: " + in_file +
  107. ", please delete it and try again!");
  108. }
  109. in_stream.close();
  110. }
  111. (void)js.erase(key);
  112. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  113. if (out_file == "") {
  114. std::ofstream o(in_file, std::ofstream::trunc);
  115. o << js;
  116. o.close();
  117. } else {
  118. std::ofstream o(out_file, std::ofstream::trunc);
  119. o << js;
  120. o.close();
  121. }
  122. }
  123. // Catch any exception and convert to Status return code
  124. catch (const std::exception &err) {
  125. RETURN_STATUS_UNEXPECTED("Update json failed ");
  126. }
  127. return Status::OK();
  128. }
  129. size_t JsonHelper::DumpData(const unsigned char *tensor_addr, const size_t &tensor_size, void *addr,
  130. const size_t &buffer_size) {
  131. // write to address, input order is: destination, source
  132. errno_t ret = memcpy_s(addr, buffer_size, tensor_addr, tensor_size);
  133. if (ret != 0) {
  134. // memcpy failed
  135. MS_LOG(ERROR) << "memcpy tensor memory failed"
  136. << ".";
  137. return 0; // amount of data copied is 0, error
  138. }
  139. return tensor_size;
  140. }
  141. } // namespace dataset
  142. } // namespace mindspore