|
1 | 1 | # -*- coding: utf-8 -*- |
2 | 2 |
|
3 | 3 | from .detection import get_detector, get_textbox |
4 | | -from .recognition import get_recognizer, get_text |
| 4 | +from .recognition import get_recognizer, get_text, get_text_prob |
5 | 5 | from .utils import group_text_box, get_image_list, calculate_md5, get_paragraph,\ |
6 | 6 | download_and_unzip, printProgressBar, diff, reformat_input,\ |
7 | 7 | make_rotated_img_list, set_result_with_confidence,\ |
@@ -298,6 +298,85 @@ def detect(self, img, min_size = 20, text_threshold = 0.7, low_text = 0.4,\ |
298 | 298 |
|
299 | 299 | return horizontal_list_agg, free_list_agg |
300 | 300 |
|
| 301 | + def recognize_prob(self, img_cv_grey, horizontal_list=None, free_list=None,\ |
| 302 | + decoder = 'greedy', beamWidth= 5, batch_size = 1,\ |
| 303 | + workers = 0, allowlist = None, blocklist = None, detail = 1,\ |
| 304 | + rotation_info = None,paragraph = False,\ |
| 305 | + contrast_ths = 0.1,adjust_contrast = 0.5, filter_ths = 0.003,\ |
| 306 | + y_ths = 0.5, x_ths = 1.0, reformat=True, output_format='standard'): |
| 307 | + |
| 308 | + if reformat: |
| 309 | + img, img_cv_grey = reformat_input(img_cv_grey) |
| 310 | + |
| 311 | + if allowlist: |
| 312 | + ignore_char = ''.join(set(self.character)-set(allowlist)) |
| 313 | + elif blocklist: |
| 314 | + ignore_char = ''.join(set(blocklist)) |
| 315 | + else: |
| 316 | + ignore_char = ''.join(set(self.character)-set(self.lang_char)) |
| 317 | + |
| 318 | + if self.model_lang in ['chinese_tra','chinese_sim']: decoder = 'greedy' |
| 319 | + |
| 320 | + if (horizontal_list==None) and (free_list==None): |
| 321 | + y_max, x_max = img_cv_grey.shape |
| 322 | + horizontal_list = [[0, x_max, 0, y_max]] |
| 323 | + free_list = [] |
| 324 | + |
| 325 | + # without gpu/parallelization, it is faster to process image one by one |
| 326 | + if ((batch_size == 1) or (self.device == 'cpu')) and not rotation_info: |
| 327 | + result = [] |
| 328 | + for bbox in horizontal_list: |
| 329 | + h_list = [bbox] |
| 330 | + f_list = [] |
| 331 | + image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH) |
| 332 | + result0 = get_text_prob(self.character, imgH, int(max_width), self.recognizer, self.converter, image_list,\ |
| 333 | + ignore_char, decoder, beamWidth, batch_size, contrast_ths, adjust_contrast, filter_ths,\ |
| 334 | + workers, self.device) |
| 335 | + result += result0 |
| 336 | + for bbox in free_list: |
| 337 | + h_list = [] |
| 338 | + f_list = [bbox] |
| 339 | + image_list, max_width = get_image_list(h_list, f_list, img_cv_grey, model_height = imgH) |
| 340 | + result0 = get_text_prob(self.character, imgH, int(max_width), self.recognizer, self.converter, image_list,\ |
| 341 | + ignore_char, decoder, beamWidth, batch_size, contrast_ths, adjust_contrast, filter_ths,\ |
| 342 | + workers, self.device) |
| 343 | + result += result0 |
| 344 | + # default mode will try to process multiple boxes at the same time |
| 345 | + else: |
| 346 | + image_list, max_width = get_image_list(horizontal_list, free_list, img_cv_grey, model_height = imgH) |
| 347 | + image_len = len(image_list) |
| 348 | + if rotation_info and image_list: |
| 349 | + image_list = make_rotated_img_list(rotation_info, image_list) |
| 350 | + max_width = max(max_width, imgH) |
| 351 | + |
| 352 | + result = get_text_prob(self.character, imgH, int(max_width), self.recognizer, self.converter, image_list,\ |
| 353 | + ignore_char, decoder, beamWidth, batch_size, contrast_ths, adjust_contrast, filter_ths,\ |
| 354 | + workers, self.device) |
| 355 | + |
| 356 | + if rotation_info and (horizontal_list+free_list): |
| 357 | + # Reshape result to be a list of lists, each row being for |
| 358 | + # one of the rotations (first row being no rotation) |
| 359 | + result = set_result_with_confidence( |
| 360 | + [result[image_len*i:image_len*(i+1)] for i in range(len(rotation_info) + 1)]) |
| 361 | + |
| 362 | + if self.model_lang == 'arabic': |
| 363 | + direction_mode = 'rtl' |
| 364 | + result = [list(item) for item in result] |
| 365 | + for item in result: |
| 366 | + item[1] = get_display(item[1]) |
| 367 | + else: |
| 368 | + direction_mode = 'ltr' |
| 369 | + |
| 370 | + if paragraph: |
| 371 | + result = get_paragraph(result, x_ths=x_ths, y_ths=y_ths, mode = direction_mode) |
| 372 | + |
| 373 | + if detail == 0: |
| 374 | + return [item[1] for item in result] |
| 375 | + elif output_format == 'dict': |
| 376 | + return [ {'boxes':item[0],'text':item[1],'confident':item[2]} for item in result] |
| 377 | + else: |
| 378 | + return result |
| 379 | + |
301 | 380 | def recognize(self, img_cv_grey, horizontal_list=None, free_list=None,\ |
302 | 381 | decoder = 'greedy', beamWidth= 5, batch_size = 1,\ |
303 | 382 | workers = 0, allowlist = None, blocklist = None, detail = 1,\ |
@@ -406,6 +485,36 @@ def readtext(self, image, decoder = 'greedy', beamWidth= 5, batch_size = 1,\ |
406 | 485 | filter_ths, y_ths, x_ths, False, output_format) |
407 | 486 |
|
408 | 487 | return result |
| 488 | + |
| 489 | + def readtext_prob(self, image, decoder = 'greedy', beamWidth= 5, batch_size = 1,\ |
| 490 | + workers = 0, allowlist = None, blocklist = None, detail = 1,\ |
| 491 | + rotation_info = None, paragraph = False, min_size = 20,\ |
| 492 | + contrast_ths = 0.1,adjust_contrast = 0.5, filter_ths = 0.003,\ |
| 493 | + text_threshold = 0.7, low_text = 0.4, link_threshold = 0.4,\ |
| 494 | + canvas_size = 2560, mag_ratio = 1.,\ |
| 495 | + slope_ths = 0.1, ycenter_ths = 0.5, height_ths = 0.5,\ |
| 496 | + width_ths = 0.5, y_ths = 0.5, x_ths = 1.0, add_margin = 0.1, output_format='standard'): |
| 497 | + ''' |
| 498 | + Parameters: |
| 499 | + image: file path or numpy-array or a byte stream object |
| 500 | + ''' |
| 501 | + img, img_cv_grey = reformat_input(image) |
| 502 | + |
| 503 | + horizontal_list, free_list = self.detect(img, min_size, text_threshold,\ |
| 504 | + low_text, link_threshold,\ |
| 505 | + canvas_size, mag_ratio,\ |
| 506 | + slope_ths, ycenter_ths,\ |
| 507 | + height_ths,width_ths,\ |
| 508 | + add_margin, False) |
| 509 | + # get the 1st result from hor & free list as self.detect returns a list of depth 3 |
| 510 | + horizontal_list, free_list = horizontal_list[0], free_list[0] |
| 511 | + result = self.recognize_prob(img_cv_grey, horizontal_list, free_list,\ |
| 512 | + decoder, beamWidth, batch_size,\ |
| 513 | + workers, allowlist, blocklist, detail, rotation_info,\ |
| 514 | + paragraph, contrast_ths, adjust_contrast,\ |
| 515 | + filter_ths, y_ths, x_ths, False, output_format) |
| 516 | + |
| 517 | + return result |
409 | 518 |
|
410 | 519 | def readtextlang(self, image, decoder = 'greedy', beamWidth= 5, batch_size = 1,\ |
411 | 520 | workers = 0, allowlist = None, blocklist = None, detail = 1,\ |
|
0 commit comments