Download Jar. Uses 24524K of libraries. Click here for Pure Java version (6199L/40K).
1 | !7 |
2 | |
3 | static double confidenceThreshold = 10; |
4 | sbool setViewSize = false; |
5 | static double timeout = 10; |
6 | static int skipFirst = 9; |
7 | static int nn = 0, totalSamples; |
8 | |
9 | lib 1008985 // webcam |
10 | lib 1008987 // bridj |
11 | lib 1004016 // slf4j api |
12 | |
13 | // OpenIMAJ |
14 | lib 1004905 lib 1004906 lib 1004908 lib 1004909 lib 1004910 |
15 | lib 1004911 lib 1004912 lib 1004913 lib 1004914 lib 1004915 |
16 | lib 1004919 lib 1004920 |
17 | lib 1009014 // processing.face |
18 | lib 1009015 // object detection |
19 | lib 1009016 // xpp (xmlpull implementation) |
20 | |
21 | import com.github.sarxos.webcam.*; |
22 | |
23 | import org.openimaj.image.ImageUtilities; |
24 | import org.openimaj.image.processing.face.detection.DetectedFace; |
25 | import org.openimaj.image.processing.face.detection.HaarCascadeDetector; |
26 | |
27 | p { |
28 | final new FaceDetector d; |
29 | d.onDetect.add(new Runnable { |
30 | new LinkedList<Pair<Long, Int>> samples; |
31 | |
32 | public void run() { |
33 | int n = l(d.faces); |
34 | long now = sysNow(); |
35 | samples.add(pair(now, n)); |
36 | while (first(samples).a <= now-toMS(timeout)) |
37 | removeFirst(samples); |
38 | |
39 | ++totalSamples; |
40 | if (totalSamples > skipFirst) { |
41 | double avg = 0; |
42 | for (Pair<Long, Int> p : samples) |
43 | avg += p.b; |
44 | avg /= l(samples); |
45 | |
46 | double threshold = nn == 0 ? 0.7 : 0.3; |
47 | print(avg); |
48 | int newNN = iceil(avg-threshold); |
49 | consoleTitle(newNN + " " + formatDouble(avg, 2)); |
50 | |
51 | if (newNN != nn) { |
52 | nn = newNN; |
53 | onNNChange(); |
54 | } |
55 | } |
56 | } |
57 | }); |
58 | d.start(); |
59 | } |
60 | |
61 | svoid onNNChange() { |
62 | playWAVSnippet( |
63 | //#1013246 |
64 | nn > 0 ? #1013246 : #1013250 |
65 | ); |
66 | } |
67 | |
68 | // original author Bartosz Firyn (SarXos) |
69 | sclass FaceDetector { |
70 | new HaarCascadeDetector detector; |
71 | Webcam webcam; |
72 | L<DetectedFace> faces; |
73 | L onDetect = synchroList(); |
74 | |
75 | void start() { |
76 | //webcam = Webcam.getDefault(); // TODO: better web cam selection |
77 | webcam = last(webcam.getWebcams()); |
78 | if (setViewSize) |
79 | webcam.setViewSize(WebcamResolution.VGA.getSize()); |
80 | webcam.open(true); |
81 | |
82 | thread "Face Detection" { |
83 | while licensed { |
84 | if (!webcam.isOpen()) return; |
85 | L<DetectedFace> _faces = synchroList(); |
86 | for (DetectedFace f : detector.detectFaces(ImageUtilities.createFImage(webcam.getImage()))) |
87 | if (f.getConfidence() >= confidenceThreshold) |
88 | _faces.add(f); |
89 | faces = _faces; |
90 | |
91 | print("Have " + n2(faces, "face")); |
92 | pnl(map faceToString(faces)); |
93 | |
94 | pcallFAll(onDetect); |
95 | //_print(); |
96 | } |
97 | } |
98 | } |
99 | } |
100 | |
101 | sS faceToString(DetectedFace f) { |
102 | ret f == null ? "" : f.getBounds() + "*" + f.getConfidence(); |
103 | } |
Began life as a copy of #1009812
download show line numbers debug dex old transpilations
Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Snippet ID: | #1013245 |
Snippet name: | Face detection without webcam window |
Eternal ID of this version: | #1013245/21 |
Text MD5: | 3155bc45338d5a49ed619c903bcbf93d |
Transpilation MD5: | 8d7eceb40d118642849180d845ee22dd |
Author: | stefan |
Category: | javax / desktop / camera |
Type: | JavaX source code (desktop) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-12-29 09:40:26 |
Source code size: | 2656 bytes / 103 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 597 / 1384 |
Version history: | 20 change(s) |
Referenced in: | [show references] |