wangrong
2025-01-23 02819b5c047bb354b0ef2374e7c6a6ac4bbd5bba
samples/from_argv_ba_jam_rtsp.cpp
@@ -1,120 +1,122 @@
#include "../nodes/vp_file_src_node.h"
#include "../nodes/vp_rtsp_src_node.h"
#include "../nodes/infers/vp_yolo_detector_node.h"
#include "../nodes/track/vp_sort_track_node.h"
#include "../nodes/ba/vp_ba_jam_node.h"
#include "../nodes/osd/vp_ba_jam_osd_node.h"
#include "../nodes/vp_screen_des_node.h"
#include "../nodes/vp_rtmp_des_node.h"
#include "../nodes/broker/vp_json_kafka_broker_node.h"
#include "../nodes/vp_file_des_node.h"
#include "../nodes/vp_split_node.h"
#include "../nodes/record/vp_record_node.h"
#include "../nodes/vp_fake_des_node.h"
#include "../utils/analysis_board/vp_analysis_board.h"
#include <vector>
#include <string>
#include <iostream>
#include <map>
#include <memory>
int main(int argc, char* argv[]) {
    VP_SET_LOG_LEVEL(vp_utils::vp_log_level::WARN);
    VP_LOGGER_INIT();
    std::vector<std::string> args(argv + 1, argv + argc);
    // 默认的命令行参数
    std::string rtsp_base_url = "rtsp://192.168.1.24:8554/demo";
    std::string kafka_server_point = "192.168.0.85:9092";
    float resize_ratio = 0.4;
    std::vector<std::string> rtsp_urls;
    // 解析命令行参数
    if (args.size() >= 22) {
        for (int i = 0; i < 20; ++i) {
            rtsp_urls.push_back(args[i]);
        }
        resize_ratio = std::stof(args[20]);
        kafka_server_point = args[21];
    } else {
        std::cout << "Usage: " << argv[0] << " <rtsp_path_1> ... <rtsp_path_20> <resize_ratio> <kafka_server_point>\n";
        std::cout << "Example: " << argv[0] << " rtsp://192.168.1.24:8554/demo 0.4 192.168.0.85:9092\n";
        return -1;
    }
    // Create RTSP source nodes
    std::vector<std::shared_ptr<vp_nodes::vp_rtsp_src_node>> rtsp_sources;
    for (int i = 0; i < 20; ++i) {
        rtsp_sources.push_back(std::make_shared<vp_nodes::vp_rtsp_src_node>(
            "rtsp_src_" + std::to_string(i + 1), i + 1, rtsp_urls[i], resize_ratio, "avdec_h264"));
    }
    auto yolo_detector = std::make_shared<vp_nodes::vp_yolo_detector_node>("yolo_detector",
        "./vp_data/models/det_cls/yolov3-tiny-2022-0721_best.weights",
        "./vp_data/models/det_cls/yolov3-tiny-2022-0721.cfg",
        "./vp_data/models/det_cls/yolov3_tiny_5classes.txt");
    auto tracker = std::make_shared<vp_nodes::vp_sort_track_node>("sort_tracker");
    // Define regions for every channel
    auto define_regions = [](int num_sources) {
        std::map<int, std::vector<vp_objects::vp_point>> regions;
        for (int i = 1; i <= num_sources; ++i) {
            regions[i] = std::vector<vp_objects::vp_point>{
                vp_objects::vp_point(0, 520), vp_objects::vp_point(450, 50),
                vp_objects::vp_point(650, 50), vp_objects::vp_point(1000, 520)};
        }
        return regions;
    };
    auto ba_jam = std::make_shared<vp_nodes::vp_ba_jam_node>("ba_jam", define_regions(20));
    auto json_kafka_broker_0 = std::make_shared<vp_nodes::vp_json_kafka_broker_node>(
        "json_kafka_broker_0", kafka_server_point, "vp_ba_jam", vp_nodes::vp_broke_for::BARESULT);
    auto osd = std::make_shared<vp_nodes::vp_ba_jam_osd_node>("osd");
    auto recorder = std::make_shared<vp_nodes::vp_record_node>("recorder", "./record", "./record");
    auto split = std::make_shared<vp_nodes::vp_split_node>("split", true);
    // Create 20 fake destination nodes, one for each channel
    std::vector<std::shared_ptr<vp_nodes::vp_fake_des_node>> fake_des_nodes;
    for (int i = 1; i <= 20; ++i) {
        fake_des_nodes.push_back(std::make_shared<vp_nodes::vp_fake_des_node>("fake_des_" + std::to_string(i), i));
    }
    // Construct pipeline
    for (auto& rtsp_source : rtsp_sources) {
        yolo_detector->attach_to({rtsp_source});
    }
    tracker->attach_to({yolo_detector});
    ba_jam->attach_to({tracker});
    json_kafka_broker_0->attach_to({ba_jam});
    osd->attach_to({json_kafka_broker_0});
    recorder->attach_to({osd});
    split->attach_to({recorder});
    // Attach fake destination nodes (one for each channel)
    for (auto& fake_des_node : fake_des_nodes) {
        fake_des_node->attach_to({split});
    }
    // Start RTSP sources
    for (auto& rtsp_source : rtsp_sources) {
        rtsp_source->start();
    }
    std::string wait;
    std::getline(std::cin, wait);
    // Cleanup
    for (auto& rtsp_source : rtsp_sources) {
        rtsp_source->detach_recursively();
    }
    return 0;
}
#include "../nodes/vp_file_src_node.h"
#include "../nodes/vp_rtsp_src_node.h"
#include "../nodes/vp_rtsp_ffmpeg_src_node.h"
#include "../nodes/vp_split_node.h"
#include "../nodes/infers/vp_trt_vehicle_detector.h"
#include "../nodes/infers/vp_trt_vehicle_plate_detector.h"
#include "../nodes/infers/vp_trt_vehicle_color_classifier.h"
#include "../nodes/infers/vp_yolo_detector_node.h"
#include "../nodes/osd/vp_osd_node.h"
#include "../nodes/vp_sync_node.h"
#include "../nodes/track/vp_sort_track_node.h"
#include "../nodes/ba/vp_ba_jam_node.h"
#include "../nodes/ba/vp_ba_stop_node.h"
#include "../nodes/osd/vp_ba_stop_osd_node.h"
#include "../nodes/broker/vp_json_kafka_broker_node.h"
#include "../nodes/record/vp_record_node.h"
#include "../nodes/vp_screen_des_node.h"
#include "../nodes/vp_fake_des_node.h"
#include "../nodes/vp_placeholder_node.h"
#include "../utils/analysis_board/vp_analysis_board.h"
/*
* ## firesmoke_detect_sample ##
* detect firesmoke using yolo.
*/
int main(int argc, char* argv[]) {
    VP_SET_LOG_LEVEL(vp_utils::vp_log_level::INFO);
    VP_LOGGER_INIT();
    std::vector<std::string> args(argv + 1, argv + argc);
    // 默认的命令行参数
    std::string rtsp_path = "rtsp://127.0.0.1:8554/demo";
    std::string kafka_server_point = "127.0.0.1:9092";
    float resize_ratio = 0.4;
    int skip_interval = 3;
    int channel_index = 21;
    bool usegpu = false; // 新增参数,默认值为 false
    // 如果提供了至少 6 个参数,则覆盖默认值
    if (args.size() >= 6) {
        rtsp_path = args[0];
        resize_ratio = std::stof(args[1]);
        kafka_server_point = args[2];
        skip_interval = std::stoi(args[3]);
        channel_index = std::stoi(args[4]);
        std::string usegpu_arg = args[5];
        // 将字符串转换为布尔值,支持 true/false 或 1/0
        std::transform(usegpu_arg.begin(), usegpu_arg.end(), usegpu_arg.begin(), ::tolower);
        if (usegpu_arg == "true" || usegpu_arg == "1") {
            usegpu = true;
        } else if (usegpu_arg == "false" || usegpu_arg == "0") {
            usegpu = false;
        } else {
            std::cerr << "Invalid value for <usegpu>. Please use 'true', 'false', '1', or '0'.\n";
            return -1;
        }
    } else {
        std::cout << "Usage: " << argv[0] << " <rtsp_path> <resize_ratio> <kafka_server_point> <skip_interval> <channel_index> <usegpu>\n";
        std::cout << "Example: " << argv[0] << " rtsp://127.0.0.1:8554/demo 0.4 127.0.0.1:9092 5 21 true\n";
        return -1;
    }
    // create nodes
    //auto file_src_0 = std::make_shared<vp_nodes::vp_file_src_node>("file_src_0", 0, rtsp_path, resize_ratio);
    //auto file_src_0 = std::make_shared<vp_nodes::vp_rtsp_src_node>("rtsp_src_1", 0, rtsp_path, resize_ratio, "avdec_h264", skip_interval);
    auto file_src_0 = std::make_shared<vp_nodes::vp_rtsp_ffmpeg_src_node>("rtsp_src_1", channel_index, rtsp_path, resize_ratio, skip_interval, usegpu);
    //auto file_src_1 = std::make_shared<vp_nodes::vp_file_src_node>("file_src_1", 1, "./vp_data/test_video/falldown.mp4", 0.5);
    //auto split = std::make_shared<vp_nodes::vp_split_node>("split", false, true);  // split by deep-copy not by channel!
    //branch 0
    //auto trt_vehicle_detector = std::make_shared<vp_nodes::vp_trt_vehicle_detector>("vehicle_detector", "./vp_data/models/trt/vehicle/vehicle_detection.trt");
    auto vehicle_detector = std::make_shared<vp_nodes::vp_yolo_detector_node>("vehicle_detector", "./vp_data/models/det_cls/yolov3-tiny-2022-0721_best.weights", "./vp_data/models/det_cls/yolov3-tiny-2022-0721.cfg", "./vp_data/models/det_cls/yolov3_tiny_5classes.txt");
    auto trt_vehicle_plate_detector = std::make_shared<vp_nodes::vp_trt_vehicle_plate_detector>("vehicle_plate_detector", "./vp_data/models/trt/plate/vehicle_plate_box_detection.trt", "./vp_data/models/trt/plate/vehicle_plate_text_recognition.trt");
    auto trt_vehicle_color_classifier = std::make_shared<vp_nodes::vp_trt_vehicle_color_classifier>("color_cls", "./vp_data/models/trt/vehicle/vehicle_color_detection.trt", std::vector<int>{0, 1, 2});
    auto tracker = std::make_shared<vp_nodes::vp_sort_track_node>("sort_tracker");
    // define a region in frame for every channel (value MUST in the scope of frame'size)
    std::map<int, std::vector<vp_objects::vp_point>> regions = {
        {channel_index, std::vector<vp_objects::vp_point>{vp_objects::vp_point(280, 30), vp_objects::vp_point(340, 30), vp_objects::vp_point(600, 330), vp_objects::vp_point(30, 350)}},  // channel0 -> region
        {1, std::vector<vp_objects::vp_point>{vp_objects::vp_point(280, 30), vp_objects::vp_point(340, 30), vp_objects::vp_point(600, 330), vp_objects::vp_point(30, 350)}}   // channel1 -> region
    };
    auto ba_jam = std::make_shared<vp_nodes::vp_ba_jam_node>("ba_jam", regions);
    auto ba_stop = std::make_shared<vp_nodes::vp_ba_stop_node>("ba_stop", regions);
    auto json_kafka_broker_0 = std::make_shared<vp_nodes::vp_json_kafka_broker_node>("json_kafka_broker_0", kafka_server_point, "vp_ba_result", vp_nodes::vp_broke_for::BARESULT);
    auto osd_0 = std::make_shared<vp_nodes::vp_ba_stop_osd_node>("osd_0", "./vp_data/font/NotoSansCJKsc-Medium.otf");
    auto recorder = std::make_shared<vp_nodes::vp_record_node>("recorder", "./record", "./record");
    // for testing. USING fake_des node in production
    auto screen_des_0 = std::make_shared<vp_nodes::vp_screen_des_node>("screen_des_0", 0);
    auto fake_des_0 = std::make_shared<vp_nodes::vp_fake_des_node>("fake_des_0", 0);
    // construct pipeline
    vehicle_detector->attach_to({file_src_0});
    trt_vehicle_plate_detector->attach_to({vehicle_detector});
    trt_vehicle_color_classifier->attach_to({trt_vehicle_plate_detector});
    tracker->attach_to({trt_vehicle_color_classifier});
    ba_jam->attach_to({tracker});
    ba_stop->attach_to({ba_jam});
    json_kafka_broker_0->attach_to({ba_stop});
    osd_0->attach_to({json_kafka_broker_0});
    recorder->attach_to({osd_0});
    fake_des_0->attach_to({recorder});
    file_src_0->start();
    // for debug purpose
    // vp_utils::vp_analysis_board board({file_src_0});
    // board.display(1, false);
    std::string wait;
    std::getline(std::cin, wait);
    file_src_0->detach_recursively();
}