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.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. while (dir_it->HasNext()) {
  38. Path v = dir_it->Next();
  39. // check if found file fits image extension
  40. // create json file in output dir with the path
  41. std::string out_file = out_dir + "/" + std::to_string(index) + ".json";
  42. RETURN_IF_NOT_OK(UpdateValue(out_file, "image", v.ToString(), out_file));
  43. index++;
  44. }
  45. return Status::OK();
  46. }
  47. Status JsonHelper::RealPath(const std::string &path) {
  48. std::string real_path;
  49. RETURN_IF_NOT_OK(Path::RealPath(path, real_path));
  50. return Status::OK();
  51. }
  52. // A print method typically used for debugging
  53. void JsonHelper::Print(std::ostream &out) const {
  54. out << " Data Helper"
  55. << "\n";
  56. }
  57. Status JsonHelper::UpdateArray(const std::string &in_file, const std::string &key,
  58. const std::vector<std::string> &value, const std::string &out_file) {
  59. try {
  60. Path in = Path(in_file);
  61. nlohmann::json js;
  62. if (in.Exists()) {
  63. RETURN_IF_NOT_OK(RealPath(in_file));
  64. try {
  65. std::ifstream in_stream(in_file);
  66. MS_LOG(INFO) << "Filename: " << in_file << ".";
  67. in_stream >> js;
  68. in_stream.close();
  69. } catch (const std::exception &err) {
  70. RETURN_STATUS_UNEXPECTED("Invalid file, failed to open json file: " + in_file +
  71. ", please delete it and try again!");
  72. }
  73. }
  74. js[key] = value;
  75. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  76. if (out_file == "") {
  77. std::ofstream o(in_file, std::ofstream::trunc);
  78. o << js;
  79. o.close();
  80. } else {
  81. std::ofstream o(out_file, std::ofstream::trunc);
  82. o << js;
  83. o.close();
  84. }
  85. }
  86. // Catch any exception and convert to Status return code
  87. catch (const std::exception &err) {
  88. RETURN_STATUS_UNEXPECTED("Update json failed ");
  89. }
  90. return Status::OK();
  91. }
  92. Status JsonHelper::RemoveKey(const std::string &in_file, const std::string &key, const std::string &out_file) {
  93. try {
  94. Path in = Path(in_file);
  95. nlohmann::json js;
  96. if (in.Exists()) {
  97. RETURN_IF_NOT_OK(RealPath(in_file));
  98. try {
  99. std::ifstream in_stream(in_file);
  100. MS_LOG(INFO) << "Filename: " << in_file << ".";
  101. in_stream >> js;
  102. in_stream.close();
  103. } catch (const std::exception &err) {
  104. RETURN_STATUS_UNEXPECTED("Invalid file, failed to open json file: " + in_file +
  105. ", please delete it and try again!");
  106. }
  107. }
  108. (void)js.erase(key);
  109. MS_LOG(INFO) << "Write outfile is: " << js << ".";
  110. if (out_file == "") {
  111. std::ofstream o(in_file, std::ofstream::trunc);
  112. o << js;
  113. o.close();
  114. } else {
  115. std::ofstream o(out_file, std::ofstream::trunc);
  116. o << js;
  117. o.close();
  118. }
  119. }
  120. // Catch any exception and convert to Status return code
  121. catch (const std::exception &err) {
  122. RETURN_STATUS_UNEXPECTED("Update json failed ");
  123. }
  124. return Status::OK();
  125. }
  126. size_t JsonHelper::DumpData(const unsigned char *tensor_addr, const size_t &tensor_size, void *addr,
  127. const size_t &buffer_size) {
  128. // write to address, input order is: destination, source
  129. errno_t ret = memcpy_s(addr, buffer_size, tensor_addr, tensor_size);
  130. if (ret != 0) {
  131. // memcpy failed
  132. MS_LOG(ERROR) << "memcpy tensor memory failed"
  133. << ".";
  134. return 0; // amount of data copied is 0, error
  135. }
  136. return tensor_size;
  137. }
  138. } // namespace dataset
  139. } // namespace mindspore