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 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 --single-branch 4.4.0\
    mv 4.4.0 opencv\
    cd opencv\
    mkdir build\
    cd build\
    cmake \
        -D BUILD_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 \
    make install

ENTRYPOINT python3 -m jupyter notebook --ip= --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.

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

    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.