Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
O
opencv
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
opencv
Commits
a391871c
Commit
a391871c
authored
Sep 15, 2017
by
Vadim Pisarevsky
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #9301 from dkurt:dnn_npy
parents
41b23fde
e74c4751
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
100 additions
and
51 deletions
+100
-51
cnpy.cpp
modules/dnn/test/cnpy.cpp
+0
-0
cnpy.h
modules/dnn/test/cnpy.h
+0
-0
npy_blob.cpp
modules/dnn/test/npy_blob.cpp
+93
-0
npy_blob.hpp
modules/dnn/test/npy_blob.hpp
+7
-51
No files found.
modules/dnn/test/cnpy.cpp
deleted
100644 → 0
View file @
41b23fde
This diff is collapsed.
Click to expand it.
modules/dnn/test/cnpy.h
deleted
100644 → 0
View file @
41b23fde
This diff is collapsed.
Click to expand it.
modules/dnn/test/npy_blob.cpp
0 → 100644
View file @
a391871c
// 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 "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
==
"
\x93
NUMPY"
);
ifs
.
ignore
(
1
);
// Skip major version byte.
ifs
.
ignore
(
1
);
// Skip minor version byte.
uint16_t
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
(
ifs
.
gcount
()
==
blob
.
total
()
*
blob
.
elemSize
());
return
blob
;
}
}
// namespace cv
modules/dnn/test/npy_blob.hpp
View file @
a391871c
/*M///////////////////////////////////////////////////////////////////////////////////////
// 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.
//
// IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
//
// By downloading, copying, installing or using the software you agree to this license.
// If you do not agree to this license, do not download, install,
// copy or use the software.
//
//
// License Agreement
// For Open Source Computer Vision Library
//
// Copyright (C) 2013, OpenCV Foundation, all rights reserved.
// Copyright (C) 2017, Intel Corporation, all rights reserved.
// Third party copyrights are property of their respective owners.
//
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
//
// * Redistribution's of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
//
// * Redistribution's in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// * The name of the copyright holders may not be used to endorse or promote products
// derived from this software without specific prior written permission.
//
// This software is provided by the copyright holders and contributors "as is" and
// any express or implied warranties, including, but not limited to, the implied
// warranties of merchantability and fitness for a particular purpose are disclaimed.
// In no event shall the Intel Corporation or contributors be liable for any direct,
// indirect, incidental, special, exemplary, or consequential damages
// (including, but not limited to, procurement of substitute goods or services;
// loss of use, data, or profits; or business interruption) however caused
// and on any theory of liability, whether in contract, strict liability,
// or tort (including negligence or otherwise) arising in any way out of
// the use of this software, even if advised of the possibility of such damage.
//
//M*/
#ifndef __OPENCV_DNN_TEST_NPY_BLOB_HPP__
#define __OPENCV_DNN_TEST_NPY_BLOB_HPP__
#include "test_precomp.hpp"
#include "cnpy.h"
namespace
cv
{
inline
Mat
blobFromNPY
(
const
String
&
path
)
{
cnpy
::
NpyArray
npyBlob
=
cnpy
::
npy_load
(
path
.
c_str
());
Mat
blob
=
Mat
((
int
)
npyBlob
.
shape
.
size
(),
(
int
*
)
&
npyBlob
.
shape
[
0
],
CV_32F
,
npyBlob
.
data
).
clone
();
npyBlob
.
destruct
();
return
blob
;
}
inline
void
saveBlobToNPY
(
const
Mat
&
blob
,
const
String
&
path
)
{
cnpy
::
npy_save
(
path
.
c_str
(),
blob
.
ptr
<
float
>
(),
(
unsigned
*
)
&
blob
.
size
.
p
[
0
],
blob
.
dims
);
}
// Parse serialized NumPy array by np.save(...)
// Based on specification of .npy data format.
Mat
blobFromNPY
(
const
std
::
string
&
path
);
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment