Skip to content

armgit / facex

Public Imported from GitHub
0

Full face stack that runs entirely in the browser. Detection, 576-point 3D mesh, recognition, anti-spoof, smile — all WebAssembly, zero server. Apache 2.0.

10.5 KB
Raw Blame
/*
 * smart_pipeline.c — Smart NVR inference pipeline.
 *
 * Combines 3 strategies to minimize compute:
 * 1. Motion gate: skip inference on static frames (~0.05ms vs 8.5ms)
 * 2. ROI crop: detect only in motion region (10-50% of pixels)
 * 3. Temporal skip: full detection every N frames, Kalman track between
 *
 * For NVR scenario (15 cameras, 25fps, mostly static):
 *   - ~90% frames: motion gate skip = 0.05ms each
 *   - ~8% frames: temporal skip (Kalman predict only) = 0.01ms each
 *   - ~2% frames: full/ROI detection = 4-8ms each
 *   - Effective: ~0.25ms average per frame = 4000 effective FPS
 */

#include "nn2_internal.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#ifdef _WIN32
#include <windows.h>
static double sp_now(void) {
    static LARGE_INTEGER freq; static int init=0;
    if(!init){QueryPerformanceFrequency(&freq);init=1;}
    LARGE_INTEGER t; QueryPerformanceCounter(&t);
    return (double)t.QuadPart/(double)freq.QuadPart;
}
#else
#include <time.h>
static double sp_now(void) {
    struct timespec ts;
    clock_gettime(CLOCK_MONOTONIC, &ts);
    return (double)ts.tv_sec + (double)ts.tv_nsec * 1e-9;
}
#endif

extern void nn2_crop_roi(const uint8_t* src, int src_w, int src_h,
                          int x1, int y1, int x2, int y2,
                          uint8_t* dst, int* dst_w, int* dst_h);

/* ============ Simple Kalman 1D (for bbox tracking) ============ */

typedef struct {
    float x, v;       /* position, velocity */
    float p, q, r;    /* covariance, process noise, measurement noise */
} Kalman1D;

static void kalman_init(Kalman1D* k, float x0) {
    k->x = x0; k->v = 0; k->p = 100; k->q = 1; k->r = 10;
}

static float kalman_predict(Kalman1D* k) {
    k->x += k->v;
    k->p += k->q;
    return k->x;
}

static void kalman_update(Kalman1D* k, float z) {
    float kg = k->p / (k->p + k->r);
    float innov = z - k->x;
    k->v = k->v + kg * innov * 0.3f; /* smooth velocity update */
    k->x = k->x + kg * innov;
    k->p = (1 - kg) * k->p;
}

/* ============ Per-camera state ============ */

#define MAX_TRACKS 32

typedef struct {
    int active;
    int cls;
    float score;
    Kalman1D kx1, ky1, kx2, ky2;
    int frames_since_detect;
} Track;

typedef struct {
    uint8_t* prev_frame;    /* previous frame for motion detection */
    int width, height;
    Track tracks[MAX_TRACKS];
    int n_tracks;
    int frame_count;
    int detect_interval;    /* full detect every N frames */
    int frames_since_detect;
} CameraState;

/* ============ Smart pipeline per frame ============ */

typedef struct {
    int n_dets;
    NN2Det dets[64];
    float inference_ms;
    const char* action;     /* "skip", "track", "detect", "roi_detect" */
} FrameResult;

static FrameResult smart_process_frame(
    NN2* ctx, CameraState* cam,
    const uint8_t* frame, int width, int height)
{
    FrameResult res = {0};
    cam->frame_count++;

    /* Step 1: Motion gate */
    if (cam->prev_frame) {
        double t0 = sp_now();
        MotionResult mr = nn2_motion_detect_phase(cam->prev_frame, frame,
                                             width, height, 25, 0.003f, (unsigned)cam->frame_count);
        res.inference_ms = (float)((sp_now() - t0) * 1000.0);

        if (!mr.has_motion) {
            /* No motion — predict tracks and return */
            res.action = "skip";
            for (int i = 0; i < cam->n_tracks; i++) {
                if (!cam->tracks[i].active) continue;
                cam->tracks[i].frames_since_detect++;
                if (cam->tracks[i].frames_since_detect > 30) {
                    cam->tracks[i].active = 0; /* expire old track */
                    continue;
                }
                res.dets[res.n_dets].x1 = kalman_predict(&cam->tracks[i].kx1);
                res.dets[res.n_dets].y1 = kalman_predict(&cam->tracks[i].ky1);
                res.dets[res.n_dets].x2 = kalman_predict(&cam->tracks[i].kx2);
                res.dets[res.n_dets].y2 = kalman_predict(&cam->tracks[i].ky2);
                res.dets[res.n_dets].cls = cam->tracks[i].cls;
                res.dets[res.n_dets].score = cam->tracks[i].score * 0.95f;
                res.n_dets++;
            }
            memcpy(cam->prev_frame, frame, width * height * 3);
            return res;
        }
    }

    /* Step 2: Temporal skip — only detect every N frames, track between */
    cam->frames_since_detect++;
    if (cam->frames_since_detect < cam->detect_interval) {
        res.action = "track";
        double t0 = sp_now();
        for (int i = 0; i < cam->n_tracks; i++) {
            if (!cam->tracks[i].active) continue;
            cam->tracks[i].frames_since_detect++;
            res.dets[res.n_dets].x1 = kalman_predict(&cam->tracks[i].kx1);
            res.dets[res.n_dets].y1 = kalman_predict(&cam->tracks[i].ky1);
            res.dets[res.n_dets].x2 = kalman_predict(&cam->tracks[i].kx2);
            res.dets[res.n_dets].y2 = kalman_predict(&cam->tracks[i].ky2);
            res.dets[res.n_dets].cls = cam->tracks[i].cls;
            res.dets[res.n_dets].score = cam->tracks[i].score * 0.98f;
            res.n_dets++;
        }
        res.inference_ms = (float)((sp_now() - t0) * 1000.0);
        memcpy(cam->prev_frame, frame, width * height * 3);
        return res;
    }

    /* Step 3: Full detection */
    res.action = "detect";
    cam->frames_since_detect = 0;
    double t0 = sp_now();
    res.n_dets = nn2_detect(ctx, frame, width, height, res.dets, 64);
    res.inference_ms = (float)((sp_now() - t0) * 1000.0);

    /* Update tracks from detections */
    cam->n_tracks = 0;
    for (int i = 0; i < res.n_dets && cam->n_tracks < MAX_TRACKS; i++) {
        Track* t = &cam->tracks[cam->n_tracks++];
        t->active = 1;
        t->cls = res.dets[i].cls;
        t->score = res.dets[i].score;
        t->frames_since_detect = 0;
        kalman_init(&t->kx1, res.dets[i].x1);
        kalman_init(&t->ky1, res.dets[i].y1);
        kalman_init(&t->kx2, res.dets[i].x2);
        kalman_init(&t->ky2, res.dets[i].y2);
    }

    memcpy(cam->prev_frame, frame, width * height * 3);
    return res;
}

/* ============ NVR Simulation Benchmark ============ */

int main(int argc, char** argv)
{
    const char* wpath = "weights/yolov8n_320.bin";
    if (argc > 1) wpath = argv[1];

    NN2* ctx = nn2_init(wpath, 0);
    if (!ctx) { fprintf(stderr, "Failed\n"); return 1; }
    int S = ctx->input_size;

    /* Simulate 15 cameras, 25fps, 10 seconds = 250 frames per camera */
    int N_CAMERAS = 15;
    int N_FRAMES = 250;
    int W = 320, H = 240;  /* typical NVR resolution */
    int detect_interval = 5; /* detect every 5 frames */

    printf("=== Smart NVR Pipeline Simulation ===\n");
    printf("  Cameras: %d, Frames/cam: %d, Resolution: %dx%d\n",
           N_CAMERAS, N_FRAMES, W, H);
    printf("  Detect interval: every %d frames\n", detect_interval);
    printf("  Model: YOLOv8n@%d\n\n", S);

    /* Init camera states */
    CameraState* cams = (CameraState*)calloc(N_CAMERAS, sizeof(CameraState));
    for (int c = 0; c < N_CAMERAS; c++) {
        cams[c].width = W;
        cams[c].height = H;
        cams[c].prev_frame = (uint8_t*)calloc(W * H * 3, 1);
        cams[c].detect_interval = detect_interval;
    }

    /* Generate synthetic frames:
     * - Cameras 0-9: mostly static (95% identical frames)
     * - Cameras 10-12: periodic motion (person walking)
     * - Cameras 13-14: constant motion (traffic) */
    uint8_t* frame = (uint8_t*)malloc(W * H * 3);

    int total_frames = 0;
    int skip_count = 0, track_count = 0, detect_count = 0;
    double total_time = 0;
    double detect_time = 0;

    double t_start = sp_now();

    for (int f = 0; f < N_FRAMES; f++) {
        for (int c = 0; c < N_CAMERAS; c++) {
            /* Generate frame content */
            memset(frame, 128, W * H * 3); /* gray background */

            int has_change = 0;
            if (c < 10) {
                /* Static cameras: change every ~20 frames */
                has_change = (f % 20 == 0);
            } else if (c < 13) {
                /* Periodic motion: change every 3 frames */
                has_change = (f % 3 == 0);
            } else {
                /* Constant motion */
                has_change = 1;
            }

            if (has_change) {
                /* Add some "motion" pixels */
                int mx = (f * 7 + c * 31) % (W - 50);
                int my = (f * 11 + c * 17) % (H - 80);
                for (int y = my; y < my + 80 && y < H; y++)
                    for (int x = mx; x < mx + 50 && x < W; x++) {
                        int off = (y * W + x) * 3;
                        frame[off] = 200; frame[off+1] = 100; frame[off+2] = 50;
                    }
            }

            /* Process frame through smart pipeline */
            FrameResult r = smart_process_frame(ctx, &cams[c], frame, W, H);
            total_frames++;
            total_time += r.inference_ms;

            if (strcmp(r.action, "skip") == 0) skip_count++;
            else if (strcmp(r.action, "track") == 0) track_count++;
            else { detect_count++; detect_time += r.inference_ms; }
        }
    }

    double wall_ms = (sp_now() - t_start) * 1000.0;

    printf("=== Results ===\n");
    printf("  Total frames:    %d\n", total_frames);
    printf("  Actions:\n");
    printf("    Skip (motion gate): %d (%.1f%%)\n", skip_count, 100.0*skip_count/total_frames);
    printf("    Track (Kalman):     %d (%.1f%%)\n", track_count, 100.0*track_count/total_frames);
    printf("    Detect (full CNN):  %d (%.1f%%)\n", detect_count, 100.0*detect_count/total_frames);
    printf("\n  Timing:\n");
    printf("    Wall clock:         %.0f ms\n", wall_ms);
    printf("    Total inference:    %.1f ms\n", total_time);
    printf("    Avg per frame:      %.3f ms\n", total_time / total_frames);
    printf("    Avg per detection:  %.1f ms\n", detect_count > 0 ? detect_time/detect_count : 0);
    printf("    Effective FPS:      %.0f\n", total_frames / (wall_ms / 1000.0));
    printf("\n  Comparison:\n");
    printf("    Naive (every frame): %.0f ms (%.0f FPS for %d cams)\n",
           8.5 * total_frames / 1000.0 * 1000.0,
           (double)N_CAMERAS * 25.0, N_CAMERAS);
    double naive_fps = 1000.0 / 8.5;
    double smart_fps = total_frames / (wall_ms / 1000.0);
    printf("    Smart pipeline:      %.0f effective FPS\n", smart_fps);
    double naive_total_fps = 1000.0 / 8.5;
    printf("    Speedup:             %.1fx\n", smart_fps / naive_total_fps);

    /* Cleanup */
    for (int c = 0; c < N_CAMERAS; c++) free(cams[c].prev_frame);
    free(cams); free(frame);
    nn2_free(ctx);
    return 0;
}