diff --git a/ext/opencv/dnn.cpp b/ext/opencv/dnn.cpp index 84d5734..3af50f8 100644 --- a/ext/opencv/dnn.cpp +++ b/ext/opencv/dnn.cpp @@ -19,7 +19,7 @@ namespace rubyopencv { VALUE image, options; rb_scan_args(argc, argv, "11", &image, &options); - cv::Mat *b; + cv::Mat *b = NULL; cv::Mat *m = Mat::obj2mat(image); try { @@ -38,7 +38,7 @@ namespace rubyopencv { } b = new cv::Mat(r); - } catch(cv::Exception& e) { + } catch(cv::Exception& e) { delete b; Error::raise(e); } @@ -48,7 +48,7 @@ namespace rubyopencv { // Net readNetFromCaffe(const String &prototxt, const String &caffeModel = String()); VALUE rb_read_net_from_caffe(VALUE self, VALUE prototxt, VALUE caffe_model) { - cv::dnn::experimental_dnn_v1::Net *net; + cv::dnn::experimental_dnn_v1::Net *net = NULL; try { net = new cv::dnn::experimental_dnn_v1::Net(cv::dnn::readNetFromCaffe(StringValueCStr(prototxt), StringValueCStr(caffe_model))); @@ -60,6 +60,34 @@ namespace rubyopencv { return Dnn::Net::net2obj(net); } + // Net readNetFromTorch(const String &model, bool isBinary) + VALUE rb_read_net_from_tensorflow(VALUE self, VALUE model) { + cv::dnn::experimental_dnn_v1::Net *net = NULL; + + try { + net = new cv::dnn::experimental_dnn_v1::Net(cv::dnn::readNetFromTensorflow(StringValueCStr(model))); + } catch(cv::Exception& e) { + delete net; + Error::raise(e); + } + + return Dnn::Net::net2obj(net); + } + + // Net readNetFromTorch(const String &model, bool isBinary) + VALUE rb_read_net_from_torch(VALUE self, VALUE model) { + cv::dnn::experimental_dnn_v1::Net *net = NULL; + + try { + net = new cv::dnn::experimental_dnn_v1::Net(cv::dnn::readNetFromTorch(StringValueCStr(model))); + } catch(cv::Exception& e) { + delete net; + Error::raise(e); + } + + return Dnn::Net::net2obj(net); + } + void init() { VALUE opencv = rb_define_module("Cv"); @@ -67,6 +95,8 @@ namespace rubyopencv { rb_define_singleton_method(rb_module, "blob_from_image", RUBY_METHOD_FUNC(rb_blob_from_image), -1); rb_define_singleton_method(rb_module, "read_net_from_caffe", RUBY_METHOD_FUNC(rb_read_net_from_caffe), 2); + rb_define_singleton_method(rb_module, "read_net_from_tensorflow", RUBY_METHOD_FUNC(rb_read_net_from_tensorflow), 1); + rb_define_singleton_method(rb_module, "read_net_from_torch", RUBY_METHOD_FUNC(rb_read_net_from_torch), 1); Dnn::Net::init(rb_module); } diff --git a/ext/opencv/dnn_net.cpp b/ext/opencv/dnn_net.cpp index 0bbeba5..0486856 100644 --- a/ext/opencv/dnn_net.cpp +++ b/ext/opencv/dnn_net.cpp @@ -71,7 +71,6 @@ namespace rubyopencv { cv::dnn::experimental_dnn_v1::Net* selfptr = obj2net(self); cv::Mat* m = NULL; - // cv::Mat m; try { cv::Mat r; @@ -82,34 +81,13 @@ namespace rubyopencv { r = selfptr->forward(StringValueCStr(output_name)); } - m = new cv::Mat(r.reshape(1, 1)); - // m = r; + m = new cv::Mat(r); } catch(cv::Exception& e) { delete m; Error::raise(e); } - // int indxCls[4] = { 0, 0, 401, 1 }; - // int cls = m->at(indxCls); - return Mat::mat2obj(m); - - // const long size = m->size[2]; - // return(ULL2NUM(m.size[2])); - // VALUE detected_objects = rb_ary_new_capa(size); - // for (long i = 0; i < size; i++) { - // int indxCls[4] = { 0, 0, i, 1 }; - // int cls = m->at(indxCls); - // rb_ary_store(detected_objects, i, INT2NUM(cls)); - // } - // - // return detected_objects; - - // cv::Point classIdPoint; - // double confidence; - // cv::minMaxLoc(m.reshape(1, 1), 0, &confidence, 0, &classIdPoint); - // int classId = classIdPoint.x; - // return(INT2NUM(classId)); } // bool empty() const diff --git a/ext/opencv/mat.cpp b/ext/opencv/mat.cpp index 2700feb..7c5b229 100644 --- a/ext/opencv/mat.cpp +++ b/ext/opencv/mat.cpp @@ -6,6 +6,7 @@ #include "mat_imgproc.hpp" #include "mat_drawing.hpp" #include "scalar.hpp" +#include "size.hpp" #include "rect.hpp" #include "error.hpp" @@ -417,6 +418,20 @@ namespace rubyopencv { return INT2NUM(dataptr->depth()); } + VALUE rb_size(int argc, VALUE *argv, VALUE self) { + VALUE i; + rb_scan_args(argc, argv, "01", &i); + + const cv::Mat* dataptr = obj2mat(self); + + if (NIL_P(i)) { + cv::Size *s = new cv::Size(dataptr->size()); + return Size::size2obj(s); + } else { + return INT2NUM(dataptr->size[NUM2INT(i)]); + } + } + /* * Returns number of channels of the matrix. * @@ -1222,6 +1237,7 @@ namespace rubyopencv { rb_define_method(rb_klass, "dims", RUBY_METHOD_FUNC(rb_dims), 0); rb_define_method(rb_klass, "depth", RUBY_METHOD_FUNC(rb_depth), 0); rb_define_method(rb_klass, "channels", RUBY_METHOD_FUNC(rb_channels), 0); + rb_define_method(rb_klass, "size", RUBY_METHOD_FUNC(rb_size), -1); rb_define_method(rb_klass, "[]", RUBY_METHOD_FUNC(rb_aref), -2); rb_define_alias(rb_klass, "at", "[]"); diff --git a/ext/opencv/size.hpp b/ext/opencv/size.hpp index b283dff..9308fc2 100644 --- a/ext/opencv/size.hpp +++ b/ext/opencv/size.hpp @@ -6,6 +6,7 @@ namespace rubyopencv { namespace Size { void init(); cv::Size* obj2size(VALUE obj); + VALUE size2obj(cv::Size* ptr); } } #endif // RUBY_OPENCV_SIZE_H