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_image.c
Go to the documentation of this file.
4#include <math.h>
5
20
22 if (!context)
23 return;
24 image_destroy(context->output);
25 context->output = NULL;
26 SAFE_FREE(context);
27}
28
30 image_t *source, double now, bool enabled,
31 bool debug, int max_width, int max_height) {
32 if (!source || source->w <= 0 || source->h <= 0)
33 return source;
34 if (!*context && !enabled && !debug)
35 return source;
36 if (!*context) {
37 *context =
39 if (!*context)
40 return source;
41 face_follow_init(&(*context)->state);
42 }
43 face_follow_image_t *ctx = *context;
44 if (source->w != ctx->source_width || source->h != ctx->source_height) {
46 ctx->last_detection = -1;
47 ctx->source_width = source->w;
48 ctx->source_height = source->h;
49 }
50 if ((enabled || debug) && now - ctx->last_detection >= 0.1) {
51 float scale = fminf((float)FACE_FOLLOW_DETECT_WIDTH / source->w,
53 int width = (int)fmaxf(1, source->w * scale),
54 height = (int)fmaxf(1, source->h * scale);
55 for (int y = 0; y < height; y++) {
56 for (int x = 0; x < width; x++) {
57 rgb_pixel_t p =
58 source->pixels[(size_t)(y * source->h / height) * source->w +
59 x * source->w / width];
60 size_t offset = ((size_t)y * width + x) * 3;
61 ctx->bgr[offset] = p.b;
62 ctx->bgr[offset + 1] = p.g;
63 ctx->bgr[offset + 2] = p.r;
64 }
65 }
67 size_t count;
68 face_follow_detect(ctx->results, ctx->bgr, width, height, faces,
70 face_follow_observe(&ctx->state, faces, count, now);
71 ctx->last_detection = now;
72 }
73 face_follow_tick(&ctx->state, now, enabled);
74 if (!enabled && !debug && ctx->state.crop.w == 1)
75 return source;
76 // Keep output geometry stable while bounding the cost of high-resolution
77 // cameras.
78 float output_scale = fminf(
79 1, fminf((float)max_width / source->w, (float)max_height / source->h));
80 int output_width = (int)fmaxf(1, source->w * output_scale);
81 int output_height = (int)fmaxf(1, source->h * output_scale);
82 if (!ctx->output || ctx->output->w != output_width ||
83 ctx->output->h != output_height) {
85 ctx->output = image_new(output_width, output_height);
86 if (!ctx->output)
87 return source;
88 }
89 face_follow_rect_t crop = ctx->state.crop;
90 face_follow_rect_t face = ctx->state.face;
91 float side = fmaxf(face.w * source->w, face.h * source->h);
92 float fx = (face.x + face.w * 0.5f) * source->w,
93 fy = (face.y + face.h * 0.5f) * source->h;
94 float thickness = 2.0f * fmaxf(3.0f, (float)source->w * crop.w / 90.0f);
95 for (int y = 0; y < output_height; y++) {
96 int sy = (int)fminf((float)source->h - 1.0f,
97 (crop.y + (float)y / output_height * crop.h) *
98 (float)source->h);
99 for (int x = 0; x < output_width; x++) {
100 int sx = (int)fminf((float)source->w - 1.0f,
101 (crop.x + (float)x / output_width * crop.w) *
102 (float)source->w);
103 rgb_pixel_t p = source->pixels[(size_t)sy * source->w + sx];
104 if (debug && ctx->state.detected && now - ctx->state.last_seen < 0.3) {
105 float dx = fabsf(sx - fx), dy = fabsf(sy - fy);
106 if ((fabsf(dx - side * 0.5f) < thickness &&
107 dy <= side * 0.5f + thickness) ||
108 (fabsf(dy - side * 0.5f) < thickness &&
109 dx <= side * 0.5f + thickness))
110 p = (rgb_pixel_t){255, 0, 0};
111 }
112 ctx->output->pixels[(size_t)y * output_width + x] = p;
113 }
114 }
115 return ctx->output;
116}
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
void face_follow_detect(unsigned char *results, unsigned char *bgr, int width, int height, face_follow_detection_t *faces, size_t capacity, size_t *count)
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
#define FACE_FOLLOW_RESULT_BUFFER_SIZE
Definition face_follow.h:6
#define FACE_FOLLOW_DETECT_HEIGHT
Definition face_follow.h:5
#define FACE_FOLLOW_DETECT_WIDTH
Definition face_follow.h:4
#define FACE_FOLLOW_MAX_DETECTIONS
Definition face_follow.h:7
image_t * face_follow_image_process(face_follow_image_t **context, image_t *source, double now, bool enabled, bool debug, int max_width, int max_height)
void face_follow_image_destroy(face_follow_image_t *context)
struct face_follow_image face_follow_image_t
audio_visualization_source_t source
Definition fft.c:25
unsigned int height
Definition fft.c:24
unsigned int width
Definition fft.c:24
bool enabled
Is filtering active?
Definition grep.c:63
#define SAFE_FREE(ptr)
#define SAFE_CALLOC(count, size, cast)
void image_destroy(image_t *image)
Destroy an image allocated with image_new().
image_t * image_new(size_t width, size_t height)
Create a new image with standard allocation.
🔢 Mathematical Utility Functions
face follow image is intentionally a small, concrete record. Fields such as state,...
face_follow_state_t state
unsigned char results[FACE_FOLLOW_RESULT_BUFFER_SIZE]
unsigned char bgr[FACE_FOLLOW_DETECT_WIDTH *FACE_FOLLOW_DETECT_HEIGHT *3]
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
int h
Image height in pixels (must be > 0).
rgb_pixel_t * pixels
Pixel data array (width * height RGB pixels, row-major order).
int w
Image width in pixels (must be > 0).
uint8_t b
Blue color component (0-255).
uint8_t g
Green color component (0-255).
uint8_t r
Red color component (0-255).