-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathinterface_crnn.cpp
153 lines (138 loc) · 4.4 KB
/
interface_crnn.cpp
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
#include <stdio.h>
#include "net.h"
#include "text_recognization.h"
#include <omp.h>
ncnn::Net netCrnn;
void DKBoxTextRecognizationInit()
{
netCrnn.load_param("crnn.param");
netCrnn.load_model("crnn.bin");
}
char* DKBoxTextRecognizationProcess(const char* rgbfilename, int iWidth, int iHeight, DKSBox box, DKSBoxTextRecognizationParam param)
{
//裁剪文字区域
FILE *stream = NULL;
stream = fopen(rgbfilename, "rb");
if(NULL == stream)
{
fprintf(stderr, "imgdata read error!");
exit(1);
}
unsigned char* rgbData = new unsigned char[iWidth*iHeight*3];
fread(rgbData, 1, iHeight*iWidth*3, stream);
fclose(stream);
int y_top = box.y1 > box.y2 ? box.y2 : box.y1;
int y_bottom = box.y3 > box.y4 ? box.y3 : box.y4;
int x_left = box.x1 > box.x4 ? box.x4 : box.x1;
int x_right = box.x2 > box.x3 ? box.x2 : box.x3;
y_top = y_top > 0 ? y_top : 0;
x_left = x_left > 0 ? x_left : 0;
y_bottom = y_bottom < iHeight ? y_bottom : iHeight;
x_right = x_right < iWidth ? x_right : iWidth;
int cols = x_right - x_left;
int rows = y_bottom - y_top;
ncnn::Mat img;
img.create(cols, rows, 3, 1);
int area = iWidth * iHeight;
#ifdef DEBUG
printf("%d,%d,%d,%d\n", y_top, x_left, y_bottom, x_right);
#endif
#ifndef RGB_META
#pragma omp parallel for
//rgb_panel
for(int i = y_top; i < y_bottom; i++)
{
for(int j = x_left; j < x_right; j++)
{
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)) = rgbData[i*iWidth + j];
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)+1) = rgbData[area + i*iWidth + j];
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)+2) = rgbData[area*2 + i*iWidth + j];
}
}
#else
// rgb_metapixel
for(int i = y_top; i < y_bottom + 1; i++)
{
for(int j = x_left; j < x_right + 1; j++)
{
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)) = rgbData[i*iWidth*3 + 3 * j];
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)+1) = rgbData[i*iWidth*3 + 3*j+1];
*((unsigned char*)(img.data)+3*(i-y_top)*cols+3*(j-x_left)+2) = rgbData[i*iWidth*3 + 3*j+2];
}
}
#endif
delete [] rgbData;
// fclose(stream);
//预处理并获取字符序列索引
ncnn::Mat in,input_data;
ncnn::Mat pred;
in = ncnn::Mat::from_pixels((unsigned char*)img.data, ncnn::Mat::PIXEL_RGB2GRAY, cols, rows);
ncnn::resize_bilinear(in,input_data,100,32);
input_data.reshape(100,32,1);
#pragma omp parallel for
for(int i=0; i<100 * 32; i++)
{
*((float*)input_data.data+i) = ((*((float*)input_data.data+i))/255.f - 0.5)/0.5;
}
ncnn::Extractor ex = netCrnn.create_extractor();
// ex.set_num_threads(2);
ex.set_light_mode(true);
ex.input("data", input_data);
ex.extract("preds", pred);
//对输出字符索引解码得到字符串。
float maxprob;
int pre_index = 0;
char alphabet[] = "0123456789abcdefghijklmnopqrstuvwxyz";
char *result;
if(NULL == (result = (char *)malloc(26 * sizeof(char))))
/*请使用if来判断,这是有必要的*/
{
perror("error...");
exit(1);
}
// static std::vector<char> result;
// std::vector<char>::iterator iter = result.begin();
// for(;iter!=result.end();)
// iter = result.erase(iter);
int ccount=0;
for (int j=0; j<pred.h; j++)
{
int char_index = 0;
maxprob = *((float*)pred.row(j));
for (int i=0; i<pred.w; i++)
{
if (*((float*)pred.row(j)+i) > maxprob)
{
maxprob = *((float*)pred.row(j)+i);
char_index = i;
}
}
if(char_index != 0 && (j==0 || char_index != pre_index))
{
result[ccount] = alphabet[char_index-1];
ccount++;
// result.push_back(alphabet[char_index-1]);
}
pre_index = char_index;
}
result[ccount] = '\0';
// result.push_back('\0');
if(param.lexicon)
{
// return result;
char * editre;
if((editre = minDistanceWord(result)) != NULL)
return editre;
else
return result;
}
else
{
return result;
// return result.data();
}
}
void DKBoxTextRecognizationEnd()
{
netCrnn.clear();
}