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

匿名 (未验证) 提交于 2019-12-03 01:23:02

问题:

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

回答1:

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:

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


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!