I'm unable to install opencv-contrib-python in

2020-03-01 09:38发布

I tried installing opencv-contrib-python but I'm unable to get it to work on docker. It says Could not find a version that satisfies the requirement opencv-contrib-python

I tried,

pip install opencv-contrib-python-headless

Then, I tired https://github.com/cassiobotaro/docker-opencv-contrib/blob/master/Dockerfile and I also tried,

    FROM python:3.5-alpine

    COPY . /app
    WORKDIR /app


    RUN apk add --no-cache ca-certificates
    RUN apk add --no-cache git build-base musl-dev alpine-sdk cmake clang clang-dev make gcc g++ libc-dev linux-headers

    RUN mkdir /tmp/opencv
    WORKDIR /tmp/opencv
    RUN wget -O opencv.zip https://github.com/opencv/opencv/archive/3.4.1.zip
    RUN unzip opencv.zip
    RUN wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.4.1.zip
    RUN unzip opencv_contrib.zip
    RUN mkdir /tmp/opencv/opencv-3.4.1/build

    WORKDIR /tmp/opencv/opencv-3.4.1/build
    RUN cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D OPENCV_EXTRA_MODULES_PATH=/tmp/opencv/opencv_contrib-3.4.1/modules -D BUILD_DOCS=OFF BUILD_EXAMPLES=OFF -D BUILD_TESTS=OFF -D BUILD_PERF_TESTS=OFF -D BUILD_opencv_java=OFF -D BUILD_opencv_python=OFF -D BUILD_opencv_python2=OFF -D BUILD_opencv_python3=OFF ..
    RUN make -j4
    RUN make install

    RUN rm -rf /tmp/opencv


    RUN pip3 install -r requirements.txt

CMD ["app.py"] 

But I cannot get either one of it to work. PLease let me know how can I install the above in docker by just the requirements file?

More references (Things that I've tried) : Unable to install/run docker with opencv

and

from .cv2 import * ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

2条回答
戒情不戒烟
2楼-- · 2020-03-01 09:52

I had the same issue. I was using python-slim. It occurs due to run time dependencies. Add the following code snippet in your DockerFile to install run time dependencies.

Install OpenCV's runtime dependencies

RUN apt-get update RUN apt-get -y install libglib2.0-0 RUN apt-get -y install libsm6 \ libxrender-dev \ libxext6

查看更多
地球回转人心会变
3楼-- · 2020-03-01 10:00

My guess is that you're seeing the failure on the -alpine version because the opencv package is a binary distribution (it's not just Python code), and it probably hasn't been built for Alpine. Alpine uses a C library that is different from everything else (Alpine uses MUSL libc while just about everthing else uses Glibc); there is some possibility that the opencv codebase won't even build for MUSL. Or maybe it's just that nobody has gotten around to building a binary package. In either case, you're better off with one of the following options:

If I use the stock python:3.5 image (not the Alpine one) it Just Works:

$ docker run -it --rm python:3.5 bash
root@95c81040aeaf:/# pip install opencv-contrib-python-headless
Collecting opencv-contrib-python-headless
  Downloading https://files.pythonhosted.org/packages/c2/50/2427b286652cf64ea3618d08bfba38c04b6571f6f2c054e950367a2f309f/opencv_contrib_python_headless-3.4.3.18-cp35-cp35m-manylinux1_x86_64.whl (24.0MB)
    100% |████████████████████████████████| 24.1MB 2.4MB/s
Collecting numpy>=1.11.1 (from opencv-contrib-python-headless)
  Downloading https://files.pythonhosted.org/packages/86/04/bd774106ae0ae1ada68c67efe89f1a16b2aa373cc2db15d974002a9f136d/numpy-1.15.4-cp35-cp35m-manylinux1_x86_64.whl (13.8MB)
    100% |████████████████████████████████| 13.8MB 4.7MB/s
Installing collected packages: numpy, opencv-contrib-python-headless
Successfully installed numpy-1.15.4 opencv-contrib-python-headless-3.4.3.18
root@95c81040aeaf:/# python
Python 3.5.6 (default, Nov 16 2018, 22:45:03)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

If I use the 3.5-slim tag, I see the same error you reported:

root@63dca11a527f:/# python
Python 3.5.5 (default, May  5 2018, 03:17:29)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory
>>>

As we can see from a package query, that library is owned by the libglib2.0 package, which is apparently not installed by default in the -slim version of the Python image. We can fix that:

# apt-get update
# apt-get -y install libglib2.0

And now it runs as expected:

root@63dca11a527f:/# python
Python 3.5.5 (default, May  5 2018, 03:17:29)
[GCC 4.9.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>>

You could build your own image incorporating this fix using a Dockerfile like:

FROM python:3.5-slim
RUN apt-get update && apt-get -y install libglib2.0; apt-get clean
RUN pip install opencv-contrib-python-headless

Update

Regarding your comment: if you want a package to be available to code running in your container then, yes, you have to install it. Where else will it come from?

If opencv-contrib-python-headless is included in your requirements.txt, then what have posted in the comments should work just fine:

FROM python:3.5
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]

If you requirements.txt does not include this (why not?), you would need to explicitly install it:

FROM python:3.5
RUN pip install opencv-contrib-python-headless
COPY . /app
WORKDIR /app
RUN pip3 install -r requirements.txt
ENTRYPOINT ["python3"]
CMD ["app.py"]
查看更多
登录 后发表回答