Merge pull request #25709 from dkurt:wrap_addLayer

* Wrap dnn addLayer
* Add typing stubs
pull/25731/head
Dmitry Kurtaev 5 months ago committed by GitHub
parent bef5a87680
commit 3700f9e1e9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 4
      modules/dnn/include/opencv2/dnn/dnn.hpp
  2. 16
      modules/dnn/misc/python/pyopencv_dnn.hpp
  3. 16
      modules/dnn/misc/python/test/test_dnn.py
  4. 7
      modules/python/src2/typing_stubs_generation/predefined_types.py

@ -533,7 +533,7 @@ CV__DNN_INLINE_NS_BEGIN
* @param params parameters which will be used to initialize the creating layer.
* @returns unique identifier of created layer, or -1 if a failure will happen.
*/
int addLayer(const String &name, const String &type, const int &dtype, LayerParams &params);
CV_WRAP int addLayer(const String &name, const String &type, const int &dtype, LayerParams &params);
/** @overload Datatype of output blobs set to default CV_32F */
int addLayer(const String &name, const String &type, LayerParams &params);
@ -541,7 +541,7 @@ CV__DNN_INLINE_NS_BEGIN
/** @brief Adds new layer and connects its first input to the first output of previously added layer.
* @see addLayer()
*/
int addLayerToPrev(const String &name, const String &type, const int &dtype, LayerParams &params);
CV_WRAP int addLayerToPrev(const String &name, const String &type, const int &dtype, LayerParams &params);
/** @overload */
int addLayerToPrev(const String &name, const String &type, LayerParams &params);

@ -71,6 +71,22 @@ PyObject* pyopencv_from(const dnn::LayerParams& lp)
return dict;
}
template<>
bool pyopencv_to(PyObject *o, dnn::LayerParams &lp, const ArgInfo& info)
{
CV_Assert(PyDict_Check(o));
PyObject *key, *value;
Py_ssize_t pos = 0;
std::string keyName;
while (PyDict_Next(o, &pos, &key, &value)) {
getUnicodeString(key, keyName);
dnn::DictValue dv;
pyopencv_to(value, dv, info);
lp.set(keyName, dv);
}
return true;
}
template<>
PyObject* pyopencv_from(const std::vector<dnn::Target> &t)
{

@ -480,5 +480,21 @@ class dnn_test(NewOpenCVTests):
params.scalefactor = 2.0
self.assertEqual(params.scalefactor, (2.0, 0.0, 0.0, 0.0))
def test_net_builder(self):
net = cv.dnn.Net()
params = {
"kernel_w": 3,
"kernel_h": 3,
"stride_w": 3,
"stride_h": 3,
"pool": "max",
}
net.addLayerToPrev("pool", "Pooling", cv.CV_32F, params)
inp = np.random.standard_normal([1, 2, 9, 12]).astype(np.float32)
net.setInput(inp)
out = net.forward()
self.assertEqual(out.shape, (1, 2, 3, 4))
if __name__ == '__main__':
NewOpenCVTests.bootstrap()

@ -194,6 +194,13 @@ _PREDEFINED_TYPES = (
export_name="ExtractMetaCallback"
),
AliasTypeNode.class_("LayerId", "DictValue"),
AliasTypeNode.dict_("LayerParams",
key_type=PrimitiveTypeNode.str_(),
value_type=UnionTypeNode("DictValue", items=(
PrimitiveTypeNode.int_(),
PrimitiveTypeNode.float_(),
PrimitiveTypeNode.str_())
)),
PrimitiveTypeNode.int_("cvflann_flann_distance_t"),
PrimitiveTypeNode.int_("flann_flann_distance_t"),
PrimitiveTypeNode.int_("cvflann_flann_algorithm_t"),

Loading…
Cancel
Save