2016-04-26 14 views
11

Ich versuche, eine Python-Anwendung zu dockerisieren, die von OpenCV abhängt. Ich habe mehrere verschiedene Möglichkeiten ausprobiert, aber ich bekomme immer ... ImportError: No module named cv2, wenn ich versuche, die Anwendung auszuführen.Installieren Sie OpenCV in einem Docker-Container

Hier ist meine aktuelle Dockerfile.

FROM python:2.7 

MAINTAINER Ewan Valentine <[email protected]> 

RUN mkdir -p /usr/src/app 
WORKDIR /usr/src/app 

# Various Python and C/build deps 
RUN apt-get update && apt-get install -y \ 
    wget \ 
    build-essential \ 
    cmake \ 
    git \ 
    pkg-config \ 
    python-dev \ 
    python-opencv \ 
    libopencv-dev \ 
    libav-tools \ 
    libjpeg-dev \ 
    libpng-dev \ 
    libtiff-dev \ 
    libjasper-dev \ 
    libgtk2.0-dev \ 
    python-numpy \ 
    python-pycurl \ 
    libatlas-base-dev \ 
    gfortran \ 
    webp \ 
    python-opencv 

# Install Open CV - Warning, this takes absolutely forever 
RUN cd ~ && git clone https://github.com/Itseez/opencv.git && \ 
    cd opencv && \ 
    git checkout 3.0.0 && \ 
    cd ~ && git clone https://github.com/Itseez/opencv_contrib.git && \ 
    cd opencv_contrib && \ 
    git checkout 3.0.0 && \ 
    cd ~/opencv && mkdir -p build && cd build && \ 
    cmake -D CMAKE_BUILD_TYPE=RELEASE \ 
    -D CMAKE_INSTALL_PREFIX=/usr/local \ 
    -D INSTALL_C_EXAMPLES=ON \ 
    -D INSTALL_PYTHON_EXAMPLES=ON \ 
    -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ 
    -D BUILD_EXAMPLES=OFF .. && \ 
    make -j4 && \ 
    make install && \ 
    ldconfig 

COPY requirements.txt /usr/src/app/ 
RUN pip install --no-cache-dir -r requirements.txt 

COPY . /usr/src/app 

Und meine requirements.txt Datei

Flask==0.8 
gunicorn==0.14.2 
requests==0.11.1 
bs4==0.0.1 
nltk==3.2.1 
pymysql==0.7.2 
xlsxwriter==0.8.5 
numpy==1.11 
Pillow==3.2.0 
cv2==1.0 
pytesseract==0.1 

Antwort

8

mit einem etwas anderen Aufbau Fest

FROM python:2.7 

MAINTAINER Ewan Valentine <[email protected]> 

RUN mkdir -p /usr/src/app 
WORKDIR /usr/src/app 

# Various Python and C/build deps 
RUN apt-get update && apt-get install -y \ 
    wget \ 
    build-essential \ 
    cmake \ 
    git \ 
    unzip \ 
    pkg-config \ 
    python-dev \ 
    python-opencv \ 
    libopencv-dev \ 
    libav-tools \ 
    libjpeg-dev \ 
    libpng-dev \ 
    libtiff-dev \ 
    libjasper-dev \ 
    libgtk2.0-dev \ 
    python-numpy \ 
    python-pycurl \ 
    libatlas-base-dev \ 
    gfortran \ 
    webp \ 
    python-opencv \ 
    qt5-default \ 
    libvtk6-dev \ 
    zlib1g-dev 

# Install Open CV - Warning, this takes absolutely forever 
RUN mkdir -p ~/opencv cd ~/opencv && \ 
    wget https://github.com/Itseez/opencv/archive/3.0.0.zip && \ 
    unzip 3.0.0.zip && \ 
    rm 3.0.0.zip && \ 
    mv opencv-3.0.0 OpenCV && \ 
    cd OpenCV && \ 
    mkdir build && \ 
    cd build && \ 
    cmake \ 
    -DWITH_QT=ON \ 
    -DWITH_OPENGL=ON \ 
    -DFORCE_VTK=ON \ 
    -DWITH_TBB=ON \ 
    -DWITH_GDAL=ON \ 
    -DWITH_XINE=ON \ 
    -DBUILD_EXAMPLES=ON .. && \ 
    make -j4 && \ 
    make install && \ 
    ldconfig 

COPY requirements.txt /usr/src/app/ 
RUN pip install --no-cache-dir -r requirements.txt 

COPY . /usr/src/app 
5

Vielen Dank für dieses Posting. Ich stieß auf das gleiche Problem und probierte Ihre Lösung aus. Obwohl es so aussah, als würde es OpenCV installieren, hinterließ es mir ein Problem mit widersprüchlichen Versionen der Python six-Bibliothek, so dass ich eine andere Route wählte. Ich denke, ein einfacherer Weg dies zu tun ist, Anaconda in Ihrem Container zu installieren und dann OpenCV hinzuzufügen. Ich verwende Python 2 so meine ganze Dockerfile zu OpenCvv ist nur installiert werden:

FROM continuumio/anaconda 
EXPOSE 5000 

ADD . /code-directory 
WORKDIR code-directory 
RUN conda install opencv 

CMD ["python", "run-code.py"] 

Dies installiert Anaconda vom continuumio/anaconda Dockerfile und dann wird es Anaconda verwenden opencv zu installieren. Es gibt eine separate continuumio Dockerfile für Python 3, wenn Sie das auch brauchen.

+0

Hallo, nicht in der Lage zu verwenden Conda mit Docker beginnen, es sagt Conda nicht gefunden. –

3

Hier ist eine image, die auf Ubuntu 16.04 mit Python2 + Python3 + OpenCV gebaut wird. Sie können es docker pull chennavarri/ubuntu_opencv_python

Hier ist die Dockerfile (sofern in dem gleichen dockerhub Repo oben erwähnt) mit ziehen, die opencv sowohl für python2 und python3 auf Ubuntu 16.04 und setzt auch den entsprechenden raw1394 Link zu installieren. Kopiert von https://github.com/chennavarri/docker-ubuntu-python-opencv

FROM ubuntu:16.04 
MAINTAINER Chenna Varri 

RUN apt-get update 
RUN apt-get install -y build-essential apt-utils 

RUN apt-get install -y cmake git libgtk2.0-dev pkg-config libavcodec-dev \ 
    libavformat-dev libswscale-dev 
RUN apt-get update && apt-get install -y python-dev python-numpy \ 
    python3 python3-pip python3-dev libtbb2 libtbb-dev \ 
    libjpeg-dev libjasper-dev libdc1394-22-dev \ 
    python-opencv libopencv-dev libav-tools python-pycurl \ 
    libatlas-base-dev gfortran webp qt5-default libvtk6-dev zlib1g-dev 

RUN pip3 install numpy 

RUN apt-get install -y python-pip 
RUN pip install --upgrade pip 

RUN cd ~/ &&\ 
    git clone https://github.com/Itseez/opencv.git &&\ 
    git clone https://github.com/Itseez/opencv_contrib.git &&\ 
    cd opencv && mkdir build && cd build && cmake -DWITH_QT=ON -DWITH_OPENGL=ON -DFORCE_VTK=ON -DWITH_TBB=ON -DWITH_GDAL=ON -DWITH_XINE=ON -DBUILD_EXAMPLES=ON .. && \ 
    make -j4 && make install && ldconfig 

# Set the appropriate link 
RUN ln /dev/null /dev/raw1394 

RUN cd ~/opencv 

Einige zusätzliche Anweisungen für die Menschen neu mit Docker Start:

  • Im Verzeichnis, in dem Sie diese Dockerfile setzen, bauen die Docker Bild als docker build -t ubuntu_cv .

  • Sobald das Bild ist gebaut, können Sie überprüfen, indem Sie docker images

    REPOSITORY TAG  IMAGE ID  CREATED    SIZE 
    
    ubuntu_cv latest 6210ddd6346b 24 minutes ago  2.192 GB 
    
  • Sie können einen Docker Container als docker run -t -i ubuntu_cv:latest

1
from ubuntu:12.10 

# Ubuntu sides with libav, I side with ffmpeg. 
run echo "deb http://ppa.launchpad.net/jon-severinsson/ffmpeg/ubuntu quantal main" >> /etc/apt/sources.list 
run apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 1DB8ADC1CFCA9579 


run apt-get update 
run apt-get install -y -q wget curl 
run apt-get install -y -q build-essential 
run apt-get install -y -q cmake 
run apt-get install -y -q python2.7 python2.7-dev 
run wget 'https://pypi.python.org/packages/2.7/s/setuptools/setuptools-0.6c11-py2.7.egg' && /bin/sh setuptools-0.6c11-py2.7.egg && rm -f setuptools-0.6c11-py2.7.egg 
run curl 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py' | python2.7 
run pip install numpy 
run apt-get install -y -q libavformat-dev libavcodec-dev libavfilter-dev libswscale-dev 
run apt-get install -y -q libjpeg-dev libpng-dev libtiff-dev libjasper-dev zlib1g-dev libopenexr-dev libxine-dev libeigen3-dev libtbb-dev 
add build_opencv.sh /build_opencv.sh 
run /bin/sh /build_opencv.sh 
run rm -rf /build_opencv.sh 
+0

sollte build_opencv.sh in Ihrer Antwort enthalten –

Verwandte Themen