2017-08-03 15:59:03 +08:00
|
|
|
// 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.
|
|
|
|
|
2018-02-28 21:44:41 +08:00
|
|
|
#include "test_precomp.hpp"
|
2017-08-03 15:59:03 +08:00
|
|
|
#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.
|
|
|
|
|
2017-09-19 18:08:23 +08:00
|
|
|
unsigned short headerSize;
|
2017-08-03 15:59:03 +08:00
|
|
|
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());
|
2017-11-05 21:48:40 +08:00
|
|
|
CV_Assert((size_t)ifs.gcount() == blob.total() * blob.elemSize());
|
2017-08-03 15:59:03 +08:00
|
|
|
|
|
|
|
return blob;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace cv
|