mirror of https://github.com/opencv/opencv.git
parent
e5aa213554
commit
8f7181429f
11 changed files with 347 additions and 32 deletions
@ -0,0 +1 @@ |
||||
misc/java/src/cpp/dnn_converters.hpp |
@ -0,0 +1,55 @@ |
||||
{ |
||||
"type_dict": { |
||||
"MatShape": { |
||||
"j_type": "MatOfInt", |
||||
"jn_type": "long", |
||||
"jni_type": "jlong", |
||||
"jni_var": "MatShape %(n)s", |
||||
"suffix": "J", |
||||
"v_type": "Mat", |
||||
"j_import": "org.opencv.core.MatOfInt" |
||||
}, |
||||
"vector_MatShape": { |
||||
"j_type": "List<MatOfInt>", |
||||
"jn_type": "List<MatOfInt>", |
||||
"jni_type": "jobject", |
||||
"jni_var": "std::vector< MatShape > %(n)s", |
||||
"suffix": "Ljava_util_List", |
||||
"v_type": "vector_MatShape", |
||||
"j_import": "org.opencv.core.MatOfInt" |
||||
}, |
||||
"vector_size_t": { |
||||
"j_type": "MatOfDouble", |
||||
"jn_type": "long", |
||||
"jni_type": "jlong", |
||||
"jni_var": "std::vector<size_t> %(n)s", |
||||
"suffix": "J", |
||||
"v_type": "Mat", |
||||
"j_import": "org.opencv.core.MatOfDouble" |
||||
}, |
||||
"vector_Ptr_Layer": { |
||||
"j_type": "List<Layer>", |
||||
"jn_type": "List<Layer>", |
||||
"jni_type": "jobject", |
||||
"jni_var": "std::vector< Ptr<cv::dnn::Layer> > %(n)s", |
||||
"suffix": "Ljava_util_List", |
||||
"v_type": "vector_Layer", |
||||
"j_import": "org.opencv.dnn.Layer" |
||||
}, |
||||
"LayerId": { |
||||
"j_type": "DictValue", |
||||
"jn_type": "long", |
||||
"jn_args": [ |
||||
[ |
||||
"__int64", |
||||
".getNativeObjAddr()" |
||||
] |
||||
|
||||
], |
||||
"jni_name": "(*(cv::dnn::DictValue*)%(n)s_nativeObj)", |
||||
"jni_type": "jlong", |
||||
"suffix": "J", |
||||
"j_import": "org.opencv.dnn.DictValue" |
||||
} |
||||
} |
||||
} |
@ -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 */ |
@ -0,0 +1,113 @@ |
||||
package org.opencv.test.dnn; |
||||
|
||||
import java.io.File; |
||||
import java.util.ArrayList; |
||||
import java.util.List; |
||||
import org.opencv.core.Core; |
||||
import org.opencv.core.Mat; |
||||
import org.opencv.core.Scalar; |
||||
import org.opencv.core.Size; |
||||
import org.opencv.dnn.DictValue; |
||||
import org.opencv.dnn.Dnn; |
||||
import org.opencv.dnn.Importer; |
||||
import org.opencv.dnn.Layer; |
||||
import org.opencv.dnn.Net; |
||||
import org.opencv.imgcodecs.Imgcodecs; |
||||
import org.opencv.imgproc.Imgproc; |
||||
import org.opencv.test.OpenCVTestCase; |
||||
|
||||
public class DnnTensorFlowTest extends OpenCVTestCase { |
||||
|
||||
private final static String ENV_OPENCV_DNN_TEST_DATA_PATH = "OPENCV_DNN_TEST_DATA_PATH"; |
||||
|
||||
private final static String ENV_OPENCV_TEST_DATA_PATH = "OPENCV_TEST_DATA_PATH"; |
||||
|
||||
String modelFileName = ""; |
||||
String sourceImageFile = ""; |
||||
|
||||
Net net; |
||||
|
||||
@Override |
||||
protected void setUp() throws Exception { |
||||
super.setUp(); |
||||
|
||||
String envDnnTestDataPath = System.getenv(ENV_OPENCV_DNN_TEST_DATA_PATH); |
||||
|
||||
if(envDnnTestDataPath == null){ |
||||
isTestCaseEnabled = false; |
||||
return; |
||||
} |
||||
|
||||
File dnnTestDataPath = new File(envDnnTestDataPath); |
||||
modelFileName = new File(dnnTestDataPath, "dnn/tensorflow_inception_graph.pb").toString(); |
||||
|
||||
String envTestDataPath = System.getenv(ENV_OPENCV_TEST_DATA_PATH); |
||||
|
||||
if(envTestDataPath == null) throw new Exception(ENV_OPENCV_TEST_DATA_PATH + " has to be defined!"); |
||||
|
||||
File testDataPath = new File(envTestDataPath); |
||||
|
||||
File f = new File(testDataPath, "dnn/space_shuttle.jpg"); |
||||
sourceImageFile = f.toString(); |
||||
if(!f.exists()) throw new Exception("Test image is missing: " + sourceImageFile); |
||||
|
||||
net = new Net(); |
||||
if(net.empty()) { |
||||
Importer importer = Dnn.createTensorflowImporter(modelFileName); |
||||
importer.populateNet(net); |
||||
} |
||||
|
||||
} |
||||
|
||||
public void testGetLayerTypes() { |
||||
List<String> layertypes = new ArrayList(); |
||||
net.getLayerTypes(layertypes); |
||||
|
||||
assertFalse("No layer types returned!", layertypes.isEmpty()); |
||||
} |
||||
|
||||
public void testGetLayer() { |
||||
List<String> layernames = net.getLayerNames(); |
||||
|
||||
assertFalse("Test net returned no layers!", layernames.isEmpty()); |
||||
|
||||
String testLayerName = layernames.get(0); |
||||
|
||||
DictValue layerId = new DictValue(testLayerName); |
||||
|
||||
assertEquals("DictValue did not return the string, which was used in constructor!", testLayerName, layerId.getStringValue()); |
||||
|
||||
Layer layer = net.getLayer(layerId); |
||||
|
||||
assertEquals("Layer name does not match the expected value!", testLayerName, layer.get_name()); |
||||
|
||||
} |
||||
|
||||
public void testTestNetForward() { |
||||
Mat rawImage = Imgcodecs.imread(sourceImageFile); |
||||
|
||||
assertNotNull("Loading image from file failed!", rawImage); |
||||
|
||||
Mat image = new Mat(); |
||||
Imgproc.resize(rawImage, image, new Size(224,224)); |
||||
|
||||
Mat inputBlob = Dnn.blobFromImage(image); |
||||
assertNotNull("Converting image to blob failed!", inputBlob); |
||||
|
||||
Mat inputBlobP = new Mat(); |
||||
Core.subtract(inputBlob, new Scalar(117.0), inputBlobP); |
||||
|
||||
net.setInput(inputBlobP, "input" ); |
||||
|
||||
Mat result = net.forward(); |
||||
|
||||
assertNotNull("Net returned no result!", result); |
||||
|
||||
Core.MinMaxLocResult minmax = Core.minMaxLoc(result.reshape(1, 1)); |
||||
|
||||
assertTrue("No image recognized!", minmax.maxVal > 0.9); |
||||
|
||||
|
||||
} |
||||
|
||||
} |
Loading…
Reference in new issue