view 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 source
1 (ns clojureDemo.FaceDetect
2 (:import (javax.swing JFrame JLabel Timer)
3 (java.awt.event ActionListener KeyAdapter)
4 (java.awt Canvas Image Color)
5 (java.awt.image MemoryImageSource)
6 (hypermedia.video OpenCV)))
8 ;this will not work with the current setup;
9 ;it's just here as a reference for how to access
10 ;cameras.
12 (def frame-rate (int 1000/30))
13 (def width 640)
14 (def height 480)
16 (defn vision []
17 (doto (OpenCV.)
18 (.capture width height)
19 (.cascade OpenCV/CASCADE_FRONTALFACE_ALT)
20 ))
22 (defn capture-image [vis]
23 (.read vis)
24 (let [mis (MemoryImageSource. (.width vis) (.height vis)
25 (.pixels vis) 0 (.width vis))]
26 (.createImage (Canvas.) mis)))
28 (defn detect-face [vis]
29 (.detect vis 1.2 2 OpenCV/HAAR_DO_CANNY_PRUNING 20 20))
31 (defn capture-action [vis panel image faces]
32 (proxy [ActionListener] []
33 (actionPerformed
34 [e]
35 (dosync (ref-set image (capture-image vis))
36 (ref-set faces (detect-face vis)))
37 (.repaint panel))))
39 (defn panel [image faces]
40 (proxy [JLabel] []
41 (paint
42 [g]
43 (.drawImage g @image 0 0 nil)
44 (.setColor g Color/red)
45 (doseq [square @faces]
46 (.drawRect g
47 (.x square) (.y square)
48 (.width square) (.height square))))))
50 (defn key-listener [vis timer]
51 (proxy [KeyAdapter] []
52 (keyReleased
53 [e]
54 (.stop timer)
55 (.dispose vis))))
57 (defn main []
58 (let [vis (vision)
59 image (ref (capture-image vis))
60 faces (ref (detect-face vis))
61 panel (panel image faces)
62 timer (Timer. frame-rate (capture-action vis panel image faces))]
63 (.start timer)
64 (doto (JFrame.)
65 (.add panel)
66 (.addKeyListener (key-listener vis timer))
67 (.setSize width height)
68 (.show))))