classifier.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import cv2
  2. import math
  3. import numpy as np
  4. from .utils import *
  5. from copy import deepcopy
  6. from hmOCR.argument import Args
  7. class Classifier:
  8. def __init__(self, args: "Args"):
  9. self.cls_image_shape = [int(v) for v in args.cls_image_shape.split(",")]
  10. self.cls_batch_num = args.cls_batch_num
  11. self.cls_thresh = args.cls_thresh
  12. postprocess_params = {
  13. "name": "ClsPostProcess",
  14. "label_list": args.cls_label_list
  15. }
  16. self.postprocess_op = build_post_process(postprocess_params)
  17. self.predictor, self.input_tensor, self.output_tensors = create_predictor(args, "cls")
  18. def resize_norm_img(self, img):
  19. imgC, imgH, imgW = self.cls_image_shape
  20. h = img.shape[0]
  21. w = img.shape[1]
  22. ratio = w / float(h)
  23. if math.ceil(imgH * ratio) > imgW:
  24. resized_w = imgW
  25. else:
  26. resized_w = int(math.ceil(imgH * ratio))
  27. resized_image = cv2.resize(img, (resized_w, imgH)) # noqa
  28. resized_image = resized_image.astype("float32")
  29. if self.cls_image_shape[0] == 1:
  30. resized_image = resized_image / 255
  31. resized_image = resized_image[np.newaxis, :]
  32. else:
  33. resized_image = resized_image.transpose((2, 0, 1)) / 255
  34. resized_image -= 0.5
  35. resized_image /= 0.5
  36. padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
  37. padding_im[:, :, 0:resized_w] = resized_image
  38. return padding_im
  39. def __call__(self, img_list):
  40. img_list = deepcopy(img_list)
  41. img_num = len(img_list)
  42. # Calculate the aspect ratio of all text bars
  43. width_list = []
  44. for img in img_list:
  45. width_list.append(img.shape[1] / float(img.shape[0]))
  46. # Sorting can speed up the cls process
  47. indices = np.argsort(np.array(width_list))
  48. batch_num = self.cls_batch_num
  49. for beg_img_no in range(0, img_num, batch_num):
  50. end_img_no = min(img_num, beg_img_no + batch_num)
  51. norm_img_batch = []
  52. max_wh_ratio = 0
  53. for ino in range(beg_img_no, end_img_no):
  54. h, w = img_list[indices[ino]].shape[0:2]
  55. wh_ratio = w * 1.0 / h
  56. max_wh_ratio = max(max_wh_ratio, wh_ratio)
  57. for ino in range(beg_img_no, end_img_no):
  58. norm_img = self.resize_norm_img(img_list[indices[ino]])
  59. norm_img = norm_img[np.newaxis, :]
  60. norm_img_batch.append(norm_img)
  61. norm_img_batch = np.concatenate(norm_img_batch)
  62. norm_img_batch = norm_img_batch.copy()
  63. self.input_tensor.copy_from_cpu(norm_img_batch)
  64. self.predictor.run()
  65. prob_out = self.output_tensors[0].copy_to_cpu()
  66. self.predictor.try_shrink_memory()
  67. cls_result = self.postprocess_op(prob_out)
  68. for rno in range(len(cls_result)):
  69. label, score = cls_result[rno]
  70. if "180" in label and score > self.cls_thresh:
  71. img_list[indices[beg_img_no + rno]] = cv2.rotate(img_list[indices[beg_img_no + rno]], 1) # noqa
  72. return img_list