Mercurial > lasercutter
comparison 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 |
comparison
equal
deleted
inserted
replaced
0:163bf9b2fd13 | 1:6d9bdaf919f7 |
---|---|
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))) | |
7 | |
8 ;this will not work with the current setup; | |
9 ;it's just here as a reference for how to access | |
10 ;cameras. | |
11 | |
12 (def frame-rate (int 1000/30)) | |
13 (def width 640) | |
14 (def height 480) | |
15 | |
16 (defn vision [] | |
17 (doto (OpenCV.) | |
18 (.capture width height) | |
19 (.cascade OpenCV/CASCADE_FRONTALFACE_ALT) | |
20 )) | |
21 | |
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))) | |
27 | |
28 (defn detect-face [vis] | |
29 (.detect vis 1.2 2 OpenCV/HAAR_DO_CANNY_PRUNING 20 20)) | |
30 | |
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)))) | |
38 | |
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)))))) | |
49 | |
50 (defn key-listener [vis timer] | |
51 (proxy [KeyAdapter] [] | |
52 (keyReleased | |
53 [e] | |
54 (.stop timer) | |
55 (.dispose vis)))) | |
56 | |
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)))) | |
69 | |
70 |