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 /main.c | |
| download | facedet-9b9005101e6170a226acb265428aac429be6edbf.tar.gz facedet-9b9005101e6170a226acb265428aac429be6edbf.zip | |
Init
Diffstat (limited to 'main.c')
| -rw-r--r-- | main.c | 216 |
1 files changed, 216 insertions, 0 deletions
@@ -0,0 +1,216 @@ +#include "yunet.h" +#include "image.h" +#include "overlay.h" + +#include <ctype.h> +#include <errno.h> +#include <math.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <sys/stat.h> + +static int token(FILE *f, char *out, size_t cap) { + if (feof(f) || ferror(f)) + return -1; + int c; + for (;;) { + c = fgetc(f); + if (c == EOF) + return -1; + if (c == '#') { + do { + c = fgetc(f); + if (c == EOF) + return -1; + } while (c != '\n'); + continue; + } + if (!isspace((unsigned char)c)) + break; + } + size_t n = 0; + do { + if (n + 1 >= cap) + return -1; + out[n++] = (char)c; + c = fgetc(f); + if (c == EOF) + return -1; + } while (!isspace((unsigned char)c)); + /* CRLF is one header separator; raster bytes are never skipped. */ + if (c == '\r') { + c = fgetc(f); + if (c == EOF || (c != '\n' && ungetc(c, f) == EOF)) + return -1; + } + out[n] = '\0'; + return 0; +} + +static int dimension(const char *s) { + char *end; + errno = 0; + long v = strtol(s, &end, 10); + return errno || !*s || *end || v < 1 || v > 4096 ? -1 : (int)v; +} + +static unsigned char *read_ppm(const char *path, int *width, int *height) { + FILE *f = fopen(path, "rb"); + if (!f) + return NULL; + unsigned char *rgb = NULL; + char word[32]; + if (token(f, word, sizeof word) || strcmp(word, "P6")) + goto end; + if (token(f, word, sizeof word) || (*width = dimension(word)) < 0) + goto end; + if (token(f, word, sizeof word) || (*height = dimension(word)) < 0) + goto end; + if (token(f, word, sizeof word) || strcmp(word, "255")) + goto end; + size_t size = (size_t)*width * *height * 3; + rgb = malloc(size); + if (rgb && fread(rgb, 1, size, f) != size) { + free(rgb); + rgb = NULL; + } +end: + fclose(f); + return rgb; +} + +static int threshold(const char *s, float *out) { + char *end; + errno = 0; + float v = strtof(s, &end); + if (errno || !*s || *end || !isfinite(v) || v < 0.0f || v > 1.0f) + return -1; + *out = v; + return 0; +} + +static void usage(FILE *file, const char *program) { + fprintf(file, "Usage: %s [-o output.ppm] image [score_threshold [nms_iou]]\n" + "Input: JPEG, PNG, or binary P6 PPM (maxval 255), up to 4096x4096.\n" + "Output: JSON on stdout. Thresholds must be finite numbers in [0, 1].\n" + " -o, --out FILE Save face boxes, scores, and landmarks as P6 PPM.\n" + " --refine Refine landmarks with optional embedded crop weights.\n" + " -h, --help Show help.\n" + " -- End options, for filenames starting with '-'.\n", program); +} + +int main(int argc, char **argv) { + const char *out_path = NULL, *args[3]; + int nargs = 0, options = 1, refine = 0; + for (int i = 1; i < argc; i++) { + const char *arg = argv[i]; + if (options && !strcmp(arg, "--")) { + options = 0; + continue; + } + if (options && (!strcmp(arg, "-h") || !strcmp(arg, "--help"))) { + usage(stdout, argv[0]); + return 0; + } + if (options && !strcmp(arg, "--refine")) { + if (refine) { + fputs("Specify --refine only once.\n", stderr); + return 1; + } + refine = 1; + continue; + } + if (options && (!strcmp(arg, "-o") || !strcmp(arg, "--out") || + !strncmp(arg, "--out=", 6))) { + if (out_path) { + fputs("Specify only one output file.\n", stderr); + return 1; + } + if (!strncmp(arg, "--out=", 6)) + out_path = arg + 6; + else if (i + 1 < argc) + out_path = argv[++i]; + if (!out_path || !*out_path || !strcmp(out_path, "-")) { + fputs("--out requires a filename; stdout is reserved for JSON.\n", stderr); + return 1; + } + continue; + } + if ((options && arg[0] == '-') || nargs == 3) { + usage(stderr, argv[0]); + return 1; + } + args[nargs++] = arg; + } + float score = 0.6f, nms = 0.3f; + if (!nargs || (nargs > 1 && threshold(args[1], &score)) || + (nargs > 2 && threshold(args[2], &nms))) { + usage(stderr, argv[0]); + return 1; + } + const char *input_path = args[0]; + if (out_path) { + struct stat input_info, output_info; + if (!strcmp(input_path, out_path) || + (stat(input_path, &input_info) == 0 && stat(out_path, &output_info) == 0 && + input_info.st_dev == output_info.st_dev && input_info.st_ino == output_info.st_ino)) { + fputs("The output file must not be the input image.\n", stderr); + return 1; + } + } + int width = 0, height = 0; + unsigned char *rgb = image_read_rgb(input_path, &width, &height); + if (!rgb) + rgb = read_ppm(input_path, &width, &height); + if (!rgb) { + fprintf(stderr, "Cannot read %s as JPEG, PNG, or P6 PPM (maxval 255) with dimensions 1..4096.\n", input_path); + return 1; + } + int iw = (width + 31) & ~31, ih = (height + 31) & ~31; + yunet *m = yunet_create(iw, ih); + int rc = m ? yunet_set_rgb(m, rgb, width, height, (size_t)width * 3) : -1; + if (rc == 0) + rc = yunet_run(m); + int cap = 21 * (iw / 32) * (ih / 32); + yunet_detection *faces = rc == 0 ? malloc((size_t)cap * sizeof(*faces)) : NULL; + int n = faces ? yunet_decode(m, score, nms, faces, cap) : -1; + yunet_free(m); + if (n < 0) { + fprintf(stderr, "Inference failed; memory allocation or input validation failed.\n"); + free(faces); + free(rgb); + return 1; + } + if (refine) { + yunet *refiner = yunet_create_refiner(); + rc = refiner ? yunet_refine(refiner, rgb, width, height, (size_t)width * 3, faces, n) : -1; + yunet_free(refiner); + if (rc) { + fputs("Landmark refinement failed; check embedded weights and available memory.\n", stderr); + free(faces); + free(rgb); + return 1; + } + } + if (out_path && overlay_write_ppm(out_path, rgb, width, height, faces, n) != 0) { + fprintf(stderr, "Could not write overlay to %s.\n", out_path); + free(faces); + free(rgb); + return 1; + } + free(rgb); + printf("{\"width\":%d,\"height\":%d,\"input_width\":%d,\"input_height\":%d,\"faces\":[", + width, height, iw, ih); + for (int i = 0; i < n; i++) { + const yunet_detection *d = &faces[i]; + printf("%s{\"box\":[%.7g,%.7g,%.7g,%.7g],\"score\":%.7g,\"landmarks\":[", + i ? "," : "", d->x, d->y, d->w, d->h, d->score); + for (int k = 0; k < 5; k++) + printf("%s[%.7g,%.7g]", k ? "," : "", d->lmk[k][0], d->lmk[k][1]); + printf("]}"); + } + printf("]}\n"); + free(faces); + return fflush(stdout) == 0 ? 0 : 1; +} |
