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 opencv.sh /home/opencv.sh
RUN /home/opencv.sh
RUN pip install Cython
RUN pip install contextlib2
RUN pip install pillow
RUN pip install lxml
ADD tfmodels.sh /home/tfmodels.sh
RUN /home/tfmodels.sh

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

cd /tf/
git clone https://github.com/tensorflow/models.git --single-branch v1.13.0
mv v1.13.0 models
git clone https://github.com/cocodataset/cocoapi.git
cd cocoapi/PythonAPI
make
cp -r pycocotools /tf/models/research/
cd /tf/models/research
wget -O protobuf.zip https://github.com/google/protobuf/releases/download/v3.0.0/protoc-3.0.0-linux-x86_64.zip
unzip protobuf.zip
./bin/protoc object_detection/protos/*.proto --python_out=.
export PYTHONPATH=$PYTHONPATH:`pwd`:`pwd`/slim
python object_detection/builders/model_builder_test.py

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

cd /
git clone https://github.com/opencv/opencv.git --single-branch 3.4.9
mv 3.4.9 cv
cd cv
mkdir build
cd build
cmake ..
make
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:

http://127.0.0.1:8888/?token=487a8ed5ac0cb13b0a57080917cc679db9242180a5181a52

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:

https://github.com/opencv/opencv/wiki/TensorFlow-Object-Detection-API

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 "http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v2_coco_2018_03_29.tar.gz"
!tar -xvzf "ssd_mobilenet_v2_coco_2018_03_29.tar.gz"
!wget "https://somewebsite.com/test.jpg"

Obviously you need to replace “https://somewebsite.com/test.jpg” 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()
    graph_def.ParseFromString(f.read())

with tf.Session() as sess:
    # Restore session
    sess.graph.as_default()
    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.run([sess.graph.get_tensor_by_name('num_detections:0'),
                    sess.graph.get_tensor_by_name('detection_scores:0'),
                    sess.graph.get_tensor_by_name('detection_boxes:0'),
                    sess.graph.get_tensor_by_name('detection_classes: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
plt.imshow(img)

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.



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.