Files
ANSCustomModels/tests/FireNSmokeDetection/run_main.cpp
T

114 lines
4.9 KiB
C++
Raw Normal View History

// FireNSmokeDetection_Run — replay a recording through the Fire & Smoke
// custom model the way ANSVIS and ans-custom-host would feed it live, on the
// VIDEO's clock, and print each alarm once as it is confirmed. Headless; this
// is the parity harness against ANSMIND's `firesmoke-run` (same verifier).
//
// FireNSmokeDetection_Run <model dir> <video> [--max-fps N] [--sensitivity F]
// [--smoke-seconds N] [--start S] [--end S] [--log]
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <map>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "ANSCustomFireNSmoke.h"
int main(int argc, char** argv) {
if (argc < 3) {
std::fprintf(stderr,
"usage: FireNSmokeDetection_Run <model dir> <video> [--max-fps N] [--sensitivity F]\n"
" [--smoke-seconds N] [--start S] [--end S] [--log]\n");
return 2;
}
const std::string modelDir = argv[1];
const std::string video = argv[2];
double maxFps = 6, startS = 0, endS = 1e9;
std::string sensitivity = "0.5", smokeSeconds = "0";
bool log = false;
for (int i = 3; i < argc; ++i) {
const std::string a = argv[i];
auto next = [&](const char* what) -> std::string {
if (i + 1 >= argc) { std::fprintf(stderr, "%s needs a value\n", what); std::exit(2); }
return argv[++i];
};
if (a == "--max-fps") maxFps = std::stod(next("--max-fps"));
else if (a == "--sensitivity") sensitivity = next("--sensitivity");
else if (a == "--smoke-seconds") smokeSeconds = next("--smoke-seconds");
else if (a == "--start") startS = std::stod(next("--start"));
else if (a == "--end") endS = std::stod(next("--end"));
else if (a == "--log") log = true;
else { std::fprintf(stderr, "unknown option %s\n", a.c_str()); return 2; }
}
if (log) _putenv_s("ANSCUSTOM_FS_LOG", "1");
ANSCustomFS model;
std::string labelMap;
const auto t0 = std::chrono::steady_clock::now();
if (!model.Initialize(modelDir, 0.5f, labelMap)) {
std::fprintf(stderr, "Initialize failed for %s\n", modelDir.c_str());
return 1;
}
const double initMs = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - t0).count();
std::printf("model %s\nclasses %s\ninit %.0f ms\n", modelDir.c_str(), labelMap.c_str(), initMs);
// Parameters the way the host pushes them: schema from the plugin, values set.
CustomParams params;
model.ConfigureParameters(params);
for (auto& p : params.Parameters) {
if (p.Name == "MaxFps") p.Value = std::to_string(int(maxFps));
else if (p.Name == "Sensitivity") p.Value = sensitivity;
else if (p.Name == "SmokeSeconds") p.Value = smokeSeconds;
}
model.SetParameters(params);
std::printf("rate %.0f fps of video time, sensitivity %s, smoke_seconds %s\n\n", maxFps,
sensitivity.c_str(), smokeSeconds.c_str());
cv::VideoCapture cap(video);
if (!cap.isOpened()) {
std::fprintf(stderr, "cannot open %s\n", video.c_str());
return 1;
}
const double fps = cap.get(cv::CAP_PROP_FPS) > 1 ? cap.get(cv::CAP_PROP_FPS) : 25.0;
long decoded = 0, processed = 0;
double lastT = 0, msSum = 0;
size_t alarms = 0;
std::map<int, double> firstSeen; // track -> first alarm time
std::map<int, double> lastSeen;
cv::Mat frame;
while (cap.read(frame)) {
const double t = double(decoded) / fps;
++decoded;
if (t < startS) continue;
if (t > endS) break;
model.SetFrameClock(t);
const auto a = std::chrono::steady_clock::now();
const std::vector<CustomObject> objs = model.RunInference(frame, "replay");
const double ms = std::chrono::duration<double, std::milli>(std::chrono::steady_clock::now() - a).count();
// The plugin holds its last verdict between detector runs; count a
// processed frame by the clock the plugin honours.
if (ms > 0.5) { ++processed; msSum += ms; }
for (const auto& o : objs) {
lastSeen[o.trackId] = t;
if (firstSeen.count(o.trackId)) continue;
firstSeen[o.trackId] = t;
++alarms;
std::printf("ALARM %-5s t=%7.2fs track %-4d conf=%.2f box=%d,%d %dx%d %s\n",
o.className.c_str(), t, o.trackId, o.confidence, o.box.x, o.box.y, o.box.width,
o.box.height, o.extraInfo.c_str());
std::fflush(stdout);
}
lastT = t;
}
size_t held = 0;
for (const auto& d : model.LastDebug())
if (!d.confirmed) ++held;
std::printf("\n%ld frames decoded, %ld processed over %.1fs of video - %zu alarm(s), %zu track(s) held back\n",
decoded, processed, lastT, alarms, held);
if (processed) std::printf("per processed frame: %.1f ms mean\n", msSum / double(processed));
return 0;
}