diff src/clojureDemo/FaceDetect.clj @ 1:6d9bdaf919f7

added clojureDemo source
author Robert McIntyre <rlm@mit.edu>
date Fri, 20 Aug 2010 00:32:44 -0400
parents
children
line wrap: on
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/clojureDemo/FaceDetect.clj	Fri Aug 20 00:32:44 2010 -0400
     1.3 @@ -0,0 +1,70 @@
     1.4 +(ns clojureDemo.FaceDetect
     1.5 +  (:import (javax.swing JFrame JLabel Timer)
     1.6 +	   (java.awt.event ActionListener KeyAdapter)
     1.7 +	   (java.awt Canvas Image Color)
     1.8 +	   (java.awt.image MemoryImageSource)
     1.9 +	   (hypermedia.video OpenCV)))
    1.10 +
    1.11 +;this will not work with the current setup;
    1.12 +;it's just here as a reference for how to access
    1.13 +;cameras.
    1.14 +
    1.15 +(def frame-rate (int 1000/30))
    1.16 +(def width 640)
    1.17 +(def height 480)
    1.18 +
    1.19 +(defn vision []
    1.20 +  (doto (OpenCV.)
    1.21 +    (.capture width height)
    1.22 +    (.cascade OpenCV/CASCADE_FRONTALFACE_ALT)
    1.23 +))
    1.24 +
    1.25 +(defn capture-image [vis]
    1.26 +  (.read vis)
    1.27 +  (let [mis (MemoryImageSource. (.width vis) (.height vis)
    1.28 +				(.pixels vis) 0 (.width vis))]
    1.29 +    (.createImage (Canvas.) mis)))
    1.30 +
    1.31 +(defn detect-face [vis]
    1.32 +  (.detect vis 1.2 2 OpenCV/HAAR_DO_CANNY_PRUNING 20 20))
    1.33 +
    1.34 +(defn capture-action [vis panel image faces]
    1.35 +  (proxy [ActionListener] []
    1.36 +    (actionPerformed
    1.37 +     [e]
    1.38 +     (dosync (ref-set image (capture-image vis))
    1.39 +	     (ref-set faces (detect-face vis)))
    1.40 +     (.repaint panel))))
    1.41 +
    1.42 +(defn panel [image faces]
    1.43 +  (proxy [JLabel] [] 
    1.44 +    (paint
    1.45 +     [g]
    1.46 +     (.drawImage g @image 0 0 nil)
    1.47 +     (.setColor g Color/red)
    1.48 +     (doseq [square @faces]
    1.49 +       (.drawRect g
    1.50 +		  (.x square) (.y square)
    1.51 +		  (.width square) (.height square))))))
    1.52 +
    1.53 +(defn key-listener [vis timer]
    1.54 +  (proxy [KeyAdapter] [] 
    1.55 +    (keyReleased 
    1.56 +     [e]
    1.57 +     (.stop timer)
    1.58 +     (.dispose vis))))
    1.59 +
    1.60 +(defn main []
    1.61 +  (let [vis   (vision)
    1.62 +	image (ref (capture-image vis))
    1.63 +	faces (ref (detect-face vis))
    1.64 +	panel (panel image faces)
    1.65 +	timer (Timer. frame-rate (capture-action vis panel image faces))]
    1.66 +    (.start timer)
    1.67 +    (doto (JFrame.)
    1.68 +      (.add panel)
    1.69 +      (.addKeyListener (key-listener vis timer))
    1.70 +      (.setSize width height)
    1.71 +      (.show))))
    1.72 +
    1.73 +