Skip to content

Commit 5590aea

Browse files
committed
Merge pull request opencv#8998 from abratchik:DNN.java.wrappers.fix
2 parents 108188e + 8f71814 commit 5590aea

File tree

11 files changed

+347
-32
lines changed

11 files changed

+347
-32
lines changed

modules/dnn/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ endif()
99

1010
set(the_description "Deep neural network module. It allows to load models from different frameworks and to make forward pass")
1111

12-
ocv_add_module(dnn opencv_core opencv_imgproc WRAP python matlab)
12+
ocv_add_module(dnn opencv_core opencv_imgproc WRAP python matlab java)
1313
ocv_warnings_disable(CMAKE_CXX_FLAGS -Wno-shadow -Wno-parentheses -Wmaybe-uninitialized -Wsign-promo
1414
-Wmissing-declarations -Wmissing-prototypes
1515
)

modules/dnn/include/opencv2/dnn/dict.hpp

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@
3939
//
4040
//M*/
4141

42-
#ifndef OPENCV_DNN_DNN_DICT_HPP
43-
#define OPENCV_DNN_DNN_DICT_HPP
44-
4542
#include <opencv2/core.hpp>
4643
#include <map>
4744
#include <ostream>
4845

4946
#include <opencv2/dnn/dnn.hpp>
5047

48+
#ifndef OPENCV_DNN_DNN_DICT_HPP
49+
#define OPENCV_DNN_DNN_DICT_HPP
50+
5151
namespace cv {
5252
namespace dnn {
5353
CV__DNN_EXPERIMENTAL_NS_BEGIN
@@ -57,14 +57,14 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
5757
/** @brief This struct stores the scalar value (or array) of one of the following type: double, cv::String or int64.
5858
* @todo Maybe int64 is useless because double type exactly stores at least 2^52 integers.
5959
*/
60-
struct DictValue
60+
struct CV_EXPORTS_W DictValue
6161
{
6262
DictValue(const DictValue &r);
6363
DictValue(int64 i = 0) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
64-
DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
64+
CV_WRAP DictValue(int i) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = i; } //!< Constructs integer scalar
6565
DictValue(unsigned p) : type(Param::INT), pi(new AutoBuffer<int64,1>) { (*pi)[0] = p; } //!< Constructs integer scalar
66-
DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
67-
DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
66+
CV_WRAP DictValue(double p) : type(Param::REAL), pd(new AutoBuffer<double,1>) { (*pd)[0] = p; } //!< Constructs floating point scalar
67+
CV_WRAP DictValue(const String &s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< Constructs string scalar
6868
DictValue(const char *s) : type(Param::STRING), ps(new AutoBuffer<String,1>) { (*ps)[0] = s; } //!< @overload
6969

7070
template<typename TypeIter>
@@ -79,9 +79,13 @@ struct DictValue
7979

8080
int size() const;
8181

82-
bool isInt() const;
83-
bool isString() const;
84-
bool isReal() const;
82+
CV_WRAP bool isInt() const;
83+
CV_WRAP bool isString() const;
84+
CV_WRAP bool isReal() const;
85+
86+
CV_WRAP int getIntValue(int idx = -1) const;
87+
CV_WRAP double getRealValue(int idx = -1) const;
88+
CV_WRAP String getStringValue(int idx = -1) const;
8589

8690
DictValue &operator=(const DictValue &r);
8791

modules/dnn/include/opencv2/dnn/dnn.hpp

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,10 @@
4646
#include <opencv2/core.hpp>
4747

4848
#if !defined CV_DOXYGEN && !defined CV_DNN_DONT_ADD_EXPERIMENTAL_NS
49-
#define CV__DNN_EXPERIMENTAL_NS_USE using namespace experimental_dnn_v1;
5049
#define CV__DNN_EXPERIMENTAL_NS_BEGIN namespace experimental_dnn_v1 {
5150
#define CV__DNN_EXPERIMENTAL_NS_END }
51+
namespace cv { namespace dnn { namespace experimental_dnn_v1 { } using namespace experimental_dnn_v1; }}
5252
#else
53-
#define CV__DNN_EXPERIMENTAL_NS_USE
5453
#define CV__DNN_EXPERIMENTAL_NS_BEGIN
5554
#define CV__DNN_EXPERIMENTAL_NS_END
5655
#endif
@@ -59,7 +58,6 @@
5958

6059
namespace cv {
6160
namespace dnn {
62-
CV__DNN_EXPERIMENTAL_NS_USE
6361
CV__DNN_EXPERIMENTAL_NS_BEGIN
6462
//! @addtogroup dnn
6563
//! @{
@@ -160,7 +158,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
160158
* Each class, derived from Layer, must implement allocate() methods to declare own outputs and forward() to compute outputs.
161159
* Also before using the new layer into networks you must register your layer by using one of @ref dnnLayerFactory "LayerFactory" macros.
162160
*/
163-
class CV_EXPORTS_W Layer
161+
class CV_EXPORTS_W Layer : public Algorithm
164162
{
165163
public:
166164

@@ -329,7 +327,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
329327
/** @brief Container for strings and integers. */
330328
typedef DictValue LayerId;
331329

332-
/** @brief Returns pointer to layer with specified name which the network use. */
330+
/** @brief Returns pointer to layer with specified id or name which the network use. */
333331
CV_WRAP Ptr<Layer> getLayer(LayerId layerId);
334332

335333
/** @brief Returns pointers to input layers of specific layer. */
@@ -517,7 +515,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
517515
/** @brief Returns list of types for layer used in model.
518516
* @param layersTypes output parameter for returning types.
519517
*/
520-
CV_WRAP void getLayerTypes(std::vector<String>& layersTypes) const;
518+
CV_WRAP void getLayerTypes(CV_OUT std::vector<String>& layersTypes) const;
521519

522520
/** @brief Returns count of layers of specified type.
523521
* @param layerType type.
@@ -532,18 +530,18 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
532530
* @param blobs output parameter to store resulting bytes for intermediate blobs.
533531
*/
534532
CV_WRAP void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
535-
size_t& weights, size_t& blobs) const;
533+
CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
536534
/** @overload */
537535
CV_WRAP void getMemoryConsumption(const MatShape& netInputShape,
538-
size_t& weights, size_t& blobs) const;
536+
CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
539537
/** @overload */
540538
CV_WRAP void getMemoryConsumption(const int layerId,
541539
const std::vector<MatShape>& netInputShapes,
542-
size_t& weights, size_t& blobs) const;
540+
CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
543541
/** @overload */
544542
CV_WRAP void getMemoryConsumption(const int layerId,
545543
const MatShape& netInputShape,
546-
size_t& weights, size_t& blobs) const;
544+
CV_OUT size_t& weights, CV_OUT size_t& blobs) const;
547545

548546
/** @brief Computes bytes number which are requered to store
549547
* all weights and intermediate blobs for each layer.
@@ -553,20 +551,20 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
553551
* @param blobs output parameter to store resulting bytes for intermediate blobs.
554552
*/
555553
CV_WRAP void getMemoryConsumption(const std::vector<MatShape>& netInputShapes,
556-
std::vector<int>& layerIds, std::vector<size_t>& weights,
557-
std::vector<size_t>& blobs) const;
554+
CV_OUT std::vector<int>& layerIds, CV_OUT std::vector<size_t>& weights,
555+
CV_OUT std::vector<size_t>& blobs) const;
558556
/** @overload */
559557
CV_WRAP void getMemoryConsumption(const MatShape& netInputShape,
560-
std::vector<int>& layerIds, std::vector<size_t>& weights,
561-
std::vector<size_t>& blobs) const;
558+
CV_OUT std::vector<int>& layerIds, CV_OUT std::vector<size_t>& weights,
559+
CV_OUT std::vector<size_t>& blobs) const;
562560
private:
563561

564562
struct Impl;
565563
Ptr<Impl> impl;
566564
};
567565

568566
/** @brief Small interface class for loading trained serialized models of different dnn-frameworks. */
569-
class CV_EXPORTS_W Importer
567+
class CV_EXPORTS_W Importer : public Algorithm
570568
{
571569
public:
572570

@@ -602,7 +600,7 @@ CV__DNN_EXPERIMENTAL_NS_BEGIN
602600
* @param model path to the .pb file with binary protobuf description of the network architecture.
603601
* @returns Pointer to the created importer, NULL in failure cases.
604602
*/
605-
CV_EXPORTS Ptr<Importer> createTensorflowImporter(const String &model);
603+
CV_EXPORTS_W Ptr<Importer> createTensorflowImporter(const String &model);
606604

607605
/** @brief Creates the importer of <a href="http://torch.ch">Torch7</a> framework network.
608606
* @param filename path to the file, dumped from Torch by using torch.save() function.
@@ -676,4 +674,4 @@ CV__DNN_EXPERIMENTAL_NS_END
676674
#include <opencv2/dnn/layer.hpp>
677675
#include <opencv2/dnn/dnn.inl.hpp>
678676

679-
#endif /* __OPENCV_DNN_DNN_HPP__ */
677+
#endif /* OPENCV_DNN_DNN_HPP */

modules/dnn/include/opencv2/dnn/dnn.inl.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,11 @@ inline int DictValue::get<int>(int idx) const
115115
return (int)get<int64>(idx);
116116
}
117117

118+
inline int DictValue::getIntValue(int idx) const
119+
{
120+
return (int)get<int64>(idx);
121+
}
122+
118123
template<>
119124
inline unsigned DictValue::get<unsigned>(int idx) const
120125
{
@@ -148,6 +153,11 @@ inline double DictValue::get<double>(int idx) const
148153
}
149154
}
150155

156+
inline double DictValue::getRealValue(int idx) const
157+
{
158+
return get<double>(idx);
159+
}
160+
151161
template<>
152162
inline float DictValue::get<float>(int idx) const
153163
{
@@ -162,6 +172,12 @@ inline String DictValue::get<String>(int idx) const
162172
return (*ps)[(idx == -1) ? 0 : idx];
163173
}
164174

175+
176+
inline String DictValue::getStringValue(int idx) const
177+
{
178+
return get<String>(idx);
179+
}
180+
165181
inline void DictValue::release()
166182
{
167183
switch (type)

modules/dnn/misc/java/filelist_common

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
misc/java/src/cpp/dnn_converters.hpp

modules/dnn/misc/java/gen_dict.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
{
2+
"type_dict": {
3+
"MatShape": {
4+
"j_type": "MatOfInt",
5+
"jn_type": "long",
6+
"jni_type": "jlong",
7+
"jni_var": "MatShape %(n)s",
8+
"suffix": "J",
9+
"v_type": "Mat",
10+
"j_import": "org.opencv.core.MatOfInt"
11+
},
12+
"vector_MatShape": {
13+
"j_type": "List<MatOfInt>",
14+
"jn_type": "List<MatOfInt>",
15+
"jni_type": "jobject",
16+
"jni_var": "std::vector< MatShape > %(n)s",
17+
"suffix": "Ljava_util_List",
18+
"v_type": "vector_MatShape",
19+
"j_import": "org.opencv.core.MatOfInt"
20+
},
21+
"vector_size_t": {
22+
"j_type": "MatOfDouble",
23+
"jn_type": "long",
24+
"jni_type": "jlong",
25+
"jni_var": "std::vector<size_t> %(n)s",
26+
"suffix": "J",
27+
"v_type": "Mat",
28+
"j_import": "org.opencv.core.MatOfDouble"
29+
},
30+
"vector_Ptr_Layer": {
31+
"j_type": "List<Layer>",
32+
"jn_type": "List<Layer>",
33+
"jni_type": "jobject",
34+
"jni_var": "std::vector< Ptr<cv::dnn::Layer> > %(n)s",
35+
"suffix": "Ljava_util_List",
36+
"v_type": "vector_Layer",
37+
"j_import": "org.opencv.dnn.Layer"
38+
},
39+
"LayerId": {
40+
"j_type": "DictValue",
41+
"jn_type": "long",
42+
"jn_args": [
43+
[
44+
"__int64",
45+
".getNativeObjAddr()"
46+
]
47+
48+
],
49+
"jni_name": "(*(cv::dnn::DictValue*)%(n)s_nativeObj)",
50+
"jni_type": "jlong",
51+
"suffix": "J",
52+
"j_import": "org.opencv.dnn.DictValue"
53+
}
54+
}
55+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// This file is part of OpenCV project.
2+
// It is subject to the license terms in the LICENSE file found in the top-level directory
3+
// of this distribution and at http://opencv.org/license.html
4+
5+
// Author: abratchik
6+
7+
#include "dnn_converters.hpp"
8+
9+
10+
void Mat_to_MatShape(cv::Mat& mat, MatShape& matshape)
11+
{
12+
matshape.clear();
13+
CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1);
14+
matshape = (MatShape) mat;
15+
}
16+
17+
void MatShape_to_Mat(MatShape& matshape, cv::Mat& mat)
18+
{
19+
mat = cv::Mat(matshape, true);
20+
}
21+
22+
void Mat_to_vector_size_t(cv::Mat& mat, std::vector<size_t>& v_size_t)
23+
{
24+
v_size_t.clear();
25+
CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1);
26+
v_size_t = (std::vector<size_t>) mat;
27+
}
28+
29+
void vector_size_t_to_Mat(std::vector<size_t>& v_size_t, cv::Mat& mat)
30+
{
31+
mat = cv::Mat(v_size_t, true);
32+
}
33+
34+
std::vector<MatShape> List_to_vector_MatShape(JNIEnv* env, jobject list)
35+
{
36+
static jclass juArrayList = ARRAYLIST(env);
37+
jmethodID m_size = LIST_SIZE(env, juArrayList);
38+
jmethodID m_get = LIST_GET(env, juArrayList);
39+
40+
static jclass jMatOfInt = MATOFINT(env);
41+
42+
jint len = env->CallIntMethod(list, m_size);
43+
std::vector<MatShape> result;
44+
result.reserve(len);
45+
for (jint i=0; i<len; i++)
46+
{
47+
jobject element = static_cast<jobject>(env->CallObjectMethod(list, m_get, i));
48+
cv::Mat& mat = *((cv::Mat*) GETNATIVEOBJ(env, jMatOfInt, element) );
49+
MatShape matshape = (MatShape) mat;
50+
result.push_back(matshape);
51+
env->DeleteLocalRef(element);
52+
}
53+
return result;
54+
}
55+
56+
jobject vector_Ptr_Layer_to_List(JNIEnv* env, std::vector<cv::Ptr<cv::dnn::Layer> >& vs)
57+
{
58+
static jclass juArrayList = ARRAYLIST(env);
59+
static jmethodID m_create = CONSTRUCTOR(env, juArrayList);
60+
jmethodID m_add = LIST_ADD(env, juArrayList);
61+
62+
static jclass jLayerClass = LAYER(env);
63+
static jmethodID m_create_layer = LAYER_CONSTRUCTOR(env, jLayerClass);
64+
65+
jobject result = env->NewObject(juArrayList, m_create, vs.size());
66+
for (std::vector< cv::Ptr<cv::dnn::Layer> >::iterator it = vs.begin(); it != vs.end(); ++it) {
67+
jobject element = env->NewObject(jLayerClass, m_create_layer, (*it).get());
68+
env->CallBooleanMethod(result, m_add, element);
69+
env->DeleteLocalRef(element);
70+
}
71+
return result;
72+
}
73+
74+
std::vector<cv::Ptr<cv::dnn::Layer> > List_to_vector_Ptr_Layer(JNIEnv* env, jobject list)
75+
{
76+
static jclass juArrayList = ARRAYLIST(env);
77+
jmethodID m_size = LIST_SIZE(env, juArrayList);
78+
jmethodID m_get = LIST_GET(env, juArrayList);
79+
80+
static jclass jLayerClass = LAYER(env);
81+
82+
jint len = env->CallIntMethod(list, m_size);
83+
std::vector< cv::Ptr<cv::dnn::Layer> > result;
84+
result.reserve(len);
85+
for (jint i=0; i<len; i++)
86+
{
87+
jobject element = static_cast<jobject>(env->CallObjectMethod(list, m_get, i));
88+
cv::Ptr<cv::dnn::Layer>* layer_ptr = (cv::Ptr<cv::dnn::Layer>*) GETNATIVEOBJ(env, jLayerClass, element) ;
89+
cv::Ptr<cv::dnn::Layer> layer = *(layer_ptr);
90+
result.push_back(layer);
91+
env->DeleteLocalRef(element);
92+
}
93+
return result;
94+
}

0 commit comments

Comments
 (0)