diff options
| author | Onur Gündüz <ogunduz@gmail.com> | 2026-10-20 11:44:30 +0200 |
|---|---|---|
| committer | Onur Gündüz <ogunduz@gmail.com> | 2026-10-20 11:44:30 +0200 |
| commit | 9b9005101e6170a226acb265428aac429be6edbf (patch) | |
| tree | f89c76f4a5c2a00a72abe9b92468ac9f15083b94 /overlay.c | |
| download | facedet-9b9005101e6170a226acb265428aac429be6edbf.tar.gz facedet-9b9005101e6170a226acb265428aac429be6edbf.zip | |
Init
Diffstat (limited to 'overlay.c')
| -rw-r--r-- | overlay.c | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/overlay.c b/overlay.c new file mode 100644 index 0000000..ab3fcf4 --- /dev/null +++ b/overlay.c @@ -0,0 +1,57 @@ +#include "overlay.h" +#include "draw.h" + +#include <math.h> +#include <stdio.h> + +static const vix_rgb GREEN = {80, 220, 80}; + +static void draw_face(const vix_canvas *canvas, const yunet_detection *face, int index) { + double left = face->x, top = face->y; + double right = left + face->w, bottom = top + face->h; + if (!isfinite(left) || !isfinite(top) || !isfinite(right) || !isfinite(bottom) || + face->w <= 0 || face->h <= 0 || right <= 0 || bottom <= 0 || + left >= canvas->w || top >= canvas->h) + return; + int x = (int)floor(fmax(0, left)); + int y = (int)floor(fmax(0, top)); + int w = (int)ceil(fmin(canvas->w, right)) - x; + int h = (int)ceil(fmin(canvas->h, bottom)) - y; + vix_draw_rect(canvas, x, y, w, h, GREEN, 2); + + char label[32]; + snprintf(label, sizeof label, "face %d %.2f", index + 1, face->score); + int label_width = vix_text_width(label, 1); + int label_x = x; + if (label_x + label_width > canvas->w) + label_x = canvas->w > label_width ? canvas->w - label_width : 0; + vix_draw_text(canvas, label_x, y - 9 < 2 ? 2 : y - 9, label, GREEN, 1); + + for (int k = 0; k < 5; k++) { + float lx = face->lmk[k][0], ly = face->lmk[k][1]; + if (!isfinite(lx) || !isfinite(ly) || lx < -2 || ly < -2 || + lx >= canvas->w + 2 || ly >= canvas->h + 2) + continue; + vix_draw_dot(canvas, (int)lroundf(lx), (int)lroundf(ly), 2, GREEN); + } +} + +int overlay_write_ppm(const char *path, unsigned char *rgb, int width, int height, + const yunet_detection *faces, int count) { + if (!path || !*path || !rgb || width < 1 || height < 1 || width > 4096 || + height > 4096 || count < 0 || (count && !faces)) + return -1; + vix_canvas canvas = {rgb, width, height}; + for (int i = 0; i < count; i++) + draw_face(&canvas, &faces[i], i); + + FILE *file = fopen(path, "wb"); + if (!file) + return -1; + size_t pixels = (size_t)canvas.w * canvas.h; + int failed = fprintf(file, "P6\n%d %d\n255\n", canvas.w, canvas.h) < 0 || + fwrite(canvas.px, 3, pixels, file) != pixels; + if (fclose(file) != 0) + failed = 1; + return failed ? -1 : 0; +} |
