Blame view

3rdparty/opencv-4.5.4/modules/dnn/test/npy_blob.cpp 2.5 KB
f4334277   Hu Chunming   提交3rdparty
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  // This file is part of OpenCV project.
  // It is subject to the license terms in the LICENSE file found in the top-level directory
  // of this distribution and at http://opencv.org/license.html.
  //
  // Copyright (C) 2017, Intel Corporation, all rights reserved.
  // Third party copyrights are property of their respective owners.
  
  #include "test_precomp.hpp"
  #include "npy_blob.hpp"
  
  namespace cv
  {
  
  static std::string getType(const std::string& header)
  {
      std::string field = "'descr':";
      int idx = header.find(field);
      CV_Assert(idx != -1);
  
      int from = header.find('\'', idx + field.size()) + 1;
      int to = header.find('\'', from);
      return header.substr(from, to - from);
  }
  
  static std::string getFortranOrder(const std::string& header)
  {
      std::string field = "'fortran_order':";
      int idx = header.find(field);
      CV_Assert(idx != -1);
  
      int from = header.find_last_of(' ', idx + field.size()) + 1;
      int to = header.find(',', from);
      return header.substr(from, to - from);
  }
  
  static std::vector<int> getShape(const std::string& header)
  {
      std::string field = "'shape':";
      int idx = header.find(field);
      CV_Assert(idx != -1);
  
      int from = header.find('(', idx + field.size()) + 1;
      int to = header.find(')', from);
  
      std::string shapeStr = header.substr(from, to - from);
      if (shapeStr.empty())
          return std::vector<int>(1, 1);
  
      // Remove all commas.
      shapeStr.erase(std::remove(shapeStr.begin(), shapeStr.end(), ','),
                     shapeStr.end());
  
      std::istringstream ss(shapeStr);
      int value;
  
      std::vector<int> shape;
      while (ss >> value)
      {
          shape.push_back(value);
      }
      return shape;
  }
  
  Mat blobFromNPY(const std::string& path)
  {
      std::ifstream ifs(path.c_str(), std::ios::binary);
      CV_Assert(ifs.is_open());
  
      std::string magic(6, '*');
      ifs.read(&magic[0], magic.size());
      CV_Assert(magic == "\x93NUMPY");
  
      ifs.ignore(1);  // Skip major version byte.
      ifs.ignore(1);  // Skip minor version byte.
  
      unsigned short headerSize;
      ifs.read((char*)&headerSize, sizeof(headerSize));
  
      std::string header(headerSize, '*');
      ifs.read(&header[0], header.size());
  
      // Extract data type.
      CV_Assert(getType(header) == "<f4");
      CV_Assert(getFortranOrder(header) == "False");
      std::vector<int> shape = getShape(header);
  
      Mat blob(shape, CV_32F);
      ifs.read((char*)blob.data, blob.total() * blob.elemSize());
      CV_Assert((size_t)ifs.gcount() == blob.total() * blob.elemSize());
  
      return blob;
  }
  
  }  // namespace cv