Files
ANSCustomModels/tests/FireNSmokeDetection/headless_check.cpp
T
2026-06-18 05:57:14 +10:00

94 lines
3.8 KiB
C++

// Headless detection checker — no GUI. Runs a video through the Fire/Smoke detector,
// logs every detection (class, confidence, box size and % of frame), and SAVES annotated
// frames (boxes + labels drawn) to an output folder for visual inspection.
//
// Usage: headless_check [modelDir] [videoPath] [maxFrames] [outDir]
#include <iostream>
#include <sstream>
#include <vector>
#include <string>
#include <opencv2/opencv.hpp>
#include <opencv2/core/utils/filesystem.hpp>
#include "ANSCustomFireNSmoke.h"
int main(int argc, char** argv) {
std::string modelDirectory = (argc > 1) ? argv[1]
: "C:\\Projects\\ANSVIS\\Models\\ANS_FireSmoke_v2.0";
std::string videoFilePath = (argc > 2) ? argv[2]
: "C:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\ANSFireFull.mp4";
int maxFrames = (argc > 3) ? std::atoi(argv[3]) : 100000;
std::string outDir = (argc > 4) ? argv[4]
: "C:\\Projects\\CLionProjects\\ANSCustomModels\\cmake-build-release\\detections";
cv::utils::fs::createDirectories(outDir);
std::cout << "Model: " << modelDirectory << "\nVideo: " << videoFilePath
<< "\nSaving annotated frames to: " << outDir << std::endl;
ANSCustomFS detector;
std::string labelMap;
detector.OptimizeModel(true);
if (!detector.Initialize(modelDirectory, 0.5f, labelMap)) {
std::cerr << "Initialize FAILED" << std::endl;
return 2;
}
std::cout << "Initialized. labelMap=" << labelMap << std::endl;
// Parse class names
std::vector<std::string> classes;
std::stringstream ss(labelMap);
std::string tok;
while (std::getline(ss, tok, ',')) classes.push_back(tok);
cv::VideoCapture capture(videoFilePath);
if (!capture.isOpened()) {
std::cerr << "Unable to open video" << std::endl;
return 3;
}
int frameIdx = 0, framesWithDet = 0, totalDet = 0, firstDetFrame = -1, savedFrames = 0;
while (frameIdx < maxFrames) {
cv::Mat frame;
if (!capture.read(frame)) break;
++frameIdx;
std::vector<CustomObject> objs = detector.RunInference(frame);
if (objs.empty()) continue;
++framesWithDet;
if (firstDetFrame < 0) firstDetFrame = frameIdx;
const double frameArea = double(frame.cols) * frame.rows;
cv::Mat draw = frame.clone();
for (const auto& o : objs) {
++totalDet;
const double pct = 100.0 * (double(o.box.width) * o.box.height) / frameArea;
const std::string name = (o.classId >= 0 && o.classId < (int)classes.size())
? classes[o.classId] : ("class" + std::to_string(o.classId));
std::cout << " frame " << frameIdx
<< " [" << frame.cols << "x" << frame.rows << "]"
<< " " << name
<< " conf=" << o.confidence
<< " box=" << o.box.width << "x" << o.box.height
<< " (" << pct << "% of frame)" << std::endl;
cv::rectangle(draw, o.box, cv::Scalar(123, 255, 123), 2);
cv::putText(draw, cv::format("%s %.2f (%.2f%%)", name.c_str(), o.confidence, pct),
cv::Point(o.box.x, std::max(0, o.box.y - 6)),
cv::FONT_HERSHEY_SIMPLEX, 0.6, cv::Scalar(0, 0, 255), 2, cv::LINE_AA);
}
const std::string outPath = outDir + "\\frame_" +
cv::format("%05d", frameIdx) + ".jpg";
cv::imwrite(outPath, draw);
++savedFrames;
}
std::cout << "\n=== SUMMARY ===\n"
<< "frames processed : " << frameIdx << "\n"
<< "frames w/ detect : " << framesWithDet << "\n"
<< "total detections : " << totalDet << "\n"
<< "annotated saved : " << savedFrames << " (in " << outDir << ")\n"
<< "first detect at : frame " << firstDetFrame << std::endl;
capture.release();
return 0;
}