使用pip在docker容器中安装opencv时遇到问题

时间:2022-09-19 07:45:08

I want to build a python docker container that has scikit-learn, opencv, and numpy. Unfortunately, I couldn't find a pre-built container that contained all these, but I did find the one below that contains numpy and scikit-learn.

我想构建一个具有scikit-learn,opencv和numpy的python docker容器。不幸的是,我找不到包含所有这些的预先构建的容器,但我确实发现下面的容器包含numpy和scikit-learn。

https://hub.docker.com/r/frolvlad/alpine-python-machinelearning/

I still needed to install opencv, so within my docker file I included a RUN pip install opencv-python. However, I keep on getting the error below:

我仍然需要安装opencv,所以在我的docker文件中我包含了一个RUN pip install opencv-python。但是,我继续收到以下错误:

Could not find a version that satisfies the requirement opencv-python (from version: ) No matching distribution found for opencv-python

找不到满足要求的版本opencv-python(来自版本:)没有找到opencv-python的匹配发行版

Every single thing I have read online says that a pip install opencv-python will work, but it isn't working for me for some reason. Is it a problem with the python package maybe?

我在网上看过的每一件事都说,一个pip install opencv-python会起作用,但由于某些原因它不适用于我。这可能是python包的问题吗?

Any help is appreciated

任何帮助表示赞赏

Also, I will include my full Dockerfile below, I am aiming to use openFaas, which is a serverless framework, so my Dockerfile might look odd:

另外,我将在下面包含我的完整Dockerfile,我的目标是使用openFaas,这是一个无服务器框架,所以我的Dockerfile可能看起来很奇怪:

FROM frolvlad/alpine-python-machinelearning

RUN apk update
RUN apk upgrade

# Alternatively use ADD https:// (which will not be cached by Docker builder)
RUN apk --no-cache add curl \
    && echo "Pulling watchdog binary from Github." \
    && curl -sSL         
https://github.com/openfaas/faas/releases/download/0.8.0/fwatchdog > /usr/bin/fwatchdog \
    && chmod +x /usr/bin/fwatchdog \
    && apk del curl --no-cache

# Add non root user
RUN addgroup -S app && adduser -S -g app app
RUN chown app /home/app

RUN pip install -U pip

USER app

ENV PATH=$PATH:/home/app/.local/bin

WORKDIR /home/app/

RUN pip install opencv-python

RUN mkdir -p function
RUN touch ./function/__init__.py
WORKDIR /home/app/function/
RUN pip install --user app opencv-python

WORKDIR /home/app/
COPY function           function

ENV fprocess="python index.py"

HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1

CMD ["fwatchdog"]

2 个解决方案

#1


1  

I've just run into this issue as well. It turns out that this is not working because opencv-python does not have any prebuilt wheels for Alpine (the distribution you're using as your base docker image).

我也遇到过这个问题。事实证明这是行不通的,因为opencv-python没有任何用于Alpine的预构建轮(您使用的分发作为基础docker镜像)。

The conversation in this issue on the opencv-python package explains why this happens in greater detail. The TL;DR is: if you really need to use Alpine, you can try forcing the installation of the manylinux wheel for opencv-python, but this can break. Your best option if you need to keep Alpine is to build the module from source. Since you are running this on OpenFAAS, I suspect you will want to keep your size low, so building from source may be a good option for you.

这个问题在opencv-python包中的对话解释了为什么会更详细地发生这种情况。 TL; DR是:如果你真的需要使用Alpine,你可以尝试强制安装manylinux轮for opencv-python,但这可能会破坏。如果您需要保留Alpine,最好的选择是从源代码构建模块。由于您在OpenFAAS上运行此功能,我怀疑您希望保持较低的尺寸,因此从源代码构建可能是一个不错的选择。

If you're not attached to Alpine, I would suggest moving to a different base docker image. If you're not sure which image to use as your base, I would recommend python:3.7-slim, since it will come with Python already installed (substitute 3.7 for whichever version you are using, but really. . . 3.7 is nice). With this container, you can simply run pip install opencv-python numpy scipy to have all three of your desired packages installed. The rest of your Dockerfile should work mostly unmodified; you will just need to install/uninstall curl using apt instead of apk.

如果你没有加入Alpine,我建议你转移到另一个基地码头图像。如果您不确定使用哪个图像作为基础,我建议使用python:3.7-slim,因为它已经安装了Python(对于您使用的任何版本替换3.7,但实际上...... 3.7很好) 。使用此容器,您只需运行pip install opencv-python numpy scipy即可安装所有三个所需的软件包。你的Dockerfile的其余部分应该大部分都是未经修改的。你只需要使用apt而不是apk来安装/卸载curl。

#2


-1  

Actually you shouldn't install open-cv using pip, you have to install using the OpenCV package. If you are using Windows:

实际上你不应该使用pip安装open-cv,你必须使用OpenCV软件包安装。如果您使用的是Windows:

http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

and then head to /opencv/build/python/xxxx folder and copy the folder to \Python\Pythonxxx\Lib\site-packages (xxx beeing your Python version)

然后转到/ opencv / build / python / xxxx文件夹并将文件夹复制到\ Python \ Pythonxxx \ Lib \ site-packages(xxx beeing your Python version)

And if you are on a Linux environment, simply:

如果您在Linux环境中,只需:

 sudo apt-get install python-opencv

Alternatively, you can install using the wheel file from open cv, which can be downloaded at: https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv Choose the right version regarding your Python version and then run

或者,您可以使用open cv中的wheel文件进​​行安装,可以从以下网址下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv选择适合您的Python版本的版本,然后运行

pip install "wheel_file_name.whl"

To test if everything is running smooth, run:

要测试一切是否正常运行,请运行:

import cv2 as cv
print(cv.__version__)

It shouln't print any errors if you did everything correctly

如果你正确地做了一切,它不会打印任何错误

#1


1  

I've just run into this issue as well. It turns out that this is not working because opencv-python does not have any prebuilt wheels for Alpine (the distribution you're using as your base docker image).

我也遇到过这个问题。事实证明这是行不通的,因为opencv-python没有任何用于Alpine的预构建轮(您使用的分发作为基础docker镜像)。

The conversation in this issue on the opencv-python package explains why this happens in greater detail. The TL;DR is: if you really need to use Alpine, you can try forcing the installation of the manylinux wheel for opencv-python, but this can break. Your best option if you need to keep Alpine is to build the module from source. Since you are running this on OpenFAAS, I suspect you will want to keep your size low, so building from source may be a good option for you.

这个问题在opencv-python包中的对话解释了为什么会更详细地发生这种情况。 TL; DR是:如果你真的需要使用Alpine,你可以尝试强制安装manylinux轮for opencv-python,但这可能会破坏。如果您需要保留Alpine,最好的选择是从源代码构建模块。由于您在OpenFAAS上运行此功能,我怀疑您希望保持较低的尺寸,因此从源代码构建可能是一个不错的选择。

If you're not attached to Alpine, I would suggest moving to a different base docker image. If you're not sure which image to use as your base, I would recommend python:3.7-slim, since it will come with Python already installed (substitute 3.7 for whichever version you are using, but really. . . 3.7 is nice). With this container, you can simply run pip install opencv-python numpy scipy to have all three of your desired packages installed. The rest of your Dockerfile should work mostly unmodified; you will just need to install/uninstall curl using apt instead of apk.

如果你没有加入Alpine,我建议你转移到另一个基地码头图像。如果您不确定使用哪个图像作为基础,我建议使用python:3.7-slim,因为它已经安装了Python(对于您使用的任何版本替换3.7,但实际上...... 3.7很好) 。使用此容器,您只需运行pip install opencv-python numpy scipy即可安装所有三个所需的软件包。你的Dockerfile的其余部分应该大部分都是未经修改的。你只需要使用apt而不是apk来安装/卸载curl。

#2


-1  

Actually you shouldn't install open-cv using pip, you have to install using the OpenCV package. If you are using Windows:

实际上你不应该使用pip安装open-cv,你必须使用OpenCV软件包安装。如果您使用的是Windows:

http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_setup/py_setup_in_windows/py_setup_in_windows.html

and then head to /opencv/build/python/xxxx folder and copy the folder to \Python\Pythonxxx\Lib\site-packages (xxx beeing your Python version)

然后转到/ opencv / build / python / xxxx文件夹并将文件夹复制到\ Python \ Pythonxxx \ Lib \ site-packages(xxx beeing your Python version)

And if you are on a Linux environment, simply:

如果您在Linux环境中,只需:

 sudo apt-get install python-opencv

Alternatively, you can install using the wheel file from open cv, which can be downloaded at: https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv Choose the right version regarding your Python version and then run

或者,您可以使用open cv中的wheel文件进​​行安装,可以从以下网址下载:https://www.lfd.uci.edu/~gohlke/pythonlibs/#opencv选择适合您的Python版本的版本,然后运行

pip install "wheel_file_name.whl"

To test if everything is running smooth, run:

要测试一切是否正常运行,请运行:

import cv2 as cv
print(cv.__version__)

It shouln't print any errors if you did everything correctly

如果你正确地做了一切,它不会打印任何错误