Lane Detection in Docker

publishing an image

Posted on October 16, 2017

To scale up the lane-detection algorithm to handle more video, it first needs to be published as a Docker image for easy deployment to cloud environments. This post covers bundling a Python microservice by building it on Docker Cloud from its repo on Github.

Drawing on OpenCV and moviepy, this algorithm from Naoki Shibuya draws red markers over detected lanes in dashcam footage as shown below:

Dockerfile

FROM ubuntu:16.04

# Update and install dependencies
RUN apt-get update -y 
RUN apt-get upgrade -y
RUN apt-get dist-upgrade -y
RUN apt-get install -y build-essential cmake pkg-config \
                libjpeg8-dev libtiff5-dev libjasper-dev libpng12-dev \
                libavcodec-dev libavformat-dev libswscale-dev libv4l-dev  \
                libxvidcore-dev libx264-dev imagemagick \
                libgtk-3-dev libatlas-base-dev gfortran 
RUN apt-get install -y python2.7-dev python3.5-dev python-opencv python3-tk \
                python-pip python3-pip 
RUN apt-get autoremove -y
RUN pip install --upgrade pip && pip3 install --upgrade pip

# Install Python3 modules
WORKDIR /opt
COPY requirements.txt .
RUN pip3 install -r requirements.txt
RUN python3 -c "import imageio; imageio.plugins.ffmpeg.download()"

COPY lane_detect.py .
ENTRYPOINT ["/usr/bin/python3", "lane_detect.py"]
CMD ["images/*", "videos/*"]

Docker Build

The Docker build was first done locally with

    docker build -t guydavis/lane-detect .

Docker Run

With some local image data mounted for the Docker container, output was saved into another volume:

    docker run -v $PWD/images:/opt/images -v $PWD/output:/opt/output \  
        guydavis/lane-detect images/mysnapshot.jpg

Image Processed

    docker run -v $PWD/videos:/opt/videos -v $PWD/output:/opt/output \
        guydavis/lane-detect videos/mydashcam.mov

Video Processed

The first time I ran this, the Python Tkinter library complained about lack of a DISPLAY environment. This worked when I ran Python directly on my Ubuntu laptop, but fails when running headless in a Docker container. The trick was to add this line beginning of the Python script to avoid the DISPLAY error:

    import matplotlib
    matplotlib.use('Agg')

Docker Cloud Build

Once I’d tested on my local machine, I pushed to Github and a Docker Cloud build kicked off automatically: Cloud Build

Conclusions

Moving the lane detection algorithm to a Docker image makes for a repeatable and immutable deployment. By encapsulating the various Python, OpenCV, and Imagemagick libraries the entire package is more portable now.

Next Steps

Now that the lane detection algorithm is containerized, I’ll next deploy it to cloud providers to process more video in a cluster of workers.

More in this series…