maratishe.github.io Packing "SimpleCV + 3WayScripts" on Docker Author: maratishe@gmail.com -- created 160126

In a resent post On the 3-Way Scripting Programming Technique I explained the basic 3-way scripting technique as well as how it can be used to implement flexible APIs.  This post shows one such API in which 3-way script is used to simplify access and, specifically, allow for easy automation, of another software.  Since the other software is implemented as a Docker container, the respective 3WayScript API is inserted into the same container and used to access the main/core software on the inside.


The SimpleCV Story



I needed to use a vision library in my research. I did a quick search and discovered that the consensus was on OpenCV.  Now, I consider myself slightly above average in terms of being able to handle software installation on Linux.  After several hours of trying, I gave up on OpenCV.  Tried a couple of separate OSes, both virtual and physical, in the meantime... all to no avail.

Another pass at the search brought me to SimpleCV.  This tool promised to have solved that very problem -- hence its name.  However, a deeper look revealed that the only interface to the tool is a Python interactive shell.  Being honest, I did not know what that was before I tried SimpleCV for the first time.  Then I got it.  You start the Docker container -- it is, in fact, the preferred mode of installation as advertised by the tool itself -- and it returns the Python shell.  Then, you interact with SimpleCV using the shell.

Now, obviously, this is bad when you need to build automation on top of the tool.  Specifically, you might want to build standard routines/procedures which you would like to call in a simple fashion -- preferably over a web API.

This is where a 3WayScript becomes helpful.


The SimpleCV + 3WayScript Package



The Docker form of 3WayScript API was explained in the post On the 3-Way Scripting Programming Technique.  The same basic packaging trick is used here as well.  The difference is that SimpleCV is based on OpenCV (it is a simplified Python interface to it) and its container is a specific version of Ubuntu.  It has Python but no PHP.

So, my new Dockerfile has to first add PHP and then add the 3WayScript API to it.  The simplecvapi project on Github has all the necessary files, including the Docker file.


Let's see what is in the files. Dockerfile is obviously the main/build file. api.php is the 3WayScript API to SimpleCV, built in full accordance with the 3-way scripting technique.  It is only the stab, but you can fill it out with your own APIs.  It requires requireme.php for all its dependencies - this is done automatically by the 3-way script skeleton code, so you do not have to do anything about it.  Finally, test.py is the test code for SimpleCV and photo.jpg is the image imported for analysis by the test code.










Building and running Docker container



Dockerfile is as follows:


FROM sightmachine/simplecv
RUN \ apt-get update -y && \
   apt-get install -y python-software-properties && \
   add-apt-repository -y ppa:ondrej/php5-5.6 && \
   apt-get update -y && \ apt-get dist-upgrade -y && \
   apt-get install -y php5 php5-cli && \
   cd / && \
   rm -Rf test && \
   mkdir test && \
   cd test COPY . /test
WORKDIR /test
EXPOSE 8001
#CMD [ "php", "php example.php server 0.0.0.0:8001"]


As you can see, I first update the OS, then install PHP 5.6 and finally copy the rest of the files from simplecvapi to the newly created /test working directory.  Port 8001 is exposed so that the web API inside the container could be accessed.

Now, you create the container by running docker build -t mycvapi . in the directory that has the above Dockerfile and run it using docker run -it -p 8001:8001 --rm mycvapi python test.py.  The python test.py is the easiest way to test whether your SimpleCV is working inside the container. 

It is then very easy to extend the same python test.py into the web API form by creating a new function inside api.php and calling this newly created API using one of the three ways allowed by the 3-way scripting technique.

Enjoy.



























text



Written with my own local WYSIWYG editor.