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
 
#ifdef VP_WITH_PADDLE
#include "vp_ppocr_text_detector_node.h"
#include "../../objects/vp_frame_text_target.h"
 
namespace vp_nodes {
        
    vp_ppocr_text_detector_node::vp_ppocr_text_detector_node(std::string node_name, 
                                                            std::string det_model_dir, 
                                                            std::string cls_model_dir, 
                                                            std::string rec_model_dir, 
                                                            std::string rec_char_dict_path):
                                                            vp_primary_infer_node(node_name, "") {
        // to make the code simpler, paddle_ocr has no more config other than model path
        // we need modify source code at ../../third_party/paddle_ocr/ if we need tune the parameters 
        ocr = std::make_shared<PaddleOCR::PPOCR>(det_model_dir, cls_model_dir, rec_model_dir, rec_char_dict_path);
        this->initialized();
    }
    
    vp_ppocr_text_detector_node::~vp_ppocr_text_detector_node() {
        deinitialized();
    }
    
    void vp_ppocr_text_detector_node::postprocess(const std::vector<cv::Mat>& raw_outputs, const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& frame_meta_with_batch) {
 
    }
    
    // please refer to vp_infer_node::run_infer_combinations
    void vp_ppocr_text_detector_node::run_infer_combinations(const std::vector<std::shared_ptr<vp_objects::vp_frame_meta>>& frame_meta_with_batch) {
        assert(frame_meta_with_batch.size() == 1);
        std::vector<cv::Mat> mats_to_infer;
 
        // start
        auto start_time = std::chrono::system_clock::now();
 
        // prepare data, as same as base class
        vp_primary_infer_node::prepare(frame_meta_with_batch, mats_to_infer);
        auto prepare_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start_time);
 
        start_time = std::chrono::system_clock::now();
        // call paddle ocr
        auto ocr_results = ocr->ocr(mats_to_infer);
        assert(ocr_results.size() == 1);
 
        auto& ocr_result = ocr_results[0];
        auto& frame_meta = frame_meta_with_batch[0];
 
        // scan text detected in frame
        for (int i = 0; i < ocr_result.size(); i++) {
            /* code */
            auto& text = ocr_result[i];
            std::vector<std::pair<int, int>> region_vertexes = {{text.box[0][0], text.box[0][1]}, 
                                                                {text.box[1][0], text.box[1][1]}, 
                                                                {text.box[2][0], text.box[2][1]}, 
                                                                {text.box[3][0], text.box[3][1]}};
            // create text target and update back into frame meta
            auto text_target = std::make_shared<vp_objects::vp_frame_text_target>(region_vertexes, text.text, text.score);
            frame_meta->text_targets.push_back(text_target);
        }
        auto infer_time = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start_time);
 
        // can not calculate preprocess time and postprocess time, set 0 by default.
        vp_infer_node::infer_combinations_time_cost(mats_to_infer.size(), prepare_time.count(), 0, infer_time.count(), 0);
    }
}
#endif