Skip to content

Instantly share code, notes, and snippets.

@nblumoe
Last active July 27, 2018 00:56
Show Gist options
  • Save nblumoe/5468625 to your computer and use it in GitHub Desktop.
Save nblumoe/5468625 to your computer and use it in GitHub Desktop.
(ns create-draw.opencv
(:import
org.opencv.core.Core
org.opencv.core.Mat
org.opencv.core.MatOfRect
org.opencv.core.Point
org.opencv.core.Rect
org.opencv.core.Scalar
org.opencv.highgui.Highgui
org.opencv.objdetect.CascadeClassifier))
(defn create-classifier
[]
(CascadeClassifier. (.getPath (clojure.java.io/resource "lbpcascade_frontalface.xml" ))))
(defn load-image
[]
(Highgui/imread (.getPath (clojure.java.io/resource "lena.png"))))
(def face-detections (atom []))
(defn detect-faces!
[classifier image]
(.detectMultiScale classifier image @face-detections))
(defn draw-bounding-boxes!
[image]
(doall (map (fn [rect]
(Core/rectangle image (Point. (.x rect) (.y rect))
(Point. (+ (.x rect) (.width rect))
(+ (.y rect) (.height rect)))
(Scalar. 0 255 0)))
(.toArray @face-detections)))
(Highgui/imwrite "faceDetections.png" image))
(defn process-and-save-image!
[]
(let [image (load-image)]
(detect-faces! (create-classifier) image)
(draw-bounding-boxes! image)))
(defn main
[]
(clojure.lang.RT/loadLibrary Core/NATIVE_LIBRARY_NAME)
(reset! face-detections (MatOfRect.))
(process-and-save-image!))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment