单字坐标对应的是处理后的图片吗?
Browse files- draw_box.py +54 -0
draw_box.py
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import json
|
| 3 |
+
import glob
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import cv2
|
| 6 |
+
import numpy as np
|
| 7 |
+
|
| 8 |
+
def draw_box(pth_webp, pth_boxs):
|
| 9 |
+
|
| 10 |
+
with open(pth_boxs, encoding='utf-8') as fp:
|
| 11 |
+
boxs = json.load(fp)
|
| 12 |
+
word_boxes = boxs['text_word_boxes']
|
| 13 |
+
word_boxes = word_boxes[::-1] # 逆序
|
| 14 |
+
text_word = boxs['text_word']
|
| 15 |
+
text_word = text_word[::-1]
|
| 16 |
+
rec_texts = boxs['rec_texts']
|
| 17 |
+
rec_texts = rec_texts[::-1]
|
| 18 |
+
fp.close()
|
| 19 |
+
|
| 20 |
+
imgData = np.fromfile(pth_webp, dtype=np.uint8)
|
| 21 |
+
img = cv2.imdecode(imgData, cv2.IMREAD_UNCHANGED)
|
| 22 |
+
|
| 23 |
+
if len(img.shape) != 3: # 转彩图
|
| 24 |
+
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
| 25 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
|
| 26 |
+
else:
|
| 27 |
+
img_color = img.copy()
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
for arr_word, arr_box in zip( text_word, word_boxes):
|
| 31 |
+
for word, box in zip(arr_word, arr_box):
|
| 32 |
+
lu = [box[0], box[1]]
|
| 33 |
+
rd = [box[2], box[3]]
|
| 34 |
+
ld = [box[0], box[3]]
|
| 35 |
+
ru = [box[2], box[1]]
|
| 36 |
+
points = np.array([lu, ru, rd, ld])
|
| 37 |
+
|
| 38 |
+
cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全
|
| 39 |
+
100, 0, 255), thickness=2) # 只画线,不填充
|
| 40 |
+
|
| 41 |
+
cv2.imshow("box", img_color)
|
| 42 |
+
cv2.waitKey(0)
|
| 43 |
+
|
| 44 |
+
path_jpg = pth_webp.replace(".webp", ".jpg")
|
| 45 |
+
cv2.imwrite(path_jpg, img_color)
|
| 46 |
+
|
| 47 |
+
def do_drawbox():
|
| 48 |
+
|
| 49 |
+
pth_box = 'output/SWX0005_00000_00001_res.json'
|
| 50 |
+
pth_webp = 'data/SWX0005_00000_00001.webp'
|
| 51 |
+
draw_box(pth_webp, pth_box)
|
| 52 |
+
|
| 53 |
+
if __name__ == "__main__":
|
| 54 |
+
do_drawbox()
|