rlm@1: (ns clojureDemo.FaceDetect rlm@1: (:import (javax.swing JFrame JLabel Timer) rlm@1: (java.awt.event ActionListener KeyAdapter) rlm@1: (java.awt Canvas Image Color) rlm@1: (java.awt.image MemoryImageSource) rlm@1: (hypermedia.video OpenCV))) rlm@1: rlm@1: ;this will not work with the current setup; rlm@1: ;it's just here as a reference for how to access rlm@1: ;cameras. rlm@1: rlm@1: (def frame-rate (int 1000/30)) rlm@1: (def width 640) rlm@1: (def height 480) rlm@1: rlm@1: (defn vision [] rlm@1: (doto (OpenCV.) rlm@1: (.capture width height) rlm@1: (.cascade OpenCV/CASCADE_FRONTALFACE_ALT) rlm@1: )) rlm@1: rlm@1: (defn capture-image [vis] rlm@1: (.read vis) rlm@1: (let [mis (MemoryImageSource. (.width vis) (.height vis) rlm@1: (.pixels vis) 0 (.width vis))] rlm@1: (.createImage (Canvas.) mis))) rlm@1: rlm@1: (defn detect-face [vis] rlm@1: (.detect vis 1.2 2 OpenCV/HAAR_DO_CANNY_PRUNING 20 20)) rlm@1: rlm@1: (defn capture-action [vis panel image faces] rlm@1: (proxy [ActionListener] [] rlm@1: (actionPerformed rlm@1: [e] rlm@1: (dosync (ref-set image (capture-image vis)) rlm@1: (ref-set faces (detect-face vis))) rlm@1: (.repaint panel)))) rlm@1: rlm@1: (defn panel [image faces] rlm@1: (proxy [JLabel] [] rlm@1: (paint rlm@1: [g] rlm@1: (.drawImage g @image 0 0 nil) rlm@1: (.setColor g Color/red) rlm@1: (doseq [square @faces] rlm@1: (.drawRect g rlm@1: (.x square) (.y square) rlm@1: (.width square) (.height square)))))) rlm@1: rlm@1: (defn key-listener [vis timer] rlm@1: (proxy [KeyAdapter] [] rlm@1: (keyReleased rlm@1: [e] rlm@1: (.stop timer) rlm@1: (.dispose vis)))) rlm@1: rlm@1: (defn main [] rlm@1: (let [vis (vision) rlm@1: image (ref (capture-image vis)) rlm@1: faces (ref (detect-face vis)) rlm@1: panel (panel image faces) rlm@1: timer (Timer. frame-rate (capture-action vis panel image faces))] rlm@1: (.start timer) rlm@1: (doto (JFrame.) rlm@1: (.add panel) rlm@1: (.addKeyListener (key-listener vis timer)) rlm@1: (.setSize width height) rlm@1: (.show)))) rlm@1: rlm@1: