Ask Your Question
1

Error importing cv2 after compiling OpenCV from source (Python)

asked 2020-09-22 10:57:44 -0600

aqiff98 gravatar image

updated 2020-09-22 15:55:52 -0600

Hello guys, I am following two tutorials on Youtube on how we can compile OpenCV (4.4.0) from source using CMake and Visual Studio 2019. And currently, I'm using Windows with CUDA 10.1.

Tutorial 1: https://www.youtube.com/watch?v=_fqpY... Tutorial 2: https://www.youtube.com/watch?v=tjXkW...

After following step by step, including "Build" for the ALL_BUILD and "INSTALL" it, I couldn't use the OpenCV in the terminal, by importing python

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'cv2'

I also noticed that after obtaining OpenCV.sln, I do not have the "binding" folder like both of tutorial videos.

image description

The questions are: 1) Does the binding file is really that important ? 2) If its not that important, how can I use the openCV that has been compiled with my Python?

Edited

Below are the outputs from opencv_version_win32.exe

General configuration for OpenCV 4.4.0 =====================================
  Version control:               unknown

  Extra modules:
    Location (extra):            C:/Users/user/OpenCV_CUDA/opencv_contrib-4.4.0/modules
    Version control (extra):     unknown

  Platform:
    Timestamp:                   2020-09-22T19:42:44Z
    Host:                        Windows 10.0.18363 AMD64
    CMake:                       3.18.2
    CMake generator:             Visual Studio 16 2019
    CMake build tool:            C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/MSBuild/Current/Bin/MSBuild.exe
    MSVC:                        1923

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (17 files):         + SSSE3 SSE4_1
      SSE4_2 (2 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (1 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (5 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (7 files):      + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      YES
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe  (ver 19.23.28107.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MD /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MDd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.23.28105/bin/Hostx64/x64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP   /MD /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:fast     /MP /MDd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /INCREMENTAL:NO
    Linker flags (Debug):        /machine:x64  /debug /INCREMENTAL
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          cudart_static.lib nppc.lib nppial.lib nppicc.lib nppicom.lib nppidei.lib nppif.lib nppig ...
(more)
edit retag flag offensive close merge delete

Comments

you should run VS as an administrator

sturkmen gravatar imagesturkmen ( 2020-09-22 11:00:39 -0600 )edit

ive tried many times and its still not working. So, I've looked at people who faced the same problem but sadly they used linux/ubuntu not windows

aqiff98 gravatar imageaqiff98 ( 2020-09-22 23:08:36 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
1

answered 2020-09-22 12:03:47 -0600

updated 2020-09-22 12:27:02 -0600

You should have a bindings folder. To confirm whether the bindings were built, go tobuild/bin/[Release|Debug] and run

opencv_version_win32.exe

In the output look for python3 under To be built: as shown below OpenCV modules:

To be built: aruco bgsegm bioinspired calib3d ccalib core cudaarithm cudabgsegm cudacodec cudafeatures2d cudafilters cudaimgproc cudalegacy cudaobjdetect cudaoptflow cudastereo cudawarping cudev datasets dnn dnn_objdetect dnn_superres dpm face features2d flann fuzzy gapi hfs highgui img_hash imgcodecs imgproc intensity_transform line_descriptor mcc ml objdetect optflow phase_unwrapping photo plot python2 python3 quality rapid reg rgbd saliency shape stereo stitching structured_light superres surface_matching text tracking ts video videoio videostab world xfeatures2d ximgproc xobjdetect xphoto

If it is not there then you haven't built the bindings and you won't be able to call your build from python.

I take it you need CUDA bindings? If not I would just use the pre-built python OpenCV libs.

edit flag offensive delete link more

Comments

opencv_version_win32.exe

It's there in the directory like how you explained including the output for To be built. The question is what to do next? I still can't call it through the python in command prompt

aqiff98 gravatar imageaqiff98 ( 2020-09-22 12:35:02 -0600 )edit

So python3 is in the list after To be built not after Disabled or Unavailable? Do you have the bindings file (cv2.cp37-win_amd64.pyd) in the below folder build\lib\python3[Release|Debug]\cv2.cp37-win_amd64.pyd

cudawarped gravatar imagecudawarped ( 2020-09-22 12:43:24 -0600 )edit

what is output of opencv_version_win32.exe

check related part ( below is mine)

  Python 3:
    Interpreter:                 C:/Program Files/Python38/python.exe (ver 3.8.5)
    Libraries:                   C:/Program Files/Python38/libs/python38.lib (ver 3.8.5)
    numpy:                       C:/Users/sturk/AppData/Roaming/Python/Python38/site-packages/numpy/core/include (ver 1.19.1)
    install path:                C:/Program Files/Python38/Lib/site-packages/cv2/python-3.8
sturkmen gravatar imagesturkmen ( 2020-09-22 12:47:32 -0600 )edit

The only python output is

Python (for build):            C:/Users/user/AppData/Local/Programs/Python/Python37/python.exe
aqiff98 gravatar imageaqiff98 ( 2020-09-22 13:11:09 -0600 )edit

do you install numpy before compile opencv?

sturkmen gravatar imagesturkmen ( 2020-09-22 13:13:05 -0600 )edit

Yes I did install it, even in the cmake, it is recognized as python3_numpy_numpy_include_dir C:/Users/user/AppData/Local/Programs/Python/Python37/lib/site-packages/numpy/core/includ. and even python3_packages_path recognized as C:/Users/user/AppData/Local/Programs/Python/Python37/Lib/site-packages

aqiff98 gravatar imageaqiff98 ( 2020-09-22 13:53:50 -0600 )edit

Thank you for posting the output. So it is clear the reason you have no python bindings is that they were not built or configured to be built. As you followed a guide which used the CMake GUI it is difficult to help you any further as you can't provide the command line options to CMake. I would suggest configuring CMake from the command line and before building inspecting the output to make sure python3 is next to To be built:. If it is still not listed then you can post your CMake commands and the full output here to get advice on what is missing. A good guide for configuring from the command line is here.

cudawarped gravatar imagecudawarped ( 2020-09-23 04:16:42 -0600 )edit

Now everything is solved, I have the bindings now but it has a new problem where I tried to import the library in Python but I received this error

    Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python38\lib\site-packages\cv2\__init__.py", line 96, in <module>
    bootstrap()
  File "C:\Python38\lib\site-packages\cv2\__init__.py", line 86, in bootstrap
    import cv2
ImportError: DLL load failed while importing cv2: The specified module could not be found.
aqiff98 gravatar imageaqiff98 ( 2020-09-23 07:33:37 -0600 )edit
1

Make sure the required dll's are on the system/user path. Scroll down to the Troubleshooting section part 3 here to see what could be missing. The most likely cause is that the path to OpenCV is not on your system/user path but its also possible the path to CUDA is missing if the existing system path was too long when you installed it.

cudawarped gravatar imagecudawarped ( 2020-09-23 08:16:59 -0600 )edit

Still, it could not solve the problem as the type of error shown is not the same as my error.

aqiff98 gravatar imageaqiff98 ( 2020-09-23 22:00:47 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-09-22 10:57:44 -0600

Seen: 6,935 times

Last updated: Sep 22 '20