Video Analysis底层库拆分,sdk的go封装
zhangmeng
2019-11-19 5fe6018c5ce963778de2d31666e61649dd02901c
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "detector.h"
 
#include <stdio.h>
#include <stdlib.h>
#include "TH_PlateID.h"
 
#include "csdk_struct.h"
 
namespace csdk_wrap
{
    static TH_PlateIDCfg config;
    static unsigned char *fmem;
    static unsigned char *pmem;
 
    void init_plate_id_config(const cPlateIDCfg *params);
    void set_enable_plate_format(const cPlateIDCfg *params);
 
    int init_plate_id_detector(const cPlateIDCfg *params, char *soPath) {
        // 初始化配置参数
        init_plate_id_config(params);
 
        // 设置Licence Manager运行路径
        int ret = TH_SetSoPath(soPath);
        printf("TH_SetSoPath ret %d\n", ret);
        // 初始化 sdk
        ret = TH_InitPlateIDSDK(&config);
        printf("TH_InitPlateIDSDK ret %d\n", ret);
 
        // 设置车牌类型识别
        set_enable_plate_format(params);
 
        // 设置识别阈值
        TH_SetRecogThreshold(params->bLocateTh, params->bOCRTh, &config);
 
        return ret;
    }
 
    void init_plate_id_config(const cPlateIDCfg *params) {
        int fMemSize = params->nFastMemorySize * 1024;
        int pMemSize = params->nMemorySize * 1024 * 1024;
 
        fmem = (unsigned char*)malloc(fMemSize * sizeof(unsigned char));
        pmem = (unsigned char*)malloc(pMemSize * sizeof(unsigned char));
 
        // 默认参数
        config.nMinPlateWidth = 60;
        config.nMaxPlateWidth = 400;
        config.bVertCompress = 0;
        config.nImageFormat = ImageFormatBGR;
        config.bOutputSingleFrame = 1;
        config.bUTF8 = 1;
 
        config.pFastMemory = fmem;
        config.nFastMemorySize = fMemSize;
        config.pMemory = pmem;
        config.nMemorySize= pMemSize;
 
        if (params->nMinPlateWidth > 60 && params->nMinPlateWidth < 400) {
            config.nMinPlateWidth = params->nMinPlateWidth;
        }
        if (params->nMaxPlateWidth > 60 && params->nMaxPlateWidth < 400) {
            config.nMaxPlateWidth = params->nMaxPlateWidth;
        }
        
        config.nMaxImageWidth = params->nMaxImageWidth;
        config.nMaxImageHeight = params->nMaxImageHeight;
        config.bIsFieldImage = params->bIsFieldImage;
        config.bMovingImage = params->bMovingImage;
        config.nOrderOpt = params->nOrderOpt;
        config.bLeanCorrection = params->bLeanCorrection;
        config.nImproveSpeed = params->nImproveSpeed;
        config.bCarLogo = params->bCarLogo;
        config.bLotDetect = params->bLotDetect;
        config.bShadow = params->bShadow;
        config.bShieldRailing = params->bShieldRailing;
        config.bCarModel = params->bCarModel;
    }
 
    void set_enable_plate_format(const cPlateIDCfg *params) {
        if (params->bOnlyLocation == 1) {
            TH_SetEnabledPlateFormat(PARAM_ONLY_LOCATION_ON, &config);
            return;
        }
        if (params->bOnlyTwoRowYellow == 1) {
            TH_SetEnabledPlateFormat(PARAM_ONLY_TWOROWYELLOW_ON, &config);
            return;
        }
        if (params->bIndividual == 1) {
            TH_SetEnabledPlateFormat(PARAM_INDIVIDUAL_ON, &config);
        }
        if (params->bTwoRowYellow == 1) {
            TH_SetEnabledPlateFormat(PARAM_TWOROWYELLOW_ON, &config);
        }
        if (params->bArmPolice == 1) {
            TH_SetEnabledPlateFormat(PARAM_ARMPOLICE_ON, &config);
        }
        if (params->bArmPolice2 == 1) {
            TH_SetEnabledPlateFormat(PARAM_ARMPOLICE2_ON, &config);
        }
        if (params->bTwoRowArmy == 1) {
            TH_SetEnabledPlateFormat(PARAM_TWOROWARMY_ON, &config);
        }
        if (params->bTractor == 1) {
            TH_SetEnabledPlateFormat(PARAM_TRACTOR_ON, &config);
        }
        if (params->bEmbassy == 1) {
            TH_SetEnabledPlateFormat(PARAM_EMBASSY_ON, &config);
        }
        if (params->bChangNei == 1) {
            TH_SetEnabledPlateFormat(PARAM_CHANGNEI_ON, &config);
        }
        if (params->bMinHang == 1) {
            TH_SetEnabledPlateFormat(PARAM_MINHANG_ON, &config);
        }
        if (params->bConsulate == 1) {
            TH_SetEnabledPlateFormat(PARAM_CONSULATE_ON, &config);
        }
        if (params->bNewEnergy == 1) {
            TH_SetEnabledPlateFormat(PARAM_NEWENERGY_ON, &config);
        }
    }
 
    cPlateIDResult* plate_id_detect(int *plateIDCount, const cIMAGE *img) {
        if (!img) {
            return NULL;
        }
 
        cPlateIDResult *ppos = NULL;
        int nResultNum = 1;
 
        ::TH_PlateIDResult result[6];        
        memset(&result[0], 0, sizeof(result));
 
        // 设置识别区域
        ::TH_RECT rcDetect;
        rcDetect.top = 0;
        rcDetect.right = img->width;
        rcDetect.bottom = img->height;
        rcDetect.left = 0;
 
        int ret = TH_RecogImage((BYTE*)(img->data), img->width, img->height, result, &nResultNum, &rcDetect, &config);
        // printf("TH_RecogImage ret = %d\n", ret);
        if (ret == 0){
            ret = TH_EvaluateCarColor((BYTE*)(img->data), img->width, img->height, result, &nResultNum, &rcDetect, &config);
        }
        if (ret == 0 && nResultNum > 0){
            ppos = (cPlateIDResult*)malloc(nResultNum * sizeof(cPlateIDResult));
            *plateIDCount = nResultNum;
            memcpy(ppos, result, sizeof(TH_PlateIDResult) * nResultNum);
        }
        return ppos;
    }
 
    int uninit_plate_id_detector() {
        free(fmem);
        free(pmem);
        fmem = NULL;
        pmem = NULL;
 
        return TH_UninitPlateIDSDK(&config);
    }
 
} // csdk_wrap