Computer Vision Docker Image with TensorFlow and OpenCV, From Scratch

After publishing this post some time ago which was a tutorial on how to create a Computer Vision Docker image using OpenCV and TensorFlow, I got many questions from people about the issues they’re facing when they try to use it. If you think something of a similar nature happened to you, then this post is meant for you.



First, the image from that tutorial was built on top of an official TensorFlow Docker image, so all the issues in that image are, unfortunately, part of my image too. There’s no way around it. The other thing is, the order in which things are installed matters and trying to go back and figuring everything out (the issues) is just gonna take a long time.

So, without further ado, here is a complete Dockerfile, which will yield a Computer Vision Docker image you can use for studying and experimenting and so on. Hopefully it will also help you avoid all the issues from the previous post:

FROM ubuntu:20.04

RUN apt-get update
RUN apt-get upgrade -y

# Workaround for an installation issue with Ubuntu 20.04
RUN DEBIAN_FRONTEND="noninteractive" apt-get install tzdata -y

RUN apt-get install wget curl build-essential cmake gcc g++ git python3 python3-pip -y

RUN pip3 install --upgrade pip
RUN pip3 install tensorflow
RUN pip3 install tensorflow_datasets
RUN pip3 install sklearn
RUN pip3 install pandas
RUN pip3 install jupyterlab
RUN pip3 install matplotlib

# Install OpenCV
RUN apt-get -y install build-essential checkinstall cmake pkg-config yasm
RUN apt-get -y install git gfortran
RUN apt-get -y install libjpeg8-dev libpng-dev

RUN apt-get -y install software-properties-common
RUN add-apt-repository "deb http://security.ubuntu.com/ubuntu xenial-security main"
RUN apt-get -y update

RUN apt-get -y install libjasper1
RUN apt-get -y install libtiff-dev

RUN apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
RUN apt-get -y install libxine2-dev libv4l-dev
RUN cd /usr/include/linux && ln -s -f ../libv4l1-videodev.h videodev.h

RUN apt-get -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
RUN apt-get -y install libgtk2.0-dev libtbb-dev qt5-default
RUN apt-get -y install libatlas-base-dev
RUN apt-get -y install libfaac-dev libmp3lame-dev libtheora-dev
RUN apt-get -y install libvorbis-dev libxvidcore-dev
RUN apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
RUN apt-get -y install libavresample-dev
RUN apt-get -y install x264 v4l-utils

RUN apt-get install libjpeg-dev libopenexr-dev libwebp-dev -y

RUN apt-get install ffmpeg -y

RUN \	
    cd / \
    &&\	
    git clone https://github.com/opencv/opencv.git --single-branch 4.4.0\
    &&\	
    mv 4.4.0 opencv\
    &&\	
    cd opencv\
    &&\	
    mkdir build\
    &&\	
    cd build\
    &&\	
    cmake \
        -D CMAKE_BUILD_TYPE=RELEASE \
        -D BUILD_TESTS=OFF \
        -D BUILD_PERF_TESTS=OFF \
        -D BUILD_opencv_python_tests=OFF \
        -D WITH_FFMPEG=ON \
        -D WITH_TBB=ON \
        -D WITH_V4L=ON \
        -D WITH_QT=ON \
        -D WITH_OPENGL=ON \
        -D WITH_GSTREAMER=ON \
        ..\
    &&\
    make\
    &&\	
    make install

ENTRYPOINT python3 -m jupyter notebook --ip=0.0.0.0 --port=5000 --allow-root

Note that this one is based on the latest versions of everything (TensorFlow, OpenCV etc.) so make sure your notebooks are compatible with it.

Also note that you have to have publish port 5000 for the image to work.

Good luck and post your questions below if you have any.



8 Replies to “Computer Vision Docker Image with TensorFlow and OpenCV, From Scratch”

  1. Same Error that on this version: https://amin-ahmadi.com/2020/03/07/computer-vision-docker-image-with-tensorflow-and-opencv/

    Error in: import tensorflow as tf

    Error Message: “The kernel appears to have died. It will restart automatically.”

    This Tutorial do not work… =/

    Command to Build: docker build –tag computervision .
    Commando To Start: docker run -p 5000:5000 computervision

    Dockerfile:
    FROM ubuntu:20.04

    RUN apt-get update
    RUN apt-get upgrade -y

    # Workaround for an installation issue with Ubuntu 20.04
    RUN DEBIAN_FRONTEND=”noninteractive” apt-get install tzdata -y

    RUN apt-get install wget curl build-essential cmake gcc g++ git python3 python3-pip -y

    RUN pip3 install –upgrade pip
    RUN pip3 install tensorflow
    RUN pip3 install tensorflow_datasets
    RUN pip3 install sklearn
    RUN pip3 install pandas
    RUN pip3 install jupyterlab
    RUN pip3 install matplotlib

    # Install OpenCV
    RUN apt-get -y install build-essential checkinstall cmake pkg-config yasm
    RUN apt-get -y install git gfortran
    RUN apt-get -y install libjpeg8-dev libpng-dev

    RUN apt-get -y install software-properties-common
    RUN add-apt-repository “deb http://security.ubuntu.com/ubuntu xenial-security main”
    RUN apt-get -y update

    RUN apt-get -y install libjasper1
    RUN apt-get -y install libtiff-dev

    RUN apt-get -y install libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev
    RUN apt-get -y install libxine2-dev libv4l-dev
    RUN cd /usr/include/linux && ln -s -f ../libv4l1-videodev.h videodev.h

    RUN apt-get -y install libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev
    RUN apt-get -y install libgtk2.0-dev libtbb-dev qt5-default
    RUN apt-get -y install libatlas-base-dev
    RUN apt-get -y install libfaac-dev libmp3lame-dev libtheora-dev
    RUN apt-get -y install libvorbis-dev libxvidcore-dev
    RUN apt-get -y install libopencore-amrnb-dev libopencore-amrwb-dev
    RUN apt-get -y install libavresample-dev
    RUN apt-get -y install x264 v4l-utils

    RUN apt-get install libjpeg-dev libopenexr-dev libwebp-dev -y

    RUN apt-get install ffmpeg -y

    RUN \
    cd / \
    &&\
    git clone https://github.com/opencv/opencv.git –single-branch 4.4.0\
    &&\
    mv 4.4.0 opencv\
    &&\
    cd opencv\
    &&\
    mkdir build\
    &&\
    cd build\
    &&\
    cmake \
    -D CMAKE_BUILD_TYPE=RELEASE \
    -D BUILD_TESTS=OFF \
    -D BUILD_PERF_TESTS=OFF \
    -D BUILD_opencv_python_tests=OFF \
    -D WITH_FFMPEG=ON \
    -D WITH_TBB=ON \
    -D WITH_V4L=ON \
    -D WITH_QT=ON \
    -D WITH_OPENGL=ON \
    -D WITH_GSTREAMER=ON \
    ..\
    &&\
    make\
    &&\
    make install

    ENTRYPOINT python3 -m jupyter notebook –ip=0.0.0.0 –port=5000 –allow-root

  2. Hi sir. Thank you for this write-up tho i have yet to try it myself because Im new in the field 🙂 Sir, do u know if there’s any way to do this simpler, i.e copy requirement.txt file? I have already a real-time detection system running. If copying requirements.txt can be done, it’d be simpler i think.. well maybe except in getting it running real-time sinnce it’ll be on docker(container)

    Thank you in advance. I hope you get what Im trying to convey 🙂

    1. What’s is your goal?
      The Dockerfile here is quite simple.
      You can use requirements.txt as well if you like but I don’t see how it makes this any “simpler”!?

      1. Hi. Is it possible to create this image (with latest version of TF and OpenCV as u said) and then copy a requirement.txt file from my existing system(dependencies to run the app etc) into the Dockerfile created for this image? I am currently running virtually on jupyter notebook a real-time occupancy detection using TF 1.10 and OpenCV and i would like containerize it with Docker for easiness developing the app with other computers..

        Thank you in advance 🙂

      2. Hi. I have tried your tutorial but with a little tweak..
        Here’s what I did:
        I deleted this lines
        RUN pip3 install –upgrade pip
        RUN pip3 install tensorflow
        RUN pip3 install tensorflow_datasets
        RUN pip3 install sklearn
        RUN pip3 install pandas
        RUN pip3 install jupyterlab
        RUN pip3 install matplotlib

        And replaced with a requirements.txt file generated from my real time object detection system(TF 1.10.0).

        The output: I could get Jupyter Notebook running but with TF latest version. This shows that my requirements.txt file was ignored. Could you tell me why, please?

        Thank you 🙂

  3. Thanks this works great for me. I am running this on Docker Windows 10 Home edition using WSL.

    1. Glad to hear it was helpful. At home I’m using it in WSL as well with Win 10 Home. At work I use it with Pro and Enterprise too. No issues so far.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.