我尝试安装 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中?
更多参考(我尝试过的事情):无法使用opencv安装/运行docker
和
从.cv2导入*ImportError:libgthread-2.0.so.0:无法打开共享对象文件:没有此类文件或目录
我的猜测是,-alpine由于opencv软件包是二进制发行版(不仅是Python代码),而且版本不是在Alpine上构建的,因此您会看到该版本的失败。Alpine使用的C库与其他所有库都不相同(Alpine使用MUSL libc,而其他几乎所有的库都使用Glibc);opencv代码库甚至可能无法为MUSL构建。也许仅仅是没有人去构建二进制包。无论哪种情况,最好使用以下选项之一:
-alpine
opencv
如果我使用普通的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标记,则会看到与您报告的错误相同的错误:
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 >>>
从包查询中可以看到,该库归libglib2.0包所有,-slim在Python映像版本中默认未安装该库。我们可以解决此问题:
libglib2.0
-slim
# 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:
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,则评论中发布的内容应该可以正常工作:
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"]