wangrong
2025-03-19 291afb9c81aae1d15d9e3edcea618d0a2e1fc5a6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#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/ba/vp_ba_wrong_direction_node.h"
#include "../nodes/ba/vp_ba_person_gathering_node.h"
#include "../nodes/ba/vp_ba_person_falldown_node.h"
#include "../nodes/ba/vp_ba_report_detect_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(290, 10), vp_objects::vp_point(340, 10), vp_objects::vp_point(600, 330), vp_objects::vp_point(40, 350)}},  // channel0 -> region
        {1, std::vector<vp_objects::vp_point>{vp_objects::vp_point(290, 10), vp_objects::vp_point(340, 10), vp_objects::vp_point(600, 330), vp_objects::vp_point(40, 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);
    
    // define a line in frame for every channel (value MUST in the scope of frame'size)
    vp_objects::vp_point left_start(10, 10);  // change to proper value
    vp_objects::vp_point left_end(10, 20);  // change to proper value
    vp_objects::vp_point right_start(10, 20);  // change to proper value
    vp_objects::vp_point right_end(10, 10);  // change to proper value
    std::map<int, std::vector<vp_objects::vp_point>> left_lines = {{0, {left_start, left_end}}};  // channel0 -> point vector
    std::map<int, std::vector<vp_objects::vp_point>> right_lines = {{0, {right_start, right_end}}};  // channel0 -> point vector
    int half_screen_width = 800;
    auto ba_wrong_direction = std::make_shared<vp_nodes::vp_ba_wrong_direction_node>("vp_ba_wrong_direction_node", left_lines,right_lines,half_screen_width,true,true);
       
    int min_gathering_count = 2;
    auto ba_person_gathering = std::make_shared<vp_nodes::vp_ba_person_gathering_node>("vp_ba_person_gathering_node", min_gathering_count,true,true);
    auto ba_person_falldown = std::make_shared<vp_nodes::vp_ba_person_falldown_node>("vp_ba_person_falldown_node", true,true);
  
    auto firesmoke_detector = std::make_shared<vp_nodes::vp_yolo_detector_node>("firesmoke_detector", "./vp_data/models/det_cls/firesmoke_yolov5s.onnx", "", "./vp_data/models/det_cls/firesmoke_3classes.txt", 640, 384,1,1000,0.8,0.8);
    auto ba_report_detect = std::make_shared<vp_nodes::vp_ba_report_detect_node>("ba_report_detect_node","fire_smoke",std::vector<int>{1000,1001,1002}, true,true);
 
 
    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});
    ba_wrong_direction->attach_to({ba_stop});
    ba_person_gathering->attach_to({ba_wrong_direction});
    ba_person_falldown->attach_to({ba_person_gathering});
    firesmoke_detector->attach_to({ba_person_falldown});
    ba_report_detect->attach_to({firesmoke_detector});
    json_kafka_broker_0->attach_to({ba_report_detect});
    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();
   
}