我无法在docker中安装opencv-contrib-python

时间:2018-11-15 21:34:24

标签: python docker opencv

我尝试安装 opencv-contrib-python ,但无法在docker上运行。它说找不到满足opencv-contrib-python要求的版本

我尝试过

pip install opencv-contrib-python-headless

那我累了 https://github.com/cassiobotaro/docker-opencv-contrib/blob/master/Dockerfile  而且我也尝试过

    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"] 

但是我不能任其工作。请让我知道如何仅通过需求文件将以上内容安装在Docker中?

更多参考(我尝试过的事情): Unable to install/run docker with opencv

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

2 个答案:

答案 0 :(得分:7)

我的猜测是您看到-alpine版本的失败,因为opencv软件包是二进制发行版(不仅是Python代码),而且可能不是为高山Alpine使用的C库与其他所有库都不相同(Alpine使用MUSL libc,而其他几乎所有的库都使用Glibc); opencv代码库甚至可能无法为MUSL构建。也许仅仅是没有人去构建二进制包。无论哪种情况,最好使用以下其中一种方法:

如果我使用普通的python:3.5图像(而不是Alpine图像),它将正常工作:

$ 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
>>>

如果我使用3.5-slim标签,则会看到与您报告的相同的错误:

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
>>>

package query中可以看到,该库归libglib2.0包所有,显然在-slim版本的Python映像中默认未安装该包。我们可以解决此问题:

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

现在它可以按预期运行:

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
>>>

您可以使用 Dockerfile喜欢:

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

更新

关于您的评论:如果您想使用软件包进行编码 然后在您的容器中运行,是的,您必须先安装它。哪里 否则它将来自?

如果您的帐户中包含opencv-contrib-python-headless requirements.txt,那么评论中发布的内容应该起作用 很好:

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

如果您requirements.txt不包含此内容(为什么不包含),您会 需要明确安装它:

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"]

答案 1 :(得分:4)

我有同样的问题。我正在使用python-slim。它是由于运行时相关性而发生的。在您的DockerFile中添加以下代码片段以安装运行时依赖项。

安装OpenCV的运行时依赖项

RUN apt-get更新RUN apt-get -y安装libglib2.0-0 RUN apt-get -y安装libsm6 \ libxrender-dev \ libxext6