fasdfsa commited on
Commit
dc9ccba
·
1 Parent(s): 904b524

find_rectange_text ok

Browse files
fix_char_rectangle.py CHANGED
@@ -13,9 +13,9 @@
13
  """
14
 
15
  # huggingface\imradv3\src\WeChatOcrCpp\WeChatOcrResult.cpp
 
16
  # cpp 加工原始 wechatocr 结果,处理多字框(现在是让它们有相同坐标)。todo: 改成一个字一个框
17
 
18
-
19
  is_debug = False
20
 
21
  dic_cache = {}
@@ -1258,7 +1258,11 @@ if __name__ == '__main__':
1258
  import base64
1259
 
1260
  with open('data/0022.jpg', "rb") as f:
1261
- img = f.read()
 
 
 
 
1262
 
1263
  base64_bytes = base64.b64encode(img)
1264
  base64_str = base64_bytes.decode('ascii')
@@ -1270,6 +1274,11 @@ if __name__ == '__main__':
1270
  else:
1271
  raise Exception('something wrong.')
1272
 
 
 
 
 
 
1273
  dir = '/yingedu/project/ocr_server_test/data/'
1274
  m5 = 'b0cd8663d03d596eab297a8250bf116c'
1275
 
 
13
  """
14
 
15
  # huggingface\imradv3\src\WeChatOcrCpp\WeChatOcrResult.cpp
16
+ # std::string OcrResult::ToJson(std::string& pth_img) const {
17
  # cpp 加工原始 wechatocr 结果,处理多字框(现在是让它们有相同坐标)。todo: 改成一个字一个框
18
 
 
19
  is_debug = False
20
 
21
  dic_cache = {}
 
1258
  import base64
1259
 
1260
  with open('data/0022.jpg', "rb") as f:
1261
+ img_bytes = f.read()
1262
+
1263
+ img_buffer_numpy = np.frombuffer(img_bytes, dtype=np.uint8)
1264
+
1265
+ img = cv2.imdecode(img_buffer_numpy, 1)
1266
 
1267
  base64_bytes = base64.b64encode(img)
1268
  base64_str = base64_bytes.decode('ascii')
 
1274
  else:
1275
  raise Exception('something wrong.')
1276
 
1277
+
1278
+ # 连续相同的 x, y, w, h 的单字符坐标,给它修正为准确的单字坐标
1279
+
1280
+
1281
+
1282
  dir = '/yingedu/project/ocr_server_test/data/'
1283
  m5 = 'b0cd8663d03d596eab297a8250bf116c'
1284
 
src/WeChatOcrCpp/WeChatOcrCpp.vcxproj CHANGED
@@ -46,7 +46,7 @@
46
  <CharacterSet>Unicode</CharacterSet>
47
  </PropertyGroup>
48
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49
- <ConfigurationType>Application</ConfigurationType>
50
  <UseDebugLibraries>false</UseDebugLibraries>
51
  <PlatformToolset>v143</PlatformToolset>
52
  <WholeProgramOptimization>true</WholeProgramOptimization>
 
46
  <CharacterSet>Unicode</CharacterSet>
47
  </PropertyGroup>
48
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
49
+ <ConfigurationType>DynamicLibrary</ConfigurationType>
50
  <UseDebugLibraries>false</UseDebugLibraries>
51
  <PlatformToolset>v143</PlatformToolset>
52
  <WholeProgramOptimization>true</WholeProgramOptimization>
src/WeChatOcrCpp/main.cpp CHANGED
@@ -146,18 +146,18 @@ int main()
146
  std::cout << "OCR服务器已启动,按Enter键退出..." << std::endl;
147
  std::cin.get();
148
 
149
- std::string result = rec("E:/huggingface/imradv3/data/0022.jpg");
150
- json jsn;
151
- std::string jsn_str;
152
-
153
- try {
154
- jsn = json::parse(result);
155
- jsn_str = jsn.dump(4);
156
- }
157
- catch (const std::exception& e) {
158
- std::cerr << "OCR识别异常: " << e.what() << std::endl;
159
- return 1;
160
- }
161
 
162
  return 0;
163
  }
 
146
  std::cout << "OCR服务器已启动,按Enter键退出..." << std::endl;
147
  std::cin.get();
148
 
149
+ // std::string result = rec("E:/huggingface/imradv3/data/0022.jpg");
150
+ // json jsn;
151
+ // std::string jsn_str;
152
+ //
153
+ // try {
154
+ // jsn = json::parse(result);
155
+ // jsn_str = jsn.dump(4);
156
+ // }
157
+ //catch (const std::exception& e) {
158
+ // std::cerr << "OCR识别异常: " << e.what() << std::endl;
159
+ // return 1;
160
+ //}
161
 
162
  return 0;
163
  }
src/WpfEditor/MainWindow.xaml.cs CHANGED
@@ -629,9 +629,9 @@ namespace WpfEditor
629
  // 找出框选的文字
630
  JObject jsn = (JObject)Editor._jsn_OpenImg.DeepClone();
631
 
632
- jsn = Global.find_rectange_text(jsn, (int)(actualLeft+0.5), (int)(actualTop+0.5), (int)(actualWidth+0.5), (int)(actualHeight));
633
 
634
- //SetText(JObject jsn, string md5)
635
 
636
 
637
  }
 
629
  // 找出框选的文字
630
  JObject jsn = (JObject)Editor._jsn_OpenImg.DeepClone();
631
 
632
+ jsn = Global.find_rectange_text(jsn, (int)(actualLeft+0.5), (int)(actualTop+0.5), (int)(actualWidth+0.5), (int)(actualHeight+0.5));
633
 
634
+ Editor.SetText(jsn, Editor._md5_OpenImg);
635
 
636
 
637
  }
src/WpfEditor/global.cs CHANGED
@@ -35,7 +35,6 @@ namespace WpfEditor
35
  return (ImagePath: pth_img, JsonPath: pth_json);
36
  }
37
 
38
-
39
  // 阿里 ocr的 JSON 需要添加 guid
40
  public static JObject set_guid_md5(JObject jsn, string md5)
41
  {
@@ -95,15 +94,16 @@ namespace WpfEditor
95
  int width = jsn.Value<int>("width");
96
  int height = jsn.Value<int>("height");
97
 
 
98
  JArray prism_wordsInfo_new = new JArray();
 
 
 
 
99
 
100
  var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
101
  foreach (var item in prism_wordsInfo)
102
  {
103
- JArray charInfo_new = new JArray();
104
- string line_word_new = "";
105
- JArray pos_new = new JArray();
106
-
107
  JArray charInfo = item["charInfo"].Value<JArray>();
108
  string line_word = item["word"].Value<string>();
109
  //int angle = item["angle"].Value<int>();
@@ -124,6 +124,17 @@ namespace WpfEditor
124
  continue;
125
  }
126
 
 
 
 
 
 
 
 
 
 
 
 
127
  // 再看列文字左右是否在选区内
128
  foreach (var item2 in charInfo)
129
  {
@@ -140,7 +151,6 @@ namespace WpfEditor
140
 
141
  // 这个字在选区内
142
  charInfo_new.Add(item2.DeepClone());
143
-
144
  }
145
  }
146
 
@@ -148,42 +158,58 @@ namespace WpfEditor
148
 
149
  if (charInfo_new.Count > 0)
150
  {
 
 
151
  var first = charInfo_new[0];
152
  var last = charInfo_new[charInfo_new.Count - 1];
153
 
 
 
 
 
 
 
154
  JObject lu_pos = new JObject
155
  {
156
- ["x"] = first["x"],
157
- ["y"] = first["y"]
158
  };
159
  JObject ru_pos = new JObject
160
  {
161
- ["x"] = (int)last["x"] + (int)last["w"],
162
- ["y"] = last["y"]
163
  };
164
-
165
-
166
- JObject item_new = new JObject
 
 
 
167
  {
168
- ["charInfo"] = charInfo_new
 
169
  };
170
 
171
- prism_wordsInfo_new.Add(item_new);
 
 
 
 
172
  }
173
  }
174
 
175
  if (prism_wordsInfo_new.Count > 0)
176
  {
177
- jsn["prism_wordsInfo"] = prism_wordsInfo_new;
178
  }
179
  else
180
  {
181
  // 如果没有文字在选区内
182
- jsn = null;
183
 
184
  }
185
 
186
- return jsn;
187
  }
188
  }
189
  }
 
35
  return (ImagePath: pth_img, JsonPath: pth_json);
36
  }
37
 
 
38
  // 阿里 ocr的 JSON 需要添加 guid
39
  public static JObject set_guid_md5(JObject jsn, string md5)
40
  {
 
94
  int width = jsn.Value<int>("width");
95
  int height = jsn.Value<int>("height");
96
 
97
+ JObject jsn_new = new JObject();
98
  JArray prism_wordsInfo_new = new JArray();
99
+ jsn_new["width"] = width;
100
+ jsn_new["height"] = height;
101
+ jsn_new["prism_wordsInfo"] = prism_wordsInfo_new;
102
+
103
 
104
  var prism_wordsInfo = jsn["prism_wordsInfo"].Value<JArray>();
105
  foreach (var item in prism_wordsInfo)
106
  {
 
 
 
 
107
  JArray charInfo = item["charInfo"].Value<JArray>();
108
  string line_word = item["word"].Value<string>();
109
  //int angle = item["angle"].Value<int>();
 
124
  continue;
125
  }
126
 
127
+ // 实际上要生成选区内的新 jsn
128
+ // 下面要生成很多新行,行内的很多符等
129
+ JObject line_new = new JObject();
130
+ string line_word_new = "";
131
+ JArray pos_new = new JArray();
132
+ JArray charInfo_new = new JArray();
133
+ line_new["word"] = "";
134
+ line_new["pos"] = pos_new;
135
+ line_new["charInfo"] = charInfo_new;
136
+ prism_wordsInfo_new.Add(line_new);
137
+
138
  // 再看列文字左右是否在选区内
139
  foreach (var item2 in charInfo)
140
  {
 
151
 
152
  // 这个字在选区内
153
  charInfo_new.Add(item2.DeepClone());
 
154
  }
155
  }
156
 
 
158
 
159
  if (charInfo_new.Count > 0)
160
  {
161
+ line_new["word"] = line_word_new;
162
+
163
  var first = charInfo_new[0];
164
  var last = charInfo_new[charInfo_new.Count - 1];
165
 
166
+ int min_left = first["x"].Value<int>(); // Math.Min
167
+ int max_right = last["x"].Value<int>() + last["w"].Value<int>(); // Math.Max
168
+
169
+ int min_top = Math.Min(first["y"].Value<int>(), last["y"].Value<int>());
170
+ int max_top = Math.Max(first["y"].Value<int>() + first["h"].Value<int>(), last["y"].Value<int>() + last["h"].Value<int>());
171
+
172
  JObject lu_pos = new JObject
173
  {
174
+ ["x"] = min_left,
175
+ ["y"] = min_top
176
  };
177
  JObject ru_pos = new JObject
178
  {
179
+ ["x"] = max_right,
180
+ ["y"] = min_top
181
  };
182
+ JObject rd_pos = new JObject
183
+ {
184
+ ["x"] = max_right,
185
+ ["y"] = max_top
186
+ };
187
+ JObject ld_pos = new JObject
188
  {
189
+ ["x"] = min_left,
190
+ ["y"] = max_top
191
  };
192
 
193
+ pos_new.Add(lu_pos);
194
+ pos_new.Add(ru_pos);
195
+ pos_new.Add(rd_pos);
196
+ pos_new.Add(ld_pos);
197
+
198
  }
199
  }
200
 
201
  if (prism_wordsInfo_new.Count > 0)
202
  {
203
+ //jsn["prism_wordsInfo"] = prism_wordsInfo_new;
204
  }
205
  else
206
  {
207
  // 如果没有文字在选区内
208
+ jsn_new = null;
209
 
210
  }
211
 
212
+ return jsn_new;
213
  }
214
  }
215
  }