add java wrappers to dnn module

This commit is contained in:
abratchik
2017-06-27 10:52:44 +04:00
parent e5aa213554
commit 8f7181429f
11 changed files with 347 additions and 32 deletions
@@ -0,0 +1,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
// Author: abratchik
#include "dnn_converters.hpp"
void Mat_to_MatShape(cv::Mat& mat, MatShape& matshape)
{
matshape.clear();
CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1);
matshape = (MatShape) mat;
}
void MatShape_to_Mat(MatShape& matshape, cv::Mat& mat)
{
mat = cv::Mat(matshape, true);
}
void Mat_to_vector_size_t(cv::Mat& mat, std::vector<size_t>& v_size_t)
{
v_size_t.clear();
CHECK_MAT(mat.type()==CV_32SC1 && mat.cols==1);
v_size_t = (std::vector<size_t>) mat;
}
void vector_size_t_to_Mat(std::vector<size_t>& v_size_t, cv::Mat& mat)
{
mat = cv::Mat(v_size_t, true);
}
std::vector<MatShape> List_to_vector_MatShape(JNIEnv* env, jobject list)
{
static jclass juArrayList = ARRAYLIST(env);
jmethodID m_size = LIST_SIZE(env, juArrayList);
jmethodID m_get = LIST_GET(env, juArrayList);
static jclass jMatOfInt = MATOFINT(env);
jint len = env->CallIntMethod(list, m_size);
std::vector<MatShape> result;
result.reserve(len);
for (jint i=0; i<len; i++)
{
jobject element = static_cast<jobject>(env->CallObjectMethod(list, m_get, i));
cv::Mat& mat = *((cv::Mat*) GETNATIVEOBJ(env, jMatOfInt, element) );
MatShape matshape = (MatShape) mat;
result.push_back(matshape);
env->DeleteLocalRef(element);
}
return result;
}
jobject vector_Ptr_Layer_to_List(JNIEnv* env, std::vector<cv::Ptr<cv::dnn::Layer> >& vs)
{
static jclass juArrayList = ARRAYLIST(env);
static jmethodID m_create = CONSTRUCTOR(env, juArrayList);
jmethodID m_add = LIST_ADD(env, juArrayList);
static jclass jLayerClass = LAYER(env);
static jmethodID m_create_layer = LAYER_CONSTRUCTOR(env, jLayerClass);
jobject result = env->NewObject(juArrayList, m_create, vs.size());
for (std::vector< cv::Ptr<cv::dnn::Layer> >::iterator it = vs.begin(); it != vs.end(); ++it) {
jobject element = env->NewObject(jLayerClass, m_create_layer, (*it).get());
env->CallBooleanMethod(result, m_add, element);
env->DeleteLocalRef(element);
}
return result;
}
std::vector<cv::Ptr<cv::dnn::Layer> > List_to_vector_Ptr_Layer(JNIEnv* env, jobject list)
{
static jclass juArrayList = ARRAYLIST(env);
jmethodID m_size = LIST_SIZE(env, juArrayList);
jmethodID m_get = LIST_GET(env, juArrayList);
static jclass jLayerClass = LAYER(env);
jint len = env->CallIntMethod(list, m_size);
std::vector< cv::Ptr<cv::dnn::Layer> > result;
result.reserve(len);
for (jint i=0; i<len; i++)
{
jobject element = static_cast<jobject>(env->CallObjectMethod(list, m_get, i));
cv::Ptr<cv::dnn::Layer>* layer_ptr = (cv::Ptr<cv::dnn::Layer>*) GETNATIVEOBJ(env, jLayerClass, element) ;
cv::Ptr<cv::dnn::Layer> layer = *(layer_ptr);
result.push_back(layer);
env->DeleteLocalRef(element);
}
return result;
}
@@ -0,0 +1,36 @@
// 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
// Author: abratchik
#ifndef DNN_CONVERTERS_HPP
#define DNN_CONVERTERS_HPP
#include <jni.h>
#include "opencv2/java.hpp"
#include "opencv2/core.hpp"
#include "opencv2/dnn/dnn.hpp"
#define LAYER(ENV) static_cast<jclass>(ENV->NewGlobalRef(ENV->FindClass("org/opencv/dnn/Layer")))
#define LAYER_CONSTRUCTOR(ENV, CLS) ENV->GetMethodID(CLS, "<init>", "(J)V")
using namespace cv::dnn;
void Mat_to_MatShape(cv::Mat& mat, MatShape& matshape);
void MatShape_to_Mat(MatShape& matshape, cv::Mat& mat);
void Mat_to_vector_size_t(cv::Mat& mat, std::vector<size_t>& v_size_t);
void vector_size_t_to_Mat(std::vector<size_t>& v_size_t, cv::Mat& mat);
std::vector<MatShape> List_to_vector_MatShape(JNIEnv* env, jobject list);
jobject vector_Ptr_Layer_to_List(JNIEnv* env, std::vector<cv::Ptr<cv::dnn::Layer> >& vs);
std::vector<cv::Ptr<cv::dnn::Layer> > List_to_vector_Ptr_Layer(JNIEnv* env, jobject list);
#endif /* DNN_CONVERTERS_HPP */