ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
face_follow.c
Go to the documentation of this file.
2#include <math.h>
3#include <string.h>
4
5static float clampf(float value, float low, float high) {
6 return fminf(high, fmaxf(low, value));
7}
8
9static face_follow_rect_t full_frame(void) {
10 return (face_follow_rect_t){0, 0, 1, 1};
11}
12
14 memset(state, 0, sizeof(*state));
15 state->crop = state->target = full_frame();
16}
17
18static float face_follow_overlap(face_follow_rect_t a, face_follow_rect_t b) {
19 float w = fmaxf(0, fminf(a.x + a.w, b.x + b.w) - fmaxf(a.x, b.x));
20 float h = fmaxf(0, fminf(a.y + a.h, b.y + b.h) - fmaxf(a.y, b.y));
21 float intersection = w * h;
22 return intersection / fmaxf(0.000001f, a.w * a.h + b.w * b.h - intersection);
23}
24
26 const face_follow_detection_t *faces, size_t count,
27 double now) {
28 int selected = -1;
29 float best = -1;
30 bool retain = state->tracking && now - state->last_seen < 1.5;
31 for (size_t i = 0; i < count; i++) {
32 face_follow_rect_t box = faces[i].box;
33 if (!isfinite(box.x) || !isfinite(box.y) || !isfinite(box.w) ||
34 !isfinite(box.h) || faces[i].confidence < 0.65f || box.w <= 0 ||
35 box.h <= 0)
36 continue;
37 float cx = box.x + box.w * 0.5f, cy = box.y + box.h * 0.5f;
38 float score;
39 if (retain) {
40 float dx = cx - (state->face.x + state->face.w * 0.5f);
41 float dy = cy - (state->face.y + state->face.h * 0.5f);
42 float distance = sqrtf(dx * dx + dy * dy);
43 float match = face_follow_overlap(box, state->face);
44 // A bystander must not steal the crop while the selected subject is
45 // absent.
46 if (match < 0.08f && distance > fmaxf(0.12f, state->face.w * 0.8f))
47 continue;
48 score = 3 * match - distance;
49 } else {
50 float dx = cx - 0.5f, dy = cy - 0.45f;
51 score = sqrtf(box.w * box.h) - 0.35f * sqrtf(dx * dx + dy * dy);
52 }
53 if (score > best) {
54 best = score;
55 selected = (int)i;
56 }
57 }
58 state->detected = selected >= 0;
59 if (selected < 0) {
60 state->pan_pending = state->zoom_pending = false;
61 state->misses++;
62 return;
63 }
64 bool acquired = !retain;
65 state->face = faces[selected].box;
66 state->last_seen = now;
67 state->tracking = true;
68 state->misses = 0;
69 face_follow_rect_t box = state->face;
70 // Retain the source aspect ratio and let the face fill about two thirds of
71 // the view.
72 float size = clampf(fmaxf(box.w * 1.8f, box.h * 1.6f), 0.35f, 1.0f);
73 float cx = box.x + box.w * 0.5f;
74 float cy = box.y + box.h * 0.55f;
75 // Measure against the last accepted face position, not the moving/clamped
76 // crop. Keep the entire composition fixed through breathing and detector box
77 // noise.
78 float dx = fabsf(cx - state->anchor_x) / state->target.w;
79 float dy = fabsf(cy - state->anchor_y) / state->target.h;
80 if (state->pan_pending &&
81 (cx - state->anchor_x) * state->pan_direction_x +
82 (cy - state->anchor_y) * state->pan_direction_y <=
83 0)
84 state->pan_pending = false;
85 if (dx < 0.12f && dy < 0.09f)
86 state->pan_pending = false;
87 else if (!state->pan_pending && (dx > 0.18f || dy > 0.14f)) {
88 state->pan_pending = true;
89 state->pan_outside_since = now;
90 state->pan_direction_x = cx - state->anchor_x;
91 state->pan_direction_y = cy - state->anchor_y;
92 }
93 // Large moves near the edge still need two or more consistent detections.
94 double pan_grace = dx > 0.35f || dy > 0.25f ? 0.2 : 0.85;
95 bool pan = acquired || (state->pan_pending &&
96 now - state->pan_outside_since >= pan_grace);
97 float zoom_delta = logf(size / state->target.w);
98 float zoom_change = fabsf(zoom_delta);
99 if (state->zoom_pending && zoom_delta * state->zoom_direction <= 0)
100 state->zoom_pending = false;
101 if (zoom_change < 0.12f)
102 state->zoom_pending = false;
103 else if (!state->zoom_pending && zoom_change > 0.18f) {
104 state->zoom_pending = true;
105 state->zoom_outside_since = now;
106 state->zoom_direction = zoom_delta;
107 }
108 bool zoom = acquired ||
109 (state->zoom_pending && now - state->zoom_outside_since >= 1.2);
110 if (pan) {
111 state->anchor_x = cx;
112 state->anchor_y = cy;
113 state->pan_pending = false;
114 }
115 if (zoom) {
116 state->target.w = state->target.h = size;
117 state->zoom_pending = false;
118 }
119 if (pan || zoom) {
120 state->target.x = clampf(state->anchor_x - state->target.w * 0.5f, 0,
121 1 - state->target.w);
122 state->target.y = clampf(state->anchor_y - state->target.h * 0.5f, 0,
123 1 - state->target.h);
124 }
125}
126
127void face_follow_tick(face_follow_state_t *state, double now, bool enabled) {
128 if (!state->initialized) {
129 if (enabled && state->tracking && now - state->last_seen <= 0.7)
130 state->crop = state->target;
131 state->last_tick = now;
132 state->initialized = true;
133 }
134 double dt = fmax(0, fmin(0.1, now - state->last_tick));
135 state->last_tick = now;
136 bool lost = !state->tracking || now - state->last_seen > 0.7;
137 face_follow_rect_t target = enabled && !lost ? state->target : full_frame();
138 if (state->tracking && now - state->last_seen > 1.5)
139 state->tracking = false;
140 float pan = (float)(1 - exp(-dt / (lost || !enabled ? 0.45 : 0.28)));
141 float zoom = (float)(1 - exp(-dt / (lost || !enabled ? 0.45 : 0.35)));
142 float cx = state->crop.x + state->crop.w * 0.5f;
143 float cy = state->crop.y + state->crop.h * 0.5f;
144 cx += (target.x + target.w * 0.5f - cx) * pan;
145 cy += (target.y + target.h * 0.5f - cy) * pan;
146 if (fabsf(cx - (target.x + target.w * 0.5f)) < 0.0001f)
147 cx = target.x + target.w * 0.5f;
148 if (fabsf(cy - (target.y + target.h * 0.5f)) < 0.0001f)
149 cy = target.y + target.h * 0.5f;
150 float size =
151 expf(logf(state->crop.w) + (logf(target.w) - logf(state->crop.w)) * zoom);
152 if (fabsf(size - target.w) < 0.0001f)
153 size = target.w;
154 state->crop =
155 (face_follow_rect_t){clampf(cx - size * 0.5f, 0, 1 - size),
156 clampf(cy - size * 0.5f, 0, 1 - size), size, size};
157}
void face_follow_tick(face_follow_state_t *state, double now, bool enabled)
void face_follow_init(face_follow_state_t *state)
Definition face_follow.c:13
void face_follow_observe(face_follow_state_t *state, const face_follow_detection_t *faces, size_t count, double now)
Definition face_follow.c:25
bool enabled
Is filtering active?
Definition grep.c:63
🔢 Mathematical Utility Functions
🔤 String Manipulation and Shell Escaping Utilities
face_follow_rect_t box
Definition face_follow.h:33
When code needs to work with face follow detection, it keeps box, confidence here instead of scatteri...
Definition face_follow.h:28
face follow state keeps the values that move through the subsystem: crop, target, face,...
Definition face_follow.h:44
face_follow_rect_t crop
Definition face_follow.h:45
face_follow_rect_t face
Definition face_follow.h:45
unsigned int misses
Definition face_follow.h:52
face_follow_rect_t target
Definition face_follow.h:45