-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceDetectionApp.java
More file actions
119 lines (103 loc) · 4.6 KB
/
Copy pathFaceDetectionApp.java
File metadata and controls
119 lines (103 loc) · 4.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package Phase5_CollectionsLambdasStreams.Collections.FaceDetectionApp;
import java.util.List;
import java.util.Map;
/**
* FaceDetectionApp - runnable demo that ties everything together.
* <p>
*
* The program SIMULATES 20 video frames from a Detector. Each frame is fed
* into a FaceRepository which exercises every collection type. Then we
* print a series of reports that exercise the queries those data structures
* make cheap.
* <p>
*
* Run:
* cd src
* javac Basics/Collections/FaceDetectionApp/*.java
* java Basics.Collections.FaceDetectionApp.FaceDetectionApp
* <p>
*
* Collection cheat-sheet for this demo
* ------------------------------------
* ArrayList<Face> all detections in order
* ArrayDeque<List<Face>> sliding window of recent frames
* LinkedHashSet<String> distinct people, insertion order
* HashSet<Integer> face IDs the user dismissed (O(1) check)
* HashMap<String,List> faces grouped by person
* HashMap<String,Long> per-person counter via Map.merge
* TreeMap<Long,Face> timeline for range queries
* PriorityQueue<Face> alerts queue, max-confidence first
*/
public class FaceDetectionApp {
public static void main(String[] args) {
Detector det = new Detector(42L); // seed for determinism
FaceRepository repo = new FaceRepository();
// ===== Simulate 20 frames =====
for (int frame = 0; frame < 20; frame++) {
List<Face> faces = det.detect(frame);
repo.recordFrame(frame, faces);
}
// User dismisses some random face IDs (suppose UI clicks).
repo.dismiss(3);
repo.dismiss(7);
// ===== Reports =====
section("1) Summary");
System.out.println("Total detections : " + repo.totalDetected());
System.out.println("Distinct people : " + repo.distinctPeople());
System.out.println("Dismissed IDs : " + repo.dismissedCount());
System.out.println("Frames in window : " + repo.windowSize());
section("2) People seen (insertion order)");
for (String name : repo.peopleSeen()) {
System.out.println(" " + name + " count=" + repo.counts().get(name));
}
section("3) Top-3 most confident detections (PriorityQueue trick)");
for (Face f : repo.topK(3)) {
System.out.printf(" id=%d %-9s conf=%.2f%n", f.id(), f.personName(), f.confidence());
}
section("4) Recent people in the sliding window (last few frames)");
System.out.println(" " + repo.recentPeople());
section("5) Timeline range query - earliest 100ms");
long earliest = repo.all().isEmpty() ? 0L : repo.all().get(0).timestampMs();
var window = repo.inRange(earliest, earliest + 100);
System.out.println(" " + window.size() + " face(s) in [" + earliest + ", " + (earliest + 100) + "]");
for (Face f : window) {
System.out.printf(" ts=%d id=%d %s conf=%.2f%n",
f.timestampMs(), f.id(), f.personName(), f.confidence());
}
section("6) Alerts queue (high-confidence, non-dismissed, known person)");
Face alert;
while ((alert = repo.nextAlert()) != null) {
System.out.printf(" ALERT id=%d %-9s conf=%.2f%n",
alert.id(), alert.personName(), alert.confidence());
}
section("7) Index by person - first 3 faces for each");
for (Map.Entry<String, List<Face>> e : repo.byPerson().entrySet()) {
String name = e.getKey();
List<Face> fs = e.getValue();
System.out.println(" " + name + " total=" + fs.size());
fs.stream().limit(3).forEach(f ->
System.out.printf(" id=%d conf=%.2f ts=%d%n",
f.id(), f.confidence(), f.timestampMs())
);
}
// SAMPLE OUTPUT (numbers depend on the seeded Random)
// ====== 1) Summary ======
// Total detections : 30
// Distinct people : 6
// Dismissed IDs : 2
// Frames in window : 5
// ====== 2) People seen (insertion order) ======
// Alice count=6
// Carol count=5
// unknown count=4
// ...
// ====== 3) Top-3 most confident detections (PriorityQueue trick) ======
// id=12 Alice conf=0.99
// id=27 Dave conf=0.97
// id= 5 Bob conf=0.95
// ...
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}