Image Scripting with Clojure, on Raspberry Pi

September 3 2019

There has been scripting chances with Clojure for some time, mostly with boot.clj or lein-exec , but it always fell the startup time of the JVM was getting in the way of actually enjoying and wanting to do scripting in Clojure.

Now more recently Closh, and Inlein have been proposing some interesting things that can be put into action on little raspberry pies.

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 the jar file and execute 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 direct clojure conversion.

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

$ ls * | (clojure.string/upper-case)
"README.MD\nAVERAGE.CLJ\nAVERAGE.JPG\nCARTOON.CLJ\nCAT.JPG\nHELLO.CLJ\nKERNEL.CLJ\nKERNEL.PNG\nOK.CLJ\nORIGAMI.CLJ\nRING.CLJ\nSIMPLE.CLJ\nTEMP.CLJ\nTEMP2.CLJ\nWEBCAM.CLJ\n"
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\")"

Output can then simply be manipulated, and transformed using pretty standard clojure.

But using a different shell all together may nice on a local machine, but on a remote server might be harder to put in place. So, here’s come 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, nor Lein download strictly 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 similar is some ways to what Gradle is doing these days with the Gradle daemon.

Some inlein examples can be found on the 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 at least to define the version of clojure you wll be using and cannot be ommited.
  • contains 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 this scripting techniques to run some arbitrary origami code.

Scripting Origami

The snippet below builds on the previous exampe, 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 realtime 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, may be lacking performance of more native languages, but …

time ./fib.clj 10000


real	0m3.802s
user	0m3.533s
sys	0m0.368s

runs pretty fast, so it’s looking promising to do more real-time computation in Clojure on the raspberry pi now.

Built with Hugo

© Nicolas Modrzyk 2019 - hellonico @ gmail dot com