14

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

jason
  • 3,932
  • 11
  • 52
  • 123
  • Have you tried any solutions that google suggests when you google that error? I get quite a few hits about people on alpine that are getting that error. Namely https://github.com/skvark/opencv-python/issues/75 – Thomas Nov 15 '18 at 21:40
  • in your custom-built image (not the github one), which base image are you using? I've been using python 3.6-slim and 3.5-slim with no issues. Also, what's going wrong with the github image? – C.Nivs Nov 15 '18 at 21:42
  • @C.Nivs Hi Nivs. I tried 3.5-slim and it has installed the libraries but I get any error while running docker. **Error: ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory** – jason Nov 16 '18 at 10:43
  • @C.Nivs I get this error when I run github docker image : – jason Nov 17 '18 at 10:28
  • @C.Nivs E: Unable to locate package libjasper-dev The command '/bin/sh -c apt-get update && apt-get install -y build-essential cmake wget git unzip yasm pkg-config libjpeg-dev libtiff-dev libjasper-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libatlas-base-dev gfortran libtbb2 libtbb-dev libpq-dev && apt-get -y clean all && rm -rf /var/lib/apt/lists/*' returned a non-zero code: 100 – jason Nov 17 '18 at 10:28

2 Answers2

18

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:

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

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-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-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-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"]
x-yuri
  • 16,722
  • 15
  • 114
  • 161
larsks
  • 277,717
  • 41
  • 399
  • 399
  • 1
    I've shown every single command I ran here in this answer. What code are you asking for? – larsks Nov 18 '18 at 14:35
  • Sorry larks but I'm very new to docker and I'm trying different variations in dockerfile to get this module to work but it always crashes out. The current code in my docker file looks like this : **FROM python:3.5 COPY . /app WORKDIR /app RUN pip3 install -r requirements.txt ENTRYPOINT ["python3"] CMD ["app.py"]** – jason Nov 18 '18 at 14:40
  • DO I need install opencv as a command on the same image or something?? – jason Nov 18 '18 at 14:41
  • SOrry I'm totally noob here! – jason Nov 18 '18 at 14:41
  • OK!!! It is running now and it displays **Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)** BUT when I open this link it shows **This page isn’t working** – jason Nov 18 '18 at 14:50
  • PLease help me!! – jason Nov 18 '18 at 14:50
  • 1
    I think you probably want to open a new question on that issue; the comments here aren't really the place for extended off-topic discussion. You would want to include whatever code in `app.py` is responsible for starting your service. – larsks Nov 18 '18 at 14:56
  • Not `libglib2.0`, `libglib2.0-0`. `libglib2.0` contains a dot, so `apt` considers it a regexp, and you end up installing more packages then you need. – x-yuri Jul 04 '20 at 11:40
4

I had the same issue. I was using python-slim. It occurs due to run time dependencies. Add the following code snippet in your DockerFile to install run time dependencies.

Install OpenCV's runtime dependencies

RUN apt-get update RUN apt-get -y install libglib2.0-0 RUN apt-get -y install libsm6 \ libxrender-dev \ libxext6

Brijesh Gupta
  • 487
  • 4
  • 8