When build opencv, could not find the file "cv2.so"
Asked Answered
M

6

7

I compiled OpenCV 3.0.0 with python2.7 in Debian system at a Board with a extended SD card (BeagleBone Black).

After OpenCV installed, there seems no error. There are many generated files in the 'Lib' folder.

Those files are named like:

libopencv_core.so
libopencv_dataset.so
libopencv_imgproc.so ...

I could not find the file cv2.so, even searched the whole file system.

I have tried several times, just can not find cv2.so.

Who knows the reason? Thanks.

make config checked, seems correct:

sudo cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local -D
Multinuclear answered 27/6, 2016 at 10:10 Comment(0)
M
9

Make sure you have numpy installed. Without numpy, cmake will say

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   NO
--     numpy:                       NO (Python wrappers can not be generated)
--     packages path:               lib/python2.7/dist-packages

install numpy, and it will say:

--   Python 2:
--     Interpreter:                 /usr/bin/python2.7 (ver 2.7.12)
--     Libraries:                   /usr/lib64/libpython2.7.so (ver 2.7.12)
--     numpy:                       /usr/local/lib/python2.7/dist-packages/numpy/core/include (ver 1.12.1)
--     packages path:               lib/python2.7/dist-packages
Mckenney answered 10/5, 2017 at 23:16 Comment(2)
my cmake shows only Interpreter in each python version, but doesnt show libraries nor numpy nor packages pathTavey
I also did not see the numpy or packages path until I add this to my cmake ` -D PYTHON3_NUMPY_INCLUDE_DIRS=/path_to_python3.5/site-packages/numpy/core/include/ -D PYTHON3_NUMPY_VERSION=1.14.0 `Australoid
F
4

check your cmake variables contain:

-D BUILD_NEW_PYTHON_SUPPORT=ON

-D BUILD_PYTHON_SUPPORT=ON

or

check if cmake shows the message:

variables were not used by the project: BUILD_NEW_PYTHON_SUPPORT BUILD_PYTHON_SUPPORT

if these two variables all not used,opencv will not generates the cv2.so,means python module will not usable even though build successfully (with no error message).

I don't know why those happened while python environment was alright.

Fleuron answered 23/9, 2016 at 3:7 Comment(0)
N
4

For me the solution was the following:

  1. Remove CMakeCache.txt file if it exists in the build directory.
  2. Add the following flags (if they are missing) to the build command (you might have to change the paths if they are different on your system):

    -D PYTHON_INCLUDE_DIR=/usr/include/python2.7
    -D PYTHON_LIBRARY=/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so
    

The entire build command that worked for me was this:

cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
-D PYTHON_EXECUTABLE=/usr/bin/python \
-D PYTHON_INCLUDE_DIR=/usr/include/python2.7 \
-D PYTHON_LIBRARY=/usr/lib/python2.7/config-x86_64-linux-gnu/libpython2.7.so \
-D BUILD_EXAMPLES=ON ..
Noaccount answered 20/10, 2017 at 21:2 Comment(0)
T
1

I solved the problem and I hope the below notes might help someone.
I'll describe first what I had when the issue was happening. On my device I had python2.7 and python3.6 installed. I wanted to use python3.7. So, I installed it via apt install as follows:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.7

Since OpenCV needs the python-dev version, I also installed it via apt install python-dev. Unfortunately, that was the problem. This was bound with Python3.6. Because I told cmake to use Python3.7, cmake could not generate the required opencv package since it cannot find the required header files.
When you run cmake, if you see that the "install path" is empty, then you can be sure that you have the same problem I described. Example:

--   Python 3:
--     Interpreter:                 /usr/bin/python3 (ver 3.7.8)
--     numpy:                       /usr/local/lib/python3.7/dist-packages/numpy/core/include (ver 1.19.0)
--     install path:                -
-- 
--   Python (for build):            /usr/bin/python3

It must show it like this:

--     install path:                lib/python3.7/dist-packages/cv2/python-3.7

To solve the problem, uninstall python-dev and install the one specific for python3.7. Example:

sudo apt-get install python3.7-dev

At the end, you should get a single ".so" file. For me, it was placed under:
/usr/local/lib/python3.7/dist-packages/cv2/python-3.7/cv2.cpython-37m-aarch64-linux-gnu.so

Tot answered 22/7, 2020 at 3:31 Comment(0)
B
0

I think you may try to find cv2.so in your download OpenCV directory.

I meet the same problem and finally find it in

dir = "./myopencv/opencv/build/lib".

When I try to solve the problem, I find that cv2.so may be a collection of some .so files. As if your version is matched, even you getting it from other's computer is OK.

Bradford answered 6/6, 2017 at 2:17 Comment(0)
M
0

In newer version of opencv, the cv2.so file is rename into something like cv2.build_information.so . So there's a chance that you're looking for wrong file name.

So after building opencv with python interpreter, check inside build/lib for corresponding file name. In my case, cv2.so is renamed as cv2.cpython-37m-x86_64-linux-gnu.so

build/lib/python3/
└── cv2.cpython-37m-x86_64-linux-gnu.so
Mantle answered 23/5, 2019 at 2:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.