Take a closer look at OpenCV scripting on Raspberry Pis with Clojure

There have been recent scripting changes with Clojure, mostly with boot.clj and lein-exec. It always felt as if the startup time of the JVM was getting in the way of actually enjoying and wanting to conduct scripting in Clojure.

Now, more recently, Closh and Inlein have proposed interesting features that can be put into action on Raspberry Pis.

You may also like: OpenCV + Apache MiniFi for IoT

Scripting in Clojure

As far as scripting goes, I like where Closh is heading. Closh is like a Clojure Shell, where both Clojure and Shell commands can be used for easy files manipulation.

For example, once you have downloaded and executed it, you can write Shell code like this:

echo hi | (clojure.string/upper-case)





... where echo hi is standard bash coding, and the pipe | redirect to the direct Clojure conversion.

You can also interact and pipe the output of Shell commands direclty into collections using |> instead of | .

$ ls * | (clojure.string/upper-case) "README.MD

AVERAGE.CLJ

AVERAGE.JPG

CARTOON.CLJ

CAT.JPG

HELLO.CLJ

KERNEL.CLJ

KERNEL.PNG

OK.CLJ

ORIGAMI.CLJ

RING.CLJ

SIMPLE.CLJ

TEMP.CLJ

TEMP2.CLJ

WEBCAM.CLJ

"

Vs.

ls * |> (clojure.string/upper-case) "(\"README.MD\" \"AVERAGE.CLJ\" \"AVERAGE.JPG\" \"CARTOON.CLJ\" \"CAT.JPG\" \"HELLO.CLJ\" \"KERNEL.CLJ\" \"KERNEL.PNG\" \"OK.CLJ\" \"ORIGAMI.CLJ\" \"RING.CLJ\" \"SIMPLE.CLJ\" \"TEMP.CLJ\" \"TEMP2.CLJ\" \"WEBCAM.CLJ\")"





The output can then simply be manipulated and transformed using pretty standard Clojure.

But using a different Shell altogether may seem nice on a local machine, but on a remote server, it might be harder to put in place. So, here comes inlein .

Inlein: Easy Scripting in Clojure

Inlein, simply said, runs a daemon dedicated to executing some Clojure code. Inlein also handles dependencies so you can use external libraries easily a-la-groovy-grape.

The main inlein script can be retrieved from the download page, and if you can come from a new device install, this is actually the only thing you need to get started with Clojure scripting. So, no Clojure download, or nor Lein download is required. Although, of course, those will be handy for other things.

After usual permission tweaking, the inlein executable can be used simply via inlein on the terminal.

inlein is a tool to handle Clojure scripts with dependencies Usage: inlein [--run] file [args...] (to run a clojure script) or inlein --task [args...] (to run an inlein task) Several tasks are available: --deps Retrieves the dependencies for a script --help Prints this banner, or extended information about a task. --ping Pings the inlein server, if it runs. --restart-daemon Restarts the inlein daemon --run Runs a clojure script with dependencies --sh-cmd Prints the shell command a clojure script with dependencies will use --shutdown-daemon Shuts off the Inlein daemon --start-daemon Starts the inlein daemon --upgrade Upgrades to the specified inlein version, or latest --version Prints the currently running Inlein version.





This is similar in some ways to what Gradle is doing these days with the Gradle daemon.

Some inlein examples can be found on this GitHub repository, but let's review some simple things by ourselves.

The very first sample would be to obviously say hello to the world.

The Clojure-on-inlein snippet below:

Starts with a shebang, referring to inlein

Add a quoted map including required dependencies. This is required to define the version of Clojure you are using and cannot be omitted.

Contains the following Clojure code:

#!/usr/bin/env inlein '{:dependencies [[org.clojure/clojure "1.8.0"]]} (println "hello world!")





Let's update the permissions of this script before running it:

chmod +x hello.clj

And execute the script with:

$ ./hello.clj hello world!





Script parameters can be directly retrieved accessing command-line-args , as shown in the Celcius-to-Fahrenheit code snippet below:

#!/usr/bin/env inlein '{:dependencies [[org.clojure/clojure "1.10.1"]]} (defn to-fahrenheit [k] (+ (* k 1.8) 32)) (-> *command-line-args* first (#(Integer/parseInt %)) to-fahrenheit println)





But in our usual case, we want to use these scripting techniques to run some arbitrary origami code, a library and a wrapper for OpenCV on the JVM.

Scripting Origami

The snippet below builds on the previous example, where we refer to the main origami dependency, uploaded on clojars, and direct origami code to show the included precompiled OpenCV version.

#!/usr/bin/env inlein '{:dependencies [[origami/origami "4.1.1"]]} (require '[opencv4.core :refer [VERSION]]) (println "Using OpenCV Version: " VERSION "...")





This works really smoothly, including on the Raspberry Pi: (carefully tested only on 3 and 4 but should work on other ones as well...)

$ uname -a Linux raspberrypi 4.19.58-v7l+ #1245 SMP Fri Jul 12 17:31:45 BST 2019 armv7l GNU/Linux $ ./ok.clj Using OpenCV Version: 4.1.1 ...





Of course, you can also run a webcam and do some real-time processing, directly with the same kind of scripting. Here is a longer snippet, including juxtaposing the original webcam stream and the manipulated buffered:

#!/usr/bin/env inlein '{:dependencies [[org.clojure/clojure "1.8.0"][origami/origami "4.1.1-3"]]} (ns opencv4.webcam (:require [opencv4.core :refer :all] [opencv4.utils :as u])) (u/simple-cam-window (fn [buffer] (u/resize-by buffer 0.3) (let [ output (new-mat) bottom (-> buffer clone (flip! -1)) ] (-> buffer (cvt-color! COLOR_RGB2GRAY) (cvt-color! COLOR_GRAY2RGB)) (put-text buffer (str (java.util.Date.)) (new-point 10 50) FONT_HERSHEY_PLAIN 1 (new-scalar 255 255 0) 1) (vconcat [buffer bottom] output) output)))





Note that running commands via SSH, you may need to set the default display with:

export DISPLAY=:0

Or loading a picture from a remote URL and cartooning its content can be done with the usual origami example:

#!/usr/bin/env inlein '{:dependencies [[org.clojure/clojure "1.8.0"][origami/origami "4.1.1-3"]]} (require '[opencv4.core :refer :all]) (require '[opencv4.utils :as u]) (-> (first *command-line-args*) (u/mat-from-url) (u/resize-by 0.3) (cvt-color! COLOR_BGR2GRAY) (gaussian-blur! (new-size 1 1) 1 1) (canny! 100.0 220.0 3 true) (bitwise-not!) (imwrite "cartoon.png"))





Then, the script can be executed on arbitrary pictures:

./cartoon.clj https://raw.githubusercontent.com/hellonico/origami/master/doc/cat_in_bowl.jpeg

More examples can be found on GitHub ... so enjoy.

Performance

In the samples, there is a very naive implementation of a fibonacci computation script. We'll put it here for reference:

#!/usr/bin/env inlein '{:dependencies [[org.clojure/clojure "1.8.0"]]} (def fib-seq-seq ((fn fib [a b] (lazy-seq (cons a (fib b (+' a b))))) 0 1)) (-> *command-line-args* first (#(Integer/parseInt %)) (take fib-seq-seq) (last) (println))





While obviously, it may be lacking the performance of more native languages, but the following code:

time ./fib.clj 10000 real 0m3.802s user 0m3.533s sys 0m0.368s





... runs rather pretty fast. So, it now looks rather promising to conduct real-time computation in Clojure on the Raspberry Pi.

Hope you enjoyed this short demonstration! Please leave your thoughts in the comments section.

Further Reading

OpenCV + Apache MiniFi for IoT

How to Deploy OpenCV on Raspberry Pi and Enable Machine Vision