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