Skip to content

OpenCV Python Wheel Build Issue: cv2 Missing Methods After Building from Source #909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
WoodieDudy opened this issue Oct 22, 2023 · 2 comments

Comments

@WoodieDudy
Copy link

WoodieDudy commented Oct 22, 2023

After building OpenCV Python wheel from source, the cv2 module seems to be missing its expected methods. When inspecting the module methods using dir(cv2), only the default Python module methods are returned.

Steps to Reproduce:

  1. Built the OpenCV Python wheel using the following Dockerfile:
FROM python:3.10

ENV DEBIAN_FRONTEND=noninteractive

RUN apt-get update && \
    apt-get install --no-install-recommends -y build-essential gcc software-properties-common curl

RUN apt-get install -y curl cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev libtbbmalloc2 \
                     libtbb-dev libjpeg-dev libpng-dev libtiff-dev libdc1394-dev

RUN python3.10 -m pip install --upgrade pip
RUN python3.10 -m pip install --upgrade wheel setuptools requests

ENV OPENCV_VERSION=4.8.1

WORKDIR /opencv

RUN git clone --recurse-submodules https://github.com/opencv/opencv-python.git
WORKDIR /opencv/opencv-python

RUN python3.10 -m pip install numpy

ENV CMAKE_ARGS="-D BUILD_SHARED_LIBS=OFF -D CMAKE_BUILD_TYPE=RELEASE -D WITH_FFMPEG=ON -D BUILD_opencv_python3=ON"
ENV ENABLE_HEADLESS=1
RUN python3.10 -m pip wheel . --verbose
  1. Entered the container to install the built wheel:
pip install opencv_python_headless-4.8.1+7cfd1ee-cp310-cp310-linux_x86_64.whl
  1. Checked available methods:
import cv2
print(dir(cv2))

Expected Behavior:
The cv2 module should have its usual methods available.

Actual Behavior:
Only the default Python module methods were returned:
['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

Additional Question:
I want to ensure that OpenCV is built with support for the avc1 codec and that no additional installations are required on the target system. I believe that adding the flags -D BUILD_SHARED_LIBS=OFF -D WITH_FFMPEG=ON will statically link the required libraries. Can you confirm or provide corrections if I'm mistaken?

@kokamido
Copy link

It looks like the described behavior caused by the /opencv/opencv-python/cv2 folder. Python treats it as a cv2 package if python is runing in WORKDIR /opencv/opencv-python

image

@WoodieDudy
Copy link
Author

Oh, really
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants