SWHL commited on
Commit
15f636b
1 Parent(s): 8d6e841

Update files

Browse files
Files changed (3) hide show
  1. images/japan_2.jpg +0 -0
  2. images/korean_1.jpg +0 -0
  3. utils.py +85 -0
images/japan_2.jpg ADDED
images/korean_1.jpg ADDED
utils.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- encoding: utf-8 -*-
2
+ # @Author: SWHL
3
+ # @Contact: liekkaskono@163.com
4
+ import math
5
+ import random
6
+ import time
7
+ from pathlib import Path
8
+
9
+ import cv2
10
+ import numpy as np
11
+ from PIL import Image, ImageDraw, ImageFont
12
+
13
+
14
+ def draw_ocr_box_txt(image, boxes, txts, font_path,
15
+ scores=None, text_score=0.5):
16
+ h, w = image.height, image.width
17
+ img_left = image.copy()
18
+ img_right = Image.new('RGB', (w, h), (255, 255, 255))
19
+
20
+ random.seed(0)
21
+ draw_left = ImageDraw.Draw(img_left)
22
+ draw_right = ImageDraw.Draw(img_right)
23
+ for idx, (box, txt) in enumerate(zip(boxes, txts)):
24
+ if scores is not None and float(scores[idx]) < text_score:
25
+ continue
26
+
27
+ color = (random.randint(0, 255),
28
+ random.randint(0, 255),
29
+ random.randint(0, 255))
30
+
31
+ box = [tuple(v) for v in box]
32
+ draw_left.polygon(box, fill=color)
33
+ draw_right.polygon([box[0][0], box[0][1],
34
+ box[1][0], box[1][1],
35
+ box[2][0], box[2][1],
36
+ box[3][0], box[3][1]],
37
+ outline=color)
38
+
39
+ box_height = math.sqrt((box[0][0] - box[3][0])**2
40
+ + (box[0][1] - box[3][1])**2)
41
+
42
+ box_width = math.sqrt((box[0][0] - box[1][0])**2
43
+ + (box[0][1] - box[1][1])**2)
44
+
45
+ if box_height > 2 * box_width:
46
+ font_size = max(int(box_width * 0.9), 10)
47
+ font = ImageFont.truetype(font_path, font_size,
48
+ encoding="utf-8")
49
+ cur_y = box[0][1]
50
+ for c in txt:
51
+ char_size = font.getsize(c)
52
+ draw_right.text((box[0][0] + 3, cur_y), c,
53
+ fill=(0, 0, 0), font=font)
54
+ cur_y += char_size[1]
55
+ else:
56
+ font_size = max(int(box_height * 0.8), 10)
57
+ font = ImageFont.truetype(font_path, font_size, encoding="utf-8")
58
+ draw_right.text([box[0][0], box[0][1]], txt,
59
+ fill=(0, 0, 0), font=font)
60
+
61
+ img_left = Image.blend(image, img_left, 0.5)
62
+ img_show = Image.new('RGB', (w * 2, h), (255, 255, 255))
63
+ img_show.paste(img_left, (0, 0, w, h))
64
+ img_show.paste(img_right, (w, 0, w * 2, h))
65
+ return np.array(img_show)
66
+
67
+
68
+ def visualize(image_path, boxes, txts, scores,
69
+ font_path="./fonts/FZYTK.TTF"):
70
+ image = Image.open(image_path)
71
+
72
+ draw_img = draw_ocr_box_txt(image, boxes,
73
+ txts, font_path,
74
+ scores,
75
+ text_score=0.5)
76
+
77
+ draw_img_save = Path("./inference_results/")
78
+ if not draw_img_save.exists():
79
+ draw_img_save.mkdir(parents=True, exist_ok=True)
80
+
81
+ time_stamp = time.strftime(
82
+ '%Y-%m-%d-%H-%M-%S', time.localtime(time.time()))
83
+ image_save = str(draw_img_save / f'{time_stamp}_{Path(image_path).name}')
84
+ cv2.imwrite(image_save, draw_img[:, :, ::-1])
85
+ return image_save