Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Invalid conversion error in cv2 file while installing opencv 3.3.0 on Raspberry Pi Stretch

I am trying to build opencv 3.3.0 on raspberry pi 3 but facing an error at 93% saying invalid conversion. Details are provided below any help will be greatly appreciated. please help.

This error message is showing up.

 /home/pi/opencv-3.3.0/modules/python/src2/cv2.cpp: In function ‘bool pyopencv_to(PyObject*, T&, const char*) [with T = cv::String; PyObject = _object]’:/home/pi/opencv-3.3.0/modules/python/src2/cv2.cpp:854:34: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive] char* str = PyString_AsString(obj);In file included from /home/pi/opencv-3.3.0/modules/python/src2/cv2.c

I Tried these commands

cd ~/opencv-3.3.0/

$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
    -D CMAKE_INSTALL_PREFIX=/usr/local \
    -D INSTALL_PYTHON_EXAMPLES=ON \
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
    -D BUILD_EXAMPLES=ON ..

#cv2.cpp code at error location.

template<>
bool pyopencv_to(PyObject* obj, String& value, const char* name)
{
 (void)name;
 if(!obj || obj == Py_None)
    return true;
 char* str = PyString_AsString(obj);
 if(!str)
    return false;
 value = String(str);
 return true;

}

like image 892
Raviraj Gardi Avatar asked Jul 02 '19 05:07

Raviraj Gardi


2 Answers

I'd use sudo apt-get install python3-opencv; but if you need the CMake compiled version, like for ROS, then it appears to be a bug in OpenCV; after I did the following change at opencv3/modules/python/src2/cv2.cpp lin 885, it compiled for me. Change...

char* str = PyString_AsString(obj);

to

const char* str = PyString_AsString(obj);

like image 108
Void Avatar answered Nov 18 '22 03:11

Void


I had encountered same phenomenon. It was hard since compile takes much time and I spent more than half day to resolve this.

TLDR:

1) Building opencv is not recommended any more. Give up building from source.

2) Raspberry pie provides opencv wheel. Try this.

sudo pip3 install opencv-contrib-python

3) If it doesn't work, simply use apt-get

sudo apt-get install python-opencv

4) For python3,

sudo apt-get install python3-opencv

Please let me know if it works. :)

like image 2
Jin Avatar answered Nov 18 '22 02:11

Jin