Computer Vision Docker Image with TensorFlow and OpenCV

It’s almost inevitable to use Docker images these days especially if you want to have a consistent development environment and configuration. They make life extremely easy by guaranteeing that your application (in this case, Computer Vision application) will always behave the same way as it did when you developed it. How? By using Containerization. If you’re not familiar with the topic then I suggest first doing some research and reading on “Containerization vs Virtualization” and how to use Docker. Then come back to this tutorial to learn how to create a Computer Vision Docker Image that you can use to develop and play around with TensorFlow and OpenCV for Object Detection.

First things first, make sure you have Docker installed on your machine.

Then create a folder called “computervision” and then create a file named “Dockerfile” in that folder. Paste the following code into “Dockerfile”:

FROM tensorflow/tensorflow:1.15.2-py3-jupyter
RUN apt-get update
RUN apt-get upgrade -y
RUN apt-get install git cmake wget unzip -y
ADD /home/
RUN /home/
RUN pip install Cython
RUN pip install contextlib2
RUN pip install pillow
RUN pip install lxml
ADD /home/
RUN /home/

Next, create a file named “” in the same folder and paste the following code in it:

cd /tf/
git clone --single-branch v1.13.0
mv v1.13.0 models
git clone
cd cocoapi/PythonAPI
cp -r pycocotools /tf/models/research/
cd /tf/models/research
wget -O
./bin/protoc object_detection/protos/*.proto --python_out=.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
python object_detection/builders/

The next file we need is called “” which will contain the following:

cd /
git clone --single-branch 3.4.9
mv 3.4.9 cv
cd cv
mkdir build
cd build
cmake ..
make install

We have all the files we need. Now run the following command from inside the “computervision” folder:

docker build --tag computervision .

This will take a long time (depending on your internet speed and computer specs) but eventually you’ll have a Docker image that you can run. It’s worth noting that the image we build contains the following main compnents:

  • TensorFlow 1.5.2
  • TensorFlow Models 1.3.0
  • OpenCV 3.4.9
  • Jupyter Notebook

Let’s run the image we just created, using the following command:

docker run -p 8888:8888 computervision

This will output some text into the Terminal/Command Prompt/PowerShell which will contain a URL similar to the following:

Copy and paste it into your browser. This should take you to the local Jupyter server running in the background using Docker. Now you can start experimenting with TensorFlow and OpenCV.

Not all the folders seen here will be available on your Jupyter server. As a great starting point, let’s use the example from this page to use TensorFlow pretrained models in OpenCV:

If you’re not familiar with Jupyter Notebooks then it might feel a bit strange at first but as soon as you get used to it, life seems impossible without it. So, let’s create a Jupyter Notebook and create the following cells in it.

The first cell is responsible for downloading all the necessary files:

!wget ""
!tar -xvzf "ssd_mobilenet_v2_coco_2018_03_29.tar.gz"
!wget ""

Obviously you need to replace “” with a real URL to a “test.jpg” file you want to test with Object Detection.

The next cell is the sample code from the OpenCV Object Detection example (slightly changed since we can’t use cv.imshow in a Jupyter Notebook:

import numpy as np
import tensorflow as tf
import cv2 as cv

# Read the graph.
with tf.gfile.FastGFile('ssd_mobilenet_v2_coco_2018_03_29/frozen_inference_graph.pb', 'rb') as f:
    graph_def = tf.GraphDef()

with tf.Session() as sess:
    # Restore session
    tf.import_graph_def(graph_def, name='')

    # Read and preprocess an image.    
    img = cv.imread('test.jpg')
    rows = img.shape[0]
    cols = img.shape[1]
    inp = cv.resize(img, (300, 300))
    inp = inp[:, :, [2, 1, 0]]  # BGR2RGB

    # Run the model
    out =[sess.graph.get_tensor_by_name('num_detections:0'),
                   feed_dict={'image_tensor:0': inp.reshape(1, inp.shape[0], inp.shape[1], 3)})

    # Visualize detected bounding boxes.
    num_detections = int(out[0][0])
    for i in range(num_detections):
        classId = int(out[3][0][i])
        score = float(out[1][0][i])
        bbox = [float(v) for v in out[2][0][i]]
        if score > 0.3:
            x = bbox[1] * cols
            y = bbox[0] * rows
            right = bbox[3] * cols
            bottom = bbox[2] * rows
            cv.rectangle(img, (int(x), int(y)), (int(right), int(bottom)), (125, 255, 51), thickness=2)

The last cell is responsible for displaying the result of Object Detection:

import matplotlib.pyplot as plt

Run these cells consecutively and you’ll see the result of object detection (like the following in my case):

Try different things and play around with TensorFlow and OpenCV. The Docker image we created, can of course be modified to use other versions of TensorFlow and OpenCV as well. The greatest thing about it though, is that it will always be ready to go and work on all platforms exactly the same way.

19 Replies to “Computer Vision Docker Image with TensorFlow and OpenCV”

  1. I’m getting the error that cv2 does not exist with all the given code (I had to use chmod -x to run the .sh in the Dockerfile)

    When I run Cell 2 in the new notebook:
    ModuleNotFoundError Traceback (most recent call last)
    1 import numpy as np
    2 import tensorflow as tf
    —-> 3 import cv2 as cv
    5 # Read the graph.

    ModuleNotFoundError: No module named ‘cv2’

    So I tried switching to a pip install of opencv-python, but that returns the same (or a very similar) failure.

    What are the tricks to this? I’ve tried to do several walkthroughs that all seem to fail on opencv, but at least this one got me to Jupyter, so thank you!

  2. Just FYI

    I won’t ask solution, but this looks defect to me in the script. The “cd\” is still causing problem . I have installed whole Docker from beginning and tried your given scripts again. May be you need to check it in windows that if these works or the scripts are compatible to other operating system.

  3. Thanks for helping all this, BTW this page popped up in google search which stated “First things first, make sure you have Docker installed on your machine.” which i already assumed to have, but never thought of different version would have play key role in it.

    Thanks for correcting a silly mistake for getting me out of 3 days drama, still who knows how many odds are yet to come. But at the same time, I accept that I should have been diligent, while putting the statement. no matter whether your solution worked me or not. Learning went crazy on me.

    Appreciate you patience!!

  4. Yea… I feel the same its long trip.

    Here is another error.

    $ docker run tensorflow/tensorflow:1.15.2-py3-jupyter -p 8888:8888
    C:\Program Files\Docker Toolbox\docker.exe: Error response from daemon: OCI runtime create failed: container_linux.go:346: starting container process caused “exec: \”-p\”: executable file not found in $PATH”: unknown.

        1. What kind of Windows do you have? Home? Pro?
          You’re using Docker Toolbox, not Docker Desktop.

          See this link:

          Which mentions this:
          Legacy desktop solution. Docker Toolbox is for older Mac and Windows systems that do not meet the requirements of Docker Desktop for Mac and Docker Desktop for Windows. We recommend updating to the newer applications, if possible.

  5. I am new to docker . Most of docker images available in docker hub are copied from each other, because no one talked about how to make jupyter notebooks run, as it was asking password for all. After lot of struggle found your website where you talked about how to create your own stuff. I got impressed and follow all the steps like religious book. For long time when logs were being printed with errors and finally not able to get the image created. I lost patience thought asking. Is there common source of where all are learning Hope I could refer that too.

    Well back to the point. If somehow we could resolve it. Here is the problem I am facing

    1. debconf: delaying package configuration, since apt-utils is not installed

    2. Step 5/12 : ADD /home/
    —> 5d9bd4bb8fb5
    Step 6/12 : RUN /home/
    —> Running in 7976c9b7574f
    /home/ 1: cd: can’t cd to /
    ‘…ing into ‘3.4.9
    Checking out files: 100% (6509/6509), done.
    mv: cannot stat ‘3.4.9’: No such file or directory
    /home/ 4: cd: can’t cd to cv
    ” does not exist.source directory “/tf/build
    Specify –help for usage, or press the help button on the CMake GUI.
    : not 8: /home/ make
    make: *** No rule to make target ‘install’. Stop.
    The command ‘/bin/sh -c /home/’ returned a non-zero code: 2

    3. Step 5/12 : ADD /home/
    —> 5d9bd4bb8fb5
    Step 6/12 : RUN /home/
    —> Running in 7976c9b7574f
    /home/ 1: cd: can’t cd to /
    ‘…ing into ‘3.4.9
    Checking out files: 100% (6509/6509), done.
    mv: cannot stat ‘3.4.9’: No such file or directory
    /home/ 4: cd: can’t cd to cv
    ” does not exist.source directory “/tf/build
    Specify –help for usage, or press the help button on the CMake GUI.
    : not 8: /home/ make
    make: *** No rule to make target ‘install’. Stop.
    The command ‘/bin/sh -c /home/’ returned a non-zero code: 2

    1. Ignore item 1, that’s just a warning. No biggie.
      2 and 3 are the same thing, more or less.
      What’s your operating system?
      Windows, macOS, Linux?

        1. That is strange, seems docker build can’t access the root folders.
          Here’s a few things for you to try.

          1. Try it on a Linux machine if you have access to

          2. use --no-cache in your docker build command

          3. replace root path is all script with /home
          This means, replace “cd /” with “cd /home” in

          Let me know how this goes.

          1. Nope… it stuck with home now…

            /home/ 1: cd: can’t cd to /home
            ‘…ing into ‘3.4.9

          2. Ok, this is going to be a long road.
            Just run the source image as it is:
            docker run tensorflow/tensorflow:1.15.2-py3-jupyter -p 8888:8888

            Let me know if it runs.

    1. I assume you want to ask “how can I make this work?”
      I’ll start with another assumption, you already know what docker is and how jupyter works and all that.
      So what exactly is the problem you are facing?In other words, what error you get?

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.