#include "ANSLIB.h"
#include <iostream>

int Yolov12EngineUnitTest(std::string modelFilePath, std::string videoFilePath) {

    std::vector<std::string> classes;
    std::string labelMap;
    std::string licenseKey = "";
    std::string modelZipFilePassword = "";
    float modelThreshold = 0.25;
    float modelConfThreshold = 0.5;
    float modelNMSThreshold = 0.5;
    int modelType = 4;// 17;//Yolov12 (4 RT)
    int detectorType = 1;
    std::string optmizedModelFolder;

	ANSCENTER::ANSLIB ansLib;
	ansLib.Initialize(licenseKey.c_str(), 
                     modelFilePath.c_str(), 
                     modelZipFilePassword.c_str(), 
                     modelThreshold, 
                     modelConfThreshold, 
                     modelNMSThreshold, 
                     modelType, detectorType,1, labelMap);

    std::stringstream ss(labelMap);
    while (ss.good())
    {
        std::string substr;
        getline(ss, substr, ',');
        classes.push_back(substr);
    }
    std::cout << "classes:" << classes.size();
    std::cout << "begin read video" << std::endl;
    cv::VideoCapture capture(videoFilePath);

    if (!capture.isOpened()) {
        printf("could not read this video file...\n");
        return -1;
    }


    while (true)
    {
        cv::Mat frame;
        if (!capture.read(frame)) // if not success, break loop
        {
            std::cout << "\n Cannot read the video file. please check your video.\n";
            break;
        }
		std::vector<ANSCENTER::Object> detectionResult;
		ansLib.RunInference(frame, "cameraId", detectionResult);

		for (auto& obj : detectionResult) {
			cv::rectangle(frame, obj.box, cv::Scalar(0, 255, 0), 2);
			cv::putText(frame, cv::format("%s:%d", classes[obj.classId], obj.classId), cv::Point(obj.box.x, obj.box.y - 5),
				0, 0.6, cv::Scalar(0, 0, 255), 1, cv::LINE_AA);
		}
        cv::imshow("ANS Object Tracking", frame);
        if (cv::waitKey(30) == 27) // Wait for 'esc' key press to exit
        {
            break;
        }
    }
    capture.release();
    cv::destroyAllWindows();
    std::cout << "End of program.\n";
    return 0;
}
int TestYOLOV12() {
    std::string modelPath = "C:\\Programs\\DemoAssets\\ANS_Plate_1503(GPU)_1.zip";
    std::string videoFile = "C:\\Programs\\DemoAssets\\Videos\\FireNSmoke\\SimFire.mp4";
    Yolov12EngineUnitTest(modelPath, videoFile);
}
int main()
{
	TestYOLOV12();
}

