gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 21:22:32 29 4
gpt4 key购买 nike

我尝试安装 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

最佳答案

我的猜测是您在 -alpine 版本上看到了失败,因为 opencv 包是一个二进制分发包(它不仅仅是 Python 代码),而且它可能还没有为 Alpine build 。 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-0 包所有,在 Python 镜像的 -slim 版本中显然没有默认安装该包。我们可以解决这个问题:

# apt-get update
# apt-get -y install libglib2.0-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-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"]

关于python - 我无法在 docker 中安装 opencv-contrib-python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53328226/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com