DBNet / aliocr_IC15_convert.py
fasdfsa's picture
done
44989e3
# pip install numpy==1.26.4 opencv-python==4.6.0.66
# see doc\lang\programming\pytorch\文本检测\DBNET 论文代码都有
"""
给 DBNet 官方代码用
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
"""
"""
icdar2015 文本检测数据集
标注格式: x1,y1,x2,y2,x3,y3,x4,y4,text
其中, x1,y1为左上角坐标,x2,y2为右上角坐标,x3,y3为右下角坐标,x4,y4为左下角坐标。
### 表示text难以辨认。
"""
import random
from pathlib import Path
import os
import glob
import base64
from importlib.resources import path
import math
import numpy as np
import cv2
import json
import decimal
import datetime
from pickletools import uint8
class DecimalEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, decimal.Decimal):
return float(o)
elif isinstance(o, datetime.datetime):
return str(o)
super(DecimalEncoder, self).default(o)
def save_json(filename, dics):
with open(filename, 'w', encoding='utf-8') as fp:
json.dump(dics, fp, indent=4, cls=DecimalEncoder, ensure_ascii=False)
fp.close()
def load_json(filename):
with open(filename, encoding='utf-8') as fp:
js = json.load(fp)
fp.close()
return js
# convert string to json
def parse(s):
return json.loads(s, strict=False)
# convert dict to string
def string(d):
return json.dumps(d, cls=DecimalEncoder, ensure_ascii=False)
def transform(points, M):
# points 算出四个点变换后移动到哪里了
# points = np.array([[word_x, word_y], # 左上
# [word_x + word_width, word_y], # 右上
# [word_x + word_width, word_y + word_height], # 右下
# [word_x, word_y + word_height], # 左下
# ])
# add ones
ones = np.ones(shape=(len(points), 1))
points_ones = np.hstack([points, ones])
# transform points
transformed_points = M.dot(points_ones.T).T
transformed_points_int = np.round(
transformed_points, decimals=0).astype(np.int32) # 批量四舍五入
return transformed_points_int
def cutPoly(img, pts):
# img = cv2.imdecode(np.fromfile('./t.png', dtype=np.uint8), -1)
# pts = np.array([[10,150],[150,100],[300,150],[350,100],[310,20],[35,10]])
## (1) Crop the bounding rect
rect = cv2.boundingRect(pts)
x,y,w,h = rect
croped = img[y:y+h, x:x+w].copy()
## (2) make mask
pts = pts - pts.min(axis=0)
mask = np.zeros(croped.shape[:2], np.uint8)
cv2.drawContours(mask, [pts], -1, (255, 255, 255), -1, cv2.LINE_AA)
## (3) do bit-op
dst = cv2.bitwise_and(croped, croped, mask=mask)
## (4) add the white background
bg = np.ones_like(croped, np.uint8)*255
cv2.bitwise_not(bg,bg, mask=mask)
dst2 = bg+ dst
# cv2.imwrite("croped.png", croped)
# cv2.imwrite("mask.png", mask)
# cv2.imwrite("dst.png", dst)
# cv2.imwrite("dst2.png", dst2)
return dst2
if __name__ == "__main__":
# 验证原版的文本标记框
# im = './datasets/icdar2015/train_images/img_1.jpg'
# gt = './datasets/icdar2015/train_gts/gt_img_1.txt'
# 验证自已生成的标记框
im = './icdar2015_aliocr/train_images/img_000001.jpg'
gt = './icdar2015_aliocr/train_gts/gt_img_000001.txt'
if os.path.exists(gt):
items = []
reader = open(gt, 'r', encoding='utf-8-sig').readlines()
for line in reader:
item = {}
parts = line.strip().split(',')
label = parts[-1]
if 'TD' in gt and label == '1':
label = '###'
line = [i.strip('\ufeff').strip('\xef\xbb\xbf') for i in parts]
if 'icdar' in gt:
poly = np.array(list(map(float, line[:8]))).reshape(
(-1, 2)).tolist()
else:
num_points = math.floor((len(line) - 1) / 2) * 2
poly = np.array(list(map(float, line[:num_points]))).reshape(
(-1, 2)).tolist()
item['poly'] = poly
item['text'] = label
# 多边形是用一个个的点表示的,起点连接第二个点,第二个连接第三个 ... 最后一点连接起点,构成一个闭合的区域
item['points'] = poly
# 此标记表示文字模糊不可辨认,文本框的标记是不可靠的
item['ignore'] = True if label == '###' else False
items.append(item)
img = cv2.imdecode(np.fromfile(im, dtype=np.uint8), -1)
# DBNet 原版代码只能处理彩图,所以统一处理成彩图
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
for i in range(len(items)):
poly = items[i]['poly']
poly = np.array(poly)
poly = poly.astype(np.int32)
# cv2.fillPoly(img, pts=[ poly ], color=(0, 0, 255))
b = random.randint(0, 255) # 用来生成[a,b]之间的随意整数,包括两个边界值。
g = random.randint(0, 255)
r = random.randint(0, 255)
# 只画线,不填充 # 就是画线,从起点连到第二个点 ... 最后一个点连到第一个点
cv2.polylines(img, [poly], isClosed=True,
color=(b, g, r), thickness=1)
cv2.imwrite("poly.jpg", img)
# cv2.imshow("poly", img)
# cv2.waitKey()
# 开始转换
out_dir = 'icdar2015_aliocr'
if os.path.exists(out_dir):
import shutil
shutil.rmtree(out_dir)
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
dir_json = './data/json' # '/yingedu/www/ocr_server/data/json'
dir_img = './data/img' # '/yingedu/www/ocr_server/data/img'
train_list = []
train_list_path = os.path.join(out_dir, 'train_list.txt')
test_list = []
test_list_path = os.path.join(out_dir, 'test_list.txt')
g_count = 1
json_paths = glob.glob('{}/*.json'.format(dir_json), recursive=True)
for json_path in json_paths:
base = Path(json_path).stem
img_train_path = os.path.join(dir_img, '{}.txt'.format(base))
if not os.path.exists(img_train_path): # 没有相应的图片,可能被删除了
continue
jsn = load_json(json_path)
with open(img_train_path, "r", encoding="utf-8") as fp:
imgdata = fp.read()
imgdata = base64.b64decode(imgdata)
imgdata = np.frombuffer(imgdata, np.uint8)
img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
# cv2.imshow('img', img)
# cv2.waitKey(0)
if len(img.shape) != 3: # 转彩图
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
img = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR) # DBNet 原版只能处理彩图,这里转一下
else:
img_color = img.copy()
img_color_origin = img_color.copy()
img_color_origin2 = img_color.copy()
img_name = "img_{:06d}.jpg".format(g_count)
gt_name = "gt_img_{:06d}.txt".format(g_count)
is_train_img = random.choices([0, 1], weights=[0.15, 0.85])[0]
# 85% 的概率是训练图
gt_txt_list = []
img_train_path = os.path.join(out_dir, 'train_images', img_name)
img_train_gt_path = os.path.join(out_dir, 'train_gts', gt_name)
img_test_path = os.path.join(out_dir, 'test_images', img_name)
img_test_gt_path = os.path.join(out_dir, 'test_gts', gt_name)
dir1 = os.path.dirname(img_train_path)
dir2 = os.path.dirname(img_train_gt_path)
dir3 = os.path.dirname(img_test_path)
dir4 = os.path.dirname(img_test_gt_path)
if not os.path.exists(dir1):
os.makedirs(dir1)
if not os.path.exists(dir2):
os.makedirs(dir2)
if not os.path.exists(dir3):
os.makedirs(dir3)
if not os.path.exists(dir4):
os.makedirs(dir4)
if is_train_img:
train_list.append(img_name)
cv2.imwrite(img_train_path, img)
else:
test_list.append(img_name)
cv2.imwrite(img_test_path, img)
wordsInfo = jsn['prism_wordsInfo']
for j in range(len(wordsInfo)):
jo = wordsInfo[j]
word = jo["word"]
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
angle = jo['angle']
img_color = img_color_origin.copy()
"""
x y 宽高全部不靠谱, pos 里是对的
"""
# word_x = jo['x']
# word_y = jo['y']
# word_width = jo['width']
# word_height = jo['height']
# if abs(angle) == 90 or abs(angle) == 270:
# word_width = jo['height']
# word_height = jo['width']
# elif angle != 0:
# # 变换前画出绿框,方便追踪点的前后变化
# img_color = cv2.rectangle(img_color, (word_x, word_y), (word_x + word_width, word_y + word_height), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
# cv2.imshow("green", img_color)
# cv2.waitKey(0)
# # 变换前的多边形蓝框
# points = np.array([
# [word_x, word_y], # 左上
# [word_x + word_width, word_y], # 右上
# [word_x + word_width, word_y + word_height], # 右下
# [word_x, word_y + word_height], # 左下
# ])
# # cv2.fillPoly(img_color, pts=[points], color=(255, 0, 0)) # 填充
# cv2.polylines(img_color, [points], isClosed=True, color=(
# 255, 0, 0), thickness=1) # 只画线,不填充
# cv2.imshow("polys", img_color)
# cv2.waitKey(0)
# # 获取图像的维度,并计算中心
# (h, w) = img_color.shape[:2]
# (cX, cY) = (w // 2, h // 2)
# # - (cX,cY): 旋转的中心点坐标
# # - 180: 旋转的度数,正度数表示逆时针旋转,而负度数表示顺时针旋转。
# # - 1.0:旋转后图像的大小,1.0原图,2.0变成原来的2倍,0.5变成原来的0.5倍
# # 1° = π/180弧度 1 弧度 = 180 / 3.1415926 // 0.0190033 是Mathematica 算出来的弧度,先转换成角度 // -0.0190033 * (180 / 3.1415926)
# M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
# img_color = cv2.warpAffine(img_color, M, (w, h))
# img_color_transform = img_color.copy()
# cv2.imshow("after trans", img_color)
# cv2.waitKey(0)
# # https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html # 原理
# # https://stackoverflow.com/questions/30327659/how-can-i-remap-a-point-after-an-image-rotation # How can I remap a point after an image rotation?
# # 如何得到移动后的坐标点
# # points 算出四个点变换后移动到哪里了
# points = np.array([[word_x, word_y], # 左上
# # 右上
# [word_x + word_width, word_y],
# [word_x + word_width, word_y + \
# word_height], # 右下
# [word_x, word_y + word_height], # 左下
# ])
# # add ones
# ones = np.ones(shape=(len(points), 1))
# points_ones = np.hstack([points, ones])
# # transform points
# transformed_points = M.dot(points_ones.T).T
# transformed_points_int = np.round(
# transformed_points, decimals=0).astype(np.int32) # 批量四舍五入
# cv2.polylines(img_color, [transformed_points_int], isClosed=True, color=(
# 0, 0, 255), thickness=2) # 画转换后的点
# cv2.polylines(img_color_origin, [points], isClosed=True, color=(
# random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
# cv2.imshow("orgin", img_color_origin)
# cv2.waitKey(0)
# 四个角的位置 # 左上、右上、右下、左下,当NeedRotate为true时,如果最外层的angle不为0,需要按照angle矫正图片后,坐标才准确
pos = jo["pos"]
x = int(pos[0]["x"]) # 左上
y = int(pos[0]["y"])
x2 = int(pos[2]["x"]) # 右下
y2 = int(pos[2]["y"])
lu = [pos[0]['x'], pos[0]['y']] # left up 四个角顺时针方向数
ru = [pos[1]['x'], pos[1]['y']]
rd = [pos[2]['x'], pos[2]['y']]
ld = [pos[3]['x'], pos[3]['y']]
# 生成 icdar2015 格式的人工标记训练数据(用于训练官方DB)
gt_txt_list.append( "{},{},{},{},{},{},{},{},{}".format(lu[0], lu[1], ru[0], ru[1], rd[0], rd[1], ld[0], ld[1], word) )
# 绘制矩形
start_point = (x, y) # 矩形的左上角
end_point = (x2, y2) # 矩形的右下角
color = (0, 0, 255) # BGR
thickness = 2
# 逐行画框
img_color = cv2.rectangle(img_color, start_point, end_point, color, thickness)
# cv2.imshow("box", img_color)
# cv2.waitKey(0)
gt_txt = "\n".join(gt_txt_list)
if is_train_img:
with open(img_train_gt_path, 'w', encoding='utf-8') as f:
f.write(gt_txt)
else:
with open(img_test_gt_path, 'w', encoding='utf-8') as f:
f.write(gt_txt)
print(f'### one task one. {g_count} / {len(json_paths)}')
g_count += 1
# points = [ lu, ru, rd, ld ]
# points0 = np.array([[word_x, word_y], # 左上
# # 右上
# [word_x + word_width, word_y],
# [word_x + word_width, word_y + \
# word_height], # 右下
# [word_x, word_y + word_height], # 左下
# ])
# points1 = np.array( [ lu, ru, rd, ld ] )
# if not (abs(angle) == 90 or abs(angle) == 270) and angle != 0:
# points = transform( points, M )
# else:
# points = np.array(points)
# ps3 = np.array(
# [
# [min( points[0][0], points1[0][0] ), min( points[0][1], points1[0][1] )], # 左上(取最两者中最小的)
# [max( points[1][0], points1[1][0] ), min( points[1][1], points1[1][1] )], # 右上
# [max( points[2][0], points1[2][0] ), max( points[2][1], points1[2][1] )], # 右下
# [min( points[3][0], points1[3][0] ), max( points[3][1], points1[3][1] )] # 左下
# ]
# )
# img_cuted = cutPoly(img, ps3)
# cv2.imwrite(f'./tmp/{g_count}.jpg', img_cuted)
# with open(f'./tmp/{g_count}.txt', 'w', encoding='utf-8') as f:
# f.write(word)
# g_count += 1
# cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全
# 100, 0, 255), thickness=2) # 只画线,不填充
# cv2.polylines(img_color_origin, [ points1 ], isClosed=True, color=(
# random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
# cv2.imshow("orgin", img_color_origin)
# cv2.waitKey(0)
# # cv2.imshow("box", img_color)
# # cv2.waitKey(0)
# # img_color = cv2.rectangle(img_color, points[0], points[2], color, thickness) # 正常矩形,框不完全
# # cv2.imshow("box", img_color)
# # cv2.waitKey(0)
# if not (abs(angle) == 90 or abs(angle) == 270) and angle != 0:
# t = word
# ps = np.array(
# [
# [min( transformed_points_int[0][0], points[0][0] ), min( transformed_points_int[0][1], points[0][1] )], # 左上(取最两者中最小的)
# [max( transformed_points_int[1][0], points[1][0] ), min( transformed_points_int[1][1], points[1][1] )], # 右上
# [max( transformed_points_int[2][0], points[2][0] ), max( transformed_points_int[2][1], points[2][1] )], # 右下
# [min( transformed_points_int[3][0], points[3][0] ), max( transformed_points_int[3][1], points[3][1] )] # 左下
# ]
# )
# ps2 = np.array(
# [
# [min( points0[0][0], points1[0][0] ), min( points0[0][1], points1[0][1] )], # 左上(取最两者中最小的)
# [max( points0[1][0], points1[1][0] ), min( points0[1][1], points1[1][1] )], # 右上
# [max( points0[2][0], points1[2][0] ), max( points0[2][1], points1[2][1] )], # 右下
# [min( points0[3][0], points1[3][0] ), max( points0[3][1], points1[3][1] )] # 左下
# ]
# )
# # img_cuted = cutPoly(img_color_transform, ps)
# # cv2.imwrite(f'./tmp/{g_count}.jpg', img_cuted)
# # with open(f'./tmp/{g_count}.txt', 'w', encoding='utf-8') as f:
# # f.write(word)
# # g_count += 1
# cv2.polylines(img_color, [ ps ], isClosed=True, color=(
# 255, 0, 0), thickness=2) # 只画线,不填充
# cv2.polylines(img_color_origin, [ ps2 ], isClosed=True, color=(
# random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 只画线,不填充
# cv2.imshow("orgin", img_color_origin)
# cv2.waitKey(0)
# img_cuted = cutPoly(img, ps2)
# cv2.imwrite(f'./tmp/{g_count}.jpg', img_cuted)
# with open(f'./tmp/{g_count}.txt', 'w', encoding='utf-8') as f:
# f.write(word)
# g_count += 1
# # cv2.imshow("box", img_color)
# # cv2.waitKey(0)
# lastx_mini = 0 # 下一个字符x 坐标的下界(肯定不小于这个值)
# prew = 0 # 上一个字符的宽度
# words = ""
# charInfo = jo["charInfo"]
# min_cx = 9999 # 最小左上角
# min_cy = 9999
# max_cxcw = -1 # 最大右下角
# max_cych = -1
# for i in range(len(charInfo)):
# joc = charInfo[i]
# c = joc["word"]
# cx = int(joc["x"])
# cy = int(joc["y"])
# cw = int(joc["w"])
# ch = int(joc["h"])
# if cx < min_cx:
# min_cx = cx
# if cy < min_cy:
# min_cy = cy
# if cx + cw > max_cxcw:
# max_cxcw = cx + cw
# if cy + ch > max_cych:
# max_cych = cy + ch
# # 绘制矩形
# start_point = (cx, cy) # 矩形的左上角
# end_point = (cx + cw, cy + ch) # 矩形的右下角
# color = (0, 0, 255) # BGR
# thickness = 2
# # 逐字画框
# # img_color = cv2.rectangle(
# # img_color, start_point, end_point, color, thickness)
# # cv2.imshow("box", img_color)
# # cv2.waitKey(0)
# # 这个框更准一些
# # img_color = cv2.rectangle(
# # img_color, (min_cx, min_cy), (max_cxcw, max_cych), (0, 255, 0), thickness)
# # cv2.imshow("box", img_color)
# # cv2.waitKey(0)
# # fix me: 如果上面的行框的左边要比这里更左,那就以行框的左边为准
# # 因为发现单个字的框会有漏字的现想
# gt_txt_list.append("{},{},{},{},{},{},{},{},{}".format(
# min_cx, min_cy, max_cxcw, min_cy, max_cxcw, max_cych, min_cx, max_cych, word))
# gt_txt = '\n'.join(gt_txt_list)
# with open(img_gt_path, "w", encoding='utf-8-sig') as fp:
# fp.write(gt_txt)
train_list_txt = "\n".join(train_list)
test_list_txt = "\n".join(test_list)
with open(os.path.join(out_dir, "train_list.txt"), 'w', encoding='utf-8') as f:
f.write(train_list_txt)
with open(os.path.join(out_dir, "test_list.txt"), 'w', encoding='utf-8') as f:
f.write(test_list_txt)
print('### all task done.')