vis.py 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # -*- coding: utf-8 -*-
  2. # @Time : 2020/6/18 10:34
  3. # @Author : zhoujun
  4. import math
  5. import numpy as np
  6. from PIL import Image, ImageDraw, ImageFont
  7. import os
  8. def get_font_file():
  9. searchs = ["./doc/田氏颜体大字库2.0.ttf", "../doc/田氏颜体大字库2.0.ttf"]
  10. for path in searchs:
  11. if os.path.exists(path):
  12. return path
  13. assert False,"can't find 田氏颜体大字库2.0.ttf"
  14. def draw_ocr_box_txt(image, boxes, txts = None, pos="horizontal"):
  15. if isinstance(image,np.ndarray):
  16. image = Image.fromarray(image)
  17. h, w = image.height, image.width
  18. img_left = image.copy()
  19. img_right = Image.new('RGB', (w, h), (255, 255, 255))
  20. import random
  21. # 每次使用相同的随机种子 ,可以保证两次颜色一致
  22. random.seed(0)
  23. draw_left = ImageDraw.Draw(img_left)
  24. draw_right = ImageDraw.Draw(img_right)
  25. for i,box in enumerate(boxes):
  26. color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
  27. draw_left.polygon(box, fill=color)
  28. draw_right.polygon([box[0][0], box[0][1],
  29. box[1][0], box[1][1],
  30. box[2][0], box[2][1],
  31. box[3][0], box[3][1]], outline=color)
  32. if txts is not None:
  33. txt = str(txts[i])
  34. box_height = math.sqrt((box[0][0] - box[3][0]) ** 2 + (box[0][1] - box[3][1]) ** 2)
  35. box_width = math.sqrt((box[0][0] - box[1][0]) ** 2 + (box[0][1] - box[1][1]) ** 2)
  36. if box_height > 2 * box_width:
  37. font_size = max(int(box_width * 0.9), 10)
  38. font = ImageFont.truetype(get_font_file(), font_size, encoding="utf-8")
  39. cur_y = box[0][1]
  40. for c in txt:
  41. char_size = font.getsize(c)
  42. draw_right.text((box[0][0] + 3, cur_y), c, fill=(0, 0, 0), font=font)
  43. cur_y += char_size[1]
  44. else:
  45. font_size = max(int(box_height * 0.8), 10)
  46. font = ImageFont.truetype(get_font_file(), font_size, encoding="utf-8")
  47. draw_right.text([box[0][0], box[0][1]], txt, fill=(0, 0, 0), font=font)
  48. img_left = Image.blend(image, img_left, 0.5)
  49. if txts is not None:
  50. if pos == "horizontal":
  51. img_show = Image.new('RGB', (w * 2, h), (255, 255, 255))
  52. img_show.paste(img_left, (0, 0, w, h))
  53. img_show.paste(img_right, (w, 0, w * 2, h))
  54. else:
  55. img_show = Image.new('RGB', (w, h * 2), (255, 255, 255))
  56. img_show.paste(img_left, (0, 0, w, h))
  57. img_show.paste(img_right, (0, h, w , h * 2))
  58. else:
  59. img_show = np.array(img_left)
  60. return np.array(img_show)
  61. def show_img(imgs: np.ndarray, title='img'):
  62. from matplotlib import pyplot as plt
  63. color = (len(imgs.shape) == 3 and imgs.shape[-1] == 3)
  64. imgs = np.expand_dims(imgs, axis=0)
  65. for i, img in enumerate(imgs):
  66. plt.figure()
  67. plt.title('{}_{}'.format(title, i))
  68. plt.imshow(img, cmap=None if color else 'gray')
  69. def draw_bbox(img_path, result, color=(255, 0, 0), thickness=2):
  70. import cv2
  71. if isinstance(img_path, str):
  72. img_path = cv2.imread(img_path)
  73. # img_path = cv2.cvtColor(img_path, cv2.COLOR_BGR2RGB)
  74. img_path = img_path.copy()
  75. for point in result:
  76. point = point.astype(int)
  77. cv2.polylines(img_path, [point], True, color, thickness)
  78. return img_path