Skip to content

add a wrapper for getAvailableTargets#16273

Merged
alalek merged 2 commits intoopencv:3.4from
JulienMaille:wrapper_available_target
Jan 17, 2020
Merged

add a wrapper for getAvailableTargets#16273
alalek merged 2 commits intoopencv:3.4from
JulienMaille:wrapper_available_target

Conversation

@JulienMaille
Copy link
Copy Markdown
Contributor

@JulienMaille JulienMaille commented Dec 31, 2019

as requested by @dkurt here: #16184 (comment)

force_builders=Custom,Custom Win,Custom Mac
build_image:Custom=ubuntu-openvino-2019r3.0:16.04
build_image:Custom Win=openvino-2019r3.0
build_image:Custom Mac=openvino-2019r3.0

test_modules:Custom=dnn,python2,python3,java
test_modules:Custom Win=dnn,python2,python3,java
test_modules:Custom Mac=dnn,python2,python3,java

buildworker:Custom=linux-1
# disabled due high memory usage: test_opencl:Custom=ON
test_opencl:Custom=OFF
test_bigdata:Custom=1
test_filter:Custom=*

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Jan 2, 2020

Thanks!
Please change target branch to 3.4. We will port to master branch

@JulienMaille JulienMaille changed the base branch from master to 3.4 January 2, 2020 12:59
@JulienMaille
Copy link
Copy Markdown
Contributor Author

done

dkurt
dkurt previously approved these changes Jan 2, 2020
Copy link
Copy Markdown
Member

@dkurt dkurt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Thank you!

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 3, 2020

Could you please add simple bindings tests?

Check should be simple enough (DNN_BACKEND_OPENCV+DNN_TARGET_CPU is always available).

Thank you!

@JulienMaille
Copy link
Copy Markdown
Contributor Author

@alalek something like this?

def test_getAvailableTargets(self):
    targets = cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_OPENCV)
    test.assertIn(cv.dnn.DNN_TARGET_CPU, targets)

@alalek
Copy link
Copy Markdown
Member

alalek commented Jan 4, 2020

@JulienMaille Yes

@JulienMaille
Copy link
Copy Markdown
Contributor Author

I tried my best, but I'm gonna need help with the java test

@JulienMaille JulienMaille force-pushed the wrapper_available_target branch 6 times, most recently from 27582c4 to 651f17b Compare January 6, 2020 23:20
@JulienMaille
Copy link
Copy Markdown
Contributor Author

JulienMaille commented Jan 8, 2020

@alalek Python test is ok but this is the error I get for the java one:

    [javac] /build/precommit_linux64/build/java_test/src/org/opencv/test/dnn/DnnTensorFlowTest.java:146: error: cannot find symbol
    [javac]         List<Integer> targets = Dnn.getAvailableTargets(Dnn.DNN_BACKEND_OPENCV);
    [javac]                                    ^
    [javac]   symbol:   method getAvailableTargets(int)
    [javac]   location: class Dnn

I probably has to do with the fact it expects an enum Backend and receives an int

@dkurt dkurt dismissed their stale review January 9, 2020 15:44

add tests

@JulienMaille
Copy link
Copy Markdown
Contributor Author

@dkurt can the issue with java be solved without changing the signature to take an int instead of an enum?

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Jan 10, 2020

@JulienMaille, I will try to check it.

@dkurt
Copy link
Copy Markdown
Member

dkurt commented Jan 16, 2020

@JulienMaille, may I ask you to try the following changes?

diff --git a/modules/dnn/include/opencv2/dnn/dnn.hpp b/modules/dnn/include/opencv2/dnn/dnn.hpp
index a2030ad..4699dac 100644
--- a/modules/dnn/include/opencv2/dnn/dnn.hpp
+++ b/modules/dnn/include/opencv2/dnn/dnn.hpp
@@ -97,7 +97,7 @@ CV__DNN_INLINE_NS_BEGIN
     };
 
     CV_EXPORTS std::vector< std::pair<Backend, Target> > getAvailableBackends();
-    CV_EXPORTS_W std::vector<Target> getAvailableTargets(Backend be);
+    CV_EXPORTS_W std::vector<Target> getAvailableTargets(dnn::Backend be);
 
     /** @brief This class provides all data needed to initialize layer.
      *
diff --git a/modules/dnn/misc/java/gen_dict.json b/modules/dnn/misc/java/gen_dict.json
index c0e0a1b..5a397ea 100644
--- a/modules/dnn/misc/java/gen_dict.json
+++ b/modules/dnn/misc/java/gen_dict.json
@@ -36,6 +36,14 @@
             "v_type": "vector_Layer",
             "j_import": "org.opencv.dnn.Layer"
         },
+        "vector_Target": {
+            "j_type": "List<Integer>",
+            "jn_type": "List<Integer>",
+            "jni_type": "jobject",
+            "jni_var": "std::vector< cv::dnn::Target > %(n)s",
+            "suffix": "Ljava_util_List",
+            "v_type": "vector_Target"
+        },
         "LayerId": {
             "j_type": "DictValue",
             "jn_type": "long",
diff --git a/modules/dnn/misc/java/src/cpp/dnn_converters.cpp b/modules/dnn/misc/java/src/cpp/dnn_converters.cpp
index 1d259ae..95184c0 100644
--- a/modules/dnn/misc/java/src/cpp/dnn_converters.cpp
+++ b/modules/dnn/misc/java/src/cpp/dnn_converters.cpp
@@ -60,6 +60,25 @@ jobject vector_Ptr_Layer_to_List(JNIEnv* env, std::vector<cv::Ptr<cv::dnn::Layer
     return result;
 }
 
+jobject vector_Target_to_List(JNIEnv* env, std::vector<cv::dnn::Target>& vs)
+{
+    static jclass juArrayList   = ARRAYLIST(env);
+    static jmethodID m_create   = CONSTRUCTOR(env, juArrayList);
+    jmethodID m_add       = LIST_ADD(env, juArrayList);
+
+    static jclass jInteger = env->FindClass("java/lang/Integer");
+    static jmethodID m_create_Integer = env->GetMethodID(jInteger, "<init>", "(I)V");
+
+    jobject result = env->NewObject(juArrayList, m_create, vs.size());
+    for (size_t i = 0; i < vs.size(); ++i)
+    {
+        jobject element = env->NewObject(jInteger, m_create_Integer, vs[i]);
+        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);
diff --git a/modules/dnn/misc/java/src/cpp/dnn_converters.hpp b/modules/dnn/misc/java/src/cpp/dnn_converters.hpp
index 6a72701..e1f63e0 100644
--- a/modules/dnn/misc/java/src/cpp/dnn_converters.hpp
+++ b/modules/dnn/misc/java/src/cpp/dnn_converters.hpp
@@ -28,5 +28,6 @@ jobject vector_Ptr_Layer_to_List(JNIEnv* env, std::vector<cv::Ptr<cv::dnn::Layer
 
 std::vector<cv::Ptr<cv::dnn::Layer> > List_to_vector_Ptr_Layer(JNIEnv* env, jobject list);
 
+jobject vector_Target_to_List(JNIEnv* env, std::vector<cv::dnn::Target>& vs);
 
 #endif /* DNN_CONVERTERS_HPP */

@JulienMaille JulienMaille force-pushed the wrapper_available_target branch from 651f17b to 6f1dd88 Compare January 16, 2020 19:59
@JulienMaille
Copy link
Copy Markdown
Contributor Author

@dkurt done

Copy link
Copy Markdown
Member

@dkurt dkurt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Looks good to me! Thanks!

@dkurt dkurt self-assigned this Jan 17, 2020
@alalek alalek merged commit 886220b into opencv:3.4 Jan 17, 2020
@JulienMaille JulienMaille deleted the wrapper_available_target branch January 17, 2020 16:37
@alalek alalek mentioned this pull request Jan 22, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants