Algomorph/pyboostcvconverter

No issue but a question: How to use pbcvt::fromMatToNDArray()?

Closed this issue · 13 comments

I cloned master and I successfully built with Visual Studio 2017, and I got the "pyboostcvconverter.hpp" along with "pbcvt_py3.lib". I want to use pbcvt::fromMatToNDArray() in my project. I both included the header "pyboostcvconverter.hpp" and linked "pbcvt_py3.lib", but I got these errors when building:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C3861	'to_python_use_handle_instead': identifier not found	TMP	C:\dev\proj\dependencies\boost\boost\python\converter\arg_to_python.hpp	177	
Severity	Code	Description	Project	File	Line	Suppression State
Error	C2027	use of undefined type 'boost::python::converter::detail::cannot_convert_raw_PyObject<T *>'	TMP	C:\dev\proj\dependencies\boost\boost\python\converter\arg_to_python.hpp	177	

It looked like there were some problems with boost? Did I miss something?
Note: There was no such problem before adding pyboostcvconverter.

@rathaROG, I haven't seen these errors before, but looks like your project is not using some headers as it should, or perhaps not using some defines as it should. Compile the boilerplate library separately and make sure that works first, then, assuming it works, see what is it you're missing. I recall that some things are tricky, like having certain defines in any of your .cpp files that use numpy.

@rathaROG, I haven't seen these errors before, but looks like your project is not using some headers as it should, or perhaps not using some defines as it should. Compile the boilerplate library separately and make sure that works first, then, assuming it works, see what is it you're missing. I recall that some things are tricky, like having certain defines in any of your .cpp files that use numpy.

Thank you so much for your quick reply! My project is kind of a very big one.. however, I will try my best to check everything and will be again here to clarify.

@rathaROG , I would recommend simply including the headers & source file of pbcvt in your project (adjusted to your liking, e.g. changing the module name and such) instead of linking the library. As the README states, the code is intended to be used as boilerplate code as opposed to a separate library.

@rathaROG , I would recommend simply including the headers & source file of pbcvt in your project (adjusted to your liking, e.g. changing the module name and such) instead of linking the library. As the README states, the code is intended to be used as boilerplate code as opposed to a separate library.

Sorry that I didn't read at the first place... but I still had the same issues while building. Here is what I did: I added the header & source files directly into my project and removed the "pbcvt_py3.lib". I also modified the include lines to according to my project. The same issues appeared with the older version of prebuit boost 1.69.0 and my own build 1.70.0.

My temporary dirty solution was to delete the line 177
cannot_convert_raw_PyObject<T*>::to_python_use_handle_instead();
from this boost header "boost\python\converter\arg_to_python.hpp"

Everything was built successfully, but my program had this issue:

Exception thrown: read access violation.
**pbcvt_ARRAY_API** was nullptr.

The issue is from line 105 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum); of "pyboost_cv4_converter.cpp"

My code:

...
cv::Mat tmp = inputImage(cv::Rect(x, y, size, size)); // crop the cv::Mat inputImage
PyObject* tmp_py = pbcvt::fromMatToNDArray(tmp); // convert tmp to PyObject and error is here
object result = analyze_this_face(tmp_py); // just call a function in python script to analyze the tmp_py
...

I really want to make it work but I have been stuck here all day.

Aha, I see. I looks like the error is stemming from the OpenCV 4 portion of the code, which was recently added. It might be that the OpenCV 4 API for python has been rapidly changing, since the fourth version hasn't been around for long, and the code here is outdated / broken due to the newer changes.

Which exact release of OpenCV are you using?
Also, which python release?

My immediate recommendation is to look over the pyboost_cv4_converter.cpp code and compare it to the corresponding cv2.cpp file in the current OpenCV:
https://github.com/opencv/opencv/blob/master/modules/python/src2/cv2.cpp

Try to see if {something}_ARRAY_API is defined somehow differently, maybe?
If this is really a bug in the boilerplate code and you just happen to be the first person who caught it and fixed it, it would be a great benefit to the community and I would be very grateful as well.

Aha, I see. I looks like the error is stemming from the OpenCV 4 portion of the code, which was recently added. It might be that the OpenCV 4 API for python has been rapidly changing, since the fourth version hasn't been around for long, and the code here is outdated / broken due to the newer changes.

Which exact release of OpenCV are you using?
Also, which python release?

My immediate recommendation is to look over the pyboost_cv4_converter.cpp code and compare it to the corresponding cv2.cpp file in the current OpenCV:
https://github.com/opencv/opencv/blob/master/modules/python/src2/cv2.cpp

Try to see if {something}_ARRAY_API is defined somehow differently, maybe?
If this is really a bug in the boilerplate code and you just happen to be the first person who caught it and fixed it, it would be a great benefit to the community and I would be very grateful as well.

Hi @Algomorph !

First, I am please to tell you how I fixed my first problem below:

Severity	Code	Description	Project	File	Line	Suppression State
Error	C3861	'to_python_use_handle_instead': identifier not found	TMP	C:\dev\proj\dependencies\boost\boost\python\converter\arg_to_python.hpp	177	
Severity	Code	Description	Project	File	Line	Suppression State
Error	C2027	use of undefined type 'boost::python::converter::detail::cannot_convert_raw_PyObject<T *>'	TMP	C:\dev\proj\dependencies\boost\boost\python\converter\arg_to_python.hpp	177

Actually, there is nothing wrong with the boost header "arg_to_python.hpp". To proper fix this error, just head to here on stackoverflow for detail explanation. Therefore, I just have to simply change the last line of my code to:

object result = analyze_this_face(boost::python::handle<>(tmp_py));

However, my second issue of line 105 PyObject* o = PyArray_SimpleNew(dims, _sizes, typenum); of "pyboost_cv4_converter.cpp" has not been fixed yet. The issue appeared with both OpenCV 4.0.1 and 4.1.0. Python 3.7.3 is used in my current state. I still haven't looked at the "cv2.cpp" yet. I will try to do as what you suggested tomorrow. Thank you so much for always helping!

Hi @Algomorph !

I replaced the whole of class NumpyAllocator based on the "cv2.cpp" of OpenCV 4.1.0, but the issue was still there. I attached the screenshot below. (Note: Python 3.7.3 & Numpy 1.16.2)

Capture

@rathaROG, I applaud your persistence. The only way to get anything done in world of code is dogged persistence and zero negative feelings when something doesn't work. Revert your changes and try the latest version of OpenCV 3 instead. My intuition tells me that OpenCV 3 stuff should still work.

I'm working on my PhD proposal ATM, so my availability is limited. The preliminary defence is on June 27, if all goes as planned. Hence I'm planning to address any lingering issues here, as well as add continuous integration to this project some time in the second half of July.

Hi @Algomorph ! I understand your situation. Take your time and good luck!

Thanks!

@rathaROG , I suppose you're using Windows 10 + Visual Studio 2017 for your build?

I couldn't replicate your issue in AppVeyor here:
https://ci.appveyor.com/project/Algomorph/pyboostcvconverter/builds/25296803/job/mm4ondaaxjedm8wr
The setup uses Windows 10, VS 2017, Python 3.7, and OpenCV 4.

@rathaROG , I suppose you're using Windows 10 + Visual Studio 2017 for your build?

I couldn't replicate your issue in AppVeyor here:
https://ci.appveyor.com/project/Algomorph/pyboostcvconverter/builds/25296803/job/mm4ondaaxjedm8wr
The setup uses Windows 10, VS 2017, Python 3.7, and OpenCV 4.

Hi @Algomorph !

Thank you so much for coming back.

I'm using the Windows 10 19H1, Python 3.7.3 x64, OpenCV 4.1.0, Boost 1.69.0.

I have no problem when C++ is embedded in Python which is executed as python script (.py), and everything is working fine. The issue happens when Python is embedded in C++ which is executed as .exe. My main program is C++ (.exe), and some tasks with deep learning are easier to deal with Python, so I want my main C++ to send some cv::Mat images to Python to analyze, and the issue seems like the Python can't read or access something here. Could you please simply conduct and test a similar scenario?

@rathaROG , I don't really support going calling Python from C++, I'm not sure what you're trying to accomplish per se (calling python from C++? how exactly? why do you need the C++ conversions to happen?), but this particular repo isn't for that at all, as I think the README clearly states in the very beginning.

If you want an easy way to transfer things from C++ to python, why not just write the OpenCV matrices as images (exr if you need float) and then just read them in your python script?