How to Solve Opencv error: assertion failed + error: (- 215)

When trying to write the ROS program of python2.7 and using the import CV2 statement, the program may automatically use the opencv3.3.1 – dev (path/opt/ROS/kinetic/lib/python2.7/dist packages/CV2. So) of ROS, rather than the higher version of OpenCV (path/usr/local/lib/python2.7/dist packages/CV2. So) installed by ourselves

Some of my programs use the face detection function provided by opencv’s DNN module

DNN = "CAFFE"
if DNN == "CAFFE":
    modelFile = "res10_300x300_ssd_iter_140000_fp16.caffemodel"
    configFile = "deploy.prototxt"
    net = cv2.dnn.readNetFromCaffe(configFile, modelFile)

When executing with ROS code, the program reports an error

[ INFO:0] Initialize OpenCL runtime...
OpenCV Error: Assertion failed (input.dims == 4 && (input.type() == 5 || input.type() == 6)) in finalize, file /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/layers/convolution_layer.cpp, line 78
Traceback (most recent call last):
  File "detect_faces.py", line 41, in <module>
    detections = net.forward()
cv2.error: /tmp/binarydeb/ros-kinetic-opencv3-3.3.1/modules/dnn/src/layers/convolution_layer.cpp:78: error: (-215) input.dims == 4 && (input.type() == 5 || input.type() == 6) in function finalize

Emmm… Tried several methods, the most effective one is to delete cv2.so in the ROS package and let the program use our own higher version of OpenCV. In order to prevent problems in the future, we just put the cv2.so of ROS into the trash instead of deleting it directly. The steps are as follows:

sudo easy_install trash-cli
# trash-cli is a software to move files into trash and recover them

sudo trash-put /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so
# This will work.

# If the python2.7 package also has a low version of opencv, the statement to install the specific version yourself is
sudo pip2 install opencv-python==3.4.0.12

Then run the program, no error will be reported

Similar Posts: