fasdfsa commited on
Commit
e5baede
·
1 Parent(s): 2927b69

return aliocr format json

Browse files
Files changed (1) hide show
  1. main.py +114 -11
main.py CHANGED
@@ -25,7 +25,7 @@ import PIL
25
  import cv2
26
  import numpy as np
27
 
28
- is_debug = False
29
 
30
  dic_cache = {}
31
 
@@ -34,7 +34,7 @@ from flask import Flask, request, jsonify
34
  app = Flask(__name__)
35
 
36
  import base64
37
- import math, re
38
 
39
  def save_json(filename, dics):
40
  with open(filename, 'w', encoding='utf-8') as fp:
@@ -162,14 +162,117 @@ def ocr(img):
162
 
163
  # ui\mainwindow.py
164
  for blk in blk_list:
165
- text = blk.get_text()
166
  lines = blk.lines
167
- results.append( { "text": text, "lines":lines } )
168
  for line in blk.lines:
169
- img_draw = cv2.rectangle(img_draw, line[0], line[2], (0, 0, 255), 2) # 在一行坚排文字的左边画一条红线
170
-
171
-
172
- return results, img_draw
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
173
 
174
  @app.route('/comicocr', methods=['post'])
175
  def comicocr():
@@ -193,15 +296,15 @@ def comicocr():
193
  # cv2.imshow('test', img)
194
  # cv2.waitKey()
195
 
196
- results, img_draw = ocr(img)
197
 
198
- return jsonify(results)
199
 
200
 
201
  def main():
202
  if is_debug:
203
  img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg')
204
- blk_list, img_draw = ocr(img)
205
 
206
  cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw)
207
 
 
25
  import cv2
26
  import numpy as np
27
 
28
+ is_debug = True
29
 
30
  dic_cache = {}
31
 
 
34
  app = Flask(__name__)
35
 
36
  import base64
37
+ import math, re, uuid
38
 
39
  def save_json(filename, dics):
40
  with open(filename, 'w', encoding='utf-8') as fp:
 
162
 
163
  # ui\mainwindow.py
164
  for blk in blk_list:
165
+ texts = blk.text
166
  lines = blk.lines
167
+ results.append( { "texts": texts, "lines":lines } )
168
  for line in blk.lines:
169
+ img_draw = cv2.rectangle(img_draw, line[0], line[2], (0, 0, 255), 2)
170
+
171
+ jsn = { "width": img.shape[1], "height": img.shape[0], "prism_wordsInfo": [] }
172
+ for result in results:
173
+ texts, lines = ( result["texts"], result["lines"])
174
+ word = ''.join(texts)
175
+ pos = []
176
+ charInfo = []
177
+
178
+ min_x = 999
179
+ min_y = 999
180
+ max_x = -1
181
+ max_y = -1
182
+
183
+ for text, line in zip(texts, lines):
184
+ lu = line[0]
185
+ ru = line[1]
186
+ rd = line[2]
187
+ ld = line[3]
188
+
189
+ minx = min(lu[0], ld[0])
190
+ maxx = max(ru[0], rd[0])
191
+ miny = min(lu[1], ru[1])
192
+ maxy = max(rd[1], ld[1])
193
+
194
+ if min_x > minx:
195
+ min_x = minx
196
+ if max_x < maxx:
197
+ max_x = maxx
198
+
199
+ if min_y > miny:
200
+ min_y = miny
201
+ if max_y < maxy:
202
+ max_y = maxy
203
+
204
+ for c in text:
205
+ charInfo.append( {"word": c, "x":minx , "y":miny, "w":maxx - minx , "h":maxy - miny, "guid": str( uuid.uuid4() ), "isDeleted": 0 } )
206
+ pass
207
+
208
+ pos = [ { "x":min_x, "y":min_y }, { "x":max_x, "y":min_y }, { "x":max_x, "y":max_y }, { "x":min_x, "y":max_y } ]
209
+
210
+ jsn["prism_wordsInfo"].append( { "word":word, "x":min_x, "y":min_y, "width":max_x - min_x, "height":max_y - min_y, "pos":pos, "charInfo":charInfo} )
211
+
212
+
213
+
214
+ # {
215
+ # "width": 1200,
216
+ # "height": 1801,
217
+ # "prism_wordsInfo": [
218
+ # {
219
+ # "word": "# 简易字",
220
+ # "prob": 0.6273085474967957,
221
+ # "x": 593,
222
+ # "y": 54,
223
+ # "width": 127,
224
+ # "height": 25,
225
+ # "pos": [
226
+ # {
227
+ # "x": 593,
228
+ # "y": 54
229
+ # },
230
+ # {
231
+ # "x": 720,
232
+ # "y": 54
233
+ # },
234
+ # {
235
+ # "x": 720,
236
+ # "y": 79
237
+ # },
238
+ # {
239
+ # "x": 593,
240
+ # "y": 79
241
+ # }
242
+ # ],
243
+ # "charInfo": [
244
+ # {
245
+ # "h": 25,
246
+ # "w": 43,
247
+ # "word": " ",
248
+ # "x": 595,
249
+ # "y": 54,
250
+ # "guid": "164e9305-3e8e-4467-bd76-1c13ee9b6a53",
251
+ # "isDeleted": 0
252
+ # },
253
+ # {
254
+ # "h": 25,
255
+ # "w": 36,
256
+ # "word": "易",
257
+ # "x": 638,
258
+ # "y": 54,
259
+ # "guid": "17319ab0-7dca-4492-b5b3-bfe1d3aee0be",
260
+ # "isDeleted": 0
261
+ # },
262
+ # {
263
+ # "h": 25,
264
+ # "w": 46,
265
+ # "word": "字",
266
+ # "x": 674,
267
+ # "y": 54,
268
+ # "guid": "71cdd286-192e-4461-b89f-89b19548e62f",
269
+ # "isDeleted": 0
270
+ # }
271
+ # ]
272
+ # },
273
+
274
+
275
+ return jsn, img_draw
276
 
277
  @app.route('/comicocr', methods=['post'])
278
  def comicocr():
 
296
  # cv2.imshow('test', img)
297
  # cv2.waitKey()
298
 
299
+ jsn, img_draw = ocr(img)
300
 
301
+ return jsonify(jsn)
302
 
303
 
304
  def main():
305
  if is_debug:
306
  img = imread('E:/huggingface/BallonsTranslator/assets/kcc-0010.jpg')
307
+ jsn, img_draw = ocr(img)
308
 
309
  cv2.imwrite("E:/xxxxxxxxxxxxxxxx.jpg", img_draw)
310