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.

data_helper.cc 4.3 kB

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