not done yet.
Browse files- .gitignore +1 -0
- aliocr_IC15_convert.py +51 -33
.gitignore
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
icdar2015_aliocr/
|
aliocr_IC15_convert.py
CHANGED
|
@@ -4,8 +4,7 @@
|
|
| 4 |
|
| 5 |
"""
|
| 6 |
|
| 7 |
-
给
|
| 8 |
-
|
| 9 |
|
| 10 |
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
|
| 11 |
|
|
@@ -133,8 +132,8 @@ def cutPoly(img, pts):
|
|
| 133 |
if __name__ == "__main__":
|
| 134 |
|
| 135 |
# 验证原版的文本标记框
|
| 136 |
-
im = './train_images/img_1.jpg'
|
| 137 |
-
gt = './train_gts/gt_img_1.txt'
|
| 138 |
|
| 139 |
# 验证自已生成的标记框
|
| 140 |
# im = './icdar2015_aliocr/imgs/training/img_1.jpg'
|
|
@@ -187,12 +186,14 @@ if __name__ == "__main__":
|
|
| 187 |
|
| 188 |
#cv2.imwrite("poly.jpg", img)
|
| 189 |
|
| 190 |
-
|
| 191 |
-
|
| 192 |
|
| 193 |
# 开始转换
|
| 194 |
|
| 195 |
out_dir = 'icdar2015_aliocr'
|
|
|
|
|
|
|
| 196 |
|
| 197 |
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
|
| 198 |
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
|
@@ -201,7 +202,10 @@ if __name__ == "__main__":
|
|
| 201 |
dir_img = './data/img' # '/yingedu/www/ocr_server/data/img'
|
| 202 |
|
| 203 |
train_list = []
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
g_count = 1
|
| 207 |
|
|
@@ -224,8 +228,8 @@ if __name__ == "__main__":
|
|
| 224 |
imgdata = np.frombuffer(imgdata, np.uint8)
|
| 225 |
img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
|
| 226 |
|
| 227 |
-
|
| 228 |
-
|
| 229 |
|
| 230 |
if len(img.shape) != 3: # 转彩图
|
| 231 |
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
@@ -239,21 +243,36 @@ if __name__ == "__main__":
|
|
| 239 |
|
| 240 |
|
| 241 |
# 生成1000 张一模一样的图
|
| 242 |
-
for i in range(1, 1000+1
|
| 243 |
|
| 244 |
num_img = i
|
| 245 |
|
| 246 |
-
img_name = "img_{}.jpg".format(num_img)
|
| 247 |
-
gt_name = "gt_img_{}.txt".format(num_img)
|
|
|
|
|
|
|
|
|
|
| 248 |
|
| 249 |
gt_txt_list = []
|
| 250 |
|
| 251 |
-
|
|
|
|
|
|
|
|
|
|
| 252 |
# num_img += 1
|
| 253 |
|
| 254 |
-
img_path = os.path.join(out_dir, '
|
| 255 |
img_gt_path = os.path.join(
|
| 256 |
-
out_dir, '
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
cv2.imwrite(img_path, img)
|
| 259 |
|
|
@@ -277,11 +296,10 @@ if __name__ == "__main__":
|
|
| 277 |
elif angle != 0:
|
| 278 |
|
| 279 |
# 变换前画出绿框,方便追踪点的前后变化
|
| 280 |
-
|
| 281 |
-
# word_x + word_width, word_y + word_height), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
|
| 282 |
|
| 283 |
-
|
| 284 |
-
|
| 285 |
|
| 286 |
# 变换前的多边形蓝框
|
| 287 |
points = np.array([
|
|
@@ -291,12 +309,12 @@ if __name__ == "__main__":
|
|
| 291 |
[word_x, word_y + word_height], # 左下
|
| 292 |
])
|
| 293 |
|
| 294 |
-
#
|
| 295 |
-
|
| 296 |
-
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
| 300 |
|
| 301 |
# 获取图像的��度,并计算中心
|
| 302 |
(h, w) = img_color.shape[:2]
|
|
@@ -310,8 +328,8 @@ if __name__ == "__main__":
|
|
| 310 |
img_color = cv2.warpAffine(img_color, M, (w, h))
|
| 311 |
img_color_transform = img_color.copy()
|
| 312 |
|
| 313 |
-
|
| 314 |
-
|
| 315 |
|
| 316 |
# https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html # 原理
|
| 317 |
# 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?
|
|
@@ -345,8 +363,8 @@ if __name__ == "__main__":
|
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
-
|
| 349 |
-
|
| 350 |
|
| 351 |
|
| 352 |
|
|
@@ -364,8 +382,8 @@ if __name__ == "__main__":
|
|
| 364 |
rd = [pos[2]['x'], pos[2]['y']]
|
| 365 |
ld = [pos[3]['x'], pos[3]['y']]
|
| 366 |
|
| 367 |
-
# 生成 icdar2015
|
| 368 |
-
|
| 369 |
|
| 370 |
# 绘制矩形
|
| 371 |
start_point = (x, y) # 矩形的左上角
|
|
@@ -377,10 +395,10 @@ if __name__ == "__main__":
|
|
| 377 |
thickness = 2
|
| 378 |
|
| 379 |
# 逐行画框
|
| 380 |
-
|
| 381 |
-
|
| 382 |
|
| 383 |
-
|
| 384 |
|
| 385 |
points = [ lu, ru, rd, ld ]
|
| 386 |
|
|
|
|
| 4 |
|
| 5 |
"""
|
| 6 |
|
| 7 |
+
给 DBNet 官方代码用
|
|
|
|
| 8 |
|
| 9 |
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
|
| 10 |
|
|
|
|
| 132 |
if __name__ == "__main__":
|
| 133 |
|
| 134 |
# 验证原版的文本标记框
|
| 135 |
+
im = './datasets/icdar2015/train_images/img_1.jpg'
|
| 136 |
+
gt = './datasets/icdar2015/train_gts/gt_img_1.txt'
|
| 137 |
|
| 138 |
# 验证自已生成的标记框
|
| 139 |
# im = './icdar2015_aliocr/imgs/training/img_1.jpg'
|
|
|
|
| 186 |
|
| 187 |
#cv2.imwrite("poly.jpg", img)
|
| 188 |
|
| 189 |
+
cv2.imshow("poly", img)
|
| 190 |
+
cv2.waitKey()
|
| 191 |
|
| 192 |
# 开始转换
|
| 193 |
|
| 194 |
out_dir = 'icdar2015_aliocr'
|
| 195 |
+
# train_list.txt
|
| 196 |
+
# test_list.txt
|
| 197 |
|
| 198 |
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
|
| 199 |
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
|
|
|
| 202 |
dir_img = './data/img' # '/yingedu/www/ocr_server/data/img'
|
| 203 |
|
| 204 |
train_list = []
|
| 205 |
+
train_list_path = os.path.join(out_dir, 'train_list.txt')
|
| 206 |
+
|
| 207 |
+
test_list = []
|
| 208 |
+
test_list_path = os.path.join(out_dir, 'test_list.txt')
|
| 209 |
|
| 210 |
g_count = 1
|
| 211 |
|
|
|
|
| 228 |
imgdata = np.frombuffer(imgdata, np.uint8)
|
| 229 |
img = cv2.imdecode(imgdata, cv2.IMREAD_UNCHANGED)
|
| 230 |
|
| 231 |
+
cv2.imshow('img', img)
|
| 232 |
+
cv2.waitKey(0)
|
| 233 |
|
| 234 |
if len(img.shape) != 3: # 转彩图
|
| 235 |
img_color = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
|
|
|
|
| 243 |
|
| 244 |
|
| 245 |
# 生成1000 张一模一样的图
|
| 246 |
+
for i in range(1, 2): # 1000+1
|
| 247 |
|
| 248 |
num_img = i
|
| 249 |
|
| 250 |
+
img_name = "img_{:06d}.jpg".format(num_img)
|
| 251 |
+
gt_name = "gt_img_{:06d}.txt".format(num_img)
|
| 252 |
+
|
| 253 |
+
is_train_img = random.choices([0, 1], weights=[0.15, 0.85])[0]
|
| 254 |
+
# 85% 的概率是训练图
|
| 255 |
|
| 256 |
gt_txt_list = []
|
| 257 |
|
| 258 |
+
if is_train_img:
|
| 259 |
+
train_list.append(img_name)
|
| 260 |
+
else:
|
| 261 |
+
test_list.append(img_name)
|
| 262 |
# num_img += 1
|
| 263 |
|
| 264 |
+
img_path = os.path.join(out_dir, 'train_images', img_name)
|
| 265 |
img_gt_path = os.path.join(
|
| 266 |
+
out_dir, 'train_gts', gt_name)
|
| 267 |
+
|
| 268 |
+
dir1 = os.path.dirname(img_path)
|
| 269 |
+
dir2 = os.path.dirname(img_gt_path)
|
| 270 |
+
|
| 271 |
+
if not os.path.exists(dir1):
|
| 272 |
+
os.makedirs(dir1)
|
| 273 |
+
|
| 274 |
+
if not os.path.exists(dir2):
|
| 275 |
+
os.makedirs(dir2)
|
| 276 |
|
| 277 |
cv2.imwrite(img_path, img)
|
| 278 |
|
|
|
|
| 296 |
elif angle != 0:
|
| 297 |
|
| 298 |
# 变换前画出绿框,方便追踪点的前后变化
|
| 299 |
+
img_color = cv2.rectangle(img_color, (word_x, word_y), (word_x + word_width, word_y + word_height), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
|
|
|
|
| 300 |
|
| 301 |
+
cv2.imshow("green", img_color)
|
| 302 |
+
cv2.waitKey(0)
|
| 303 |
|
| 304 |
# 变换前的多边形蓝框
|
| 305 |
points = np.array([
|
|
|
|
| 309 |
[word_x, word_y + word_height], # 左下
|
| 310 |
])
|
| 311 |
|
| 312 |
+
# cv2.fillPoly(img_color, pts=[points], color=(255, 0, 0)) # 填充
|
| 313 |
+
cv2.polylines(img_color, [points], isClosed=True, color=(
|
| 314 |
+
255, 0, 0), thickness=1) # 只画线,不填充
|
| 315 |
|
| 316 |
+
cv2.imshow("polys", img_color)
|
| 317 |
+
cv2.waitKey(0)
|
| 318 |
|
| 319 |
# 获取图像的��度,并计算中心
|
| 320 |
(h, w) = img_color.shape[:2]
|
|
|
|
| 328 |
img_color = cv2.warpAffine(img_color, M, (w, h))
|
| 329 |
img_color_transform = img_color.copy()
|
| 330 |
|
| 331 |
+
cv2.imshow("after trans", img_color)
|
| 332 |
+
cv2.waitKey(0)
|
| 333 |
|
| 334 |
# https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html # 原理
|
| 335 |
# 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?
|
|
|
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
+
cv2.imshow("orgin", img_color_origin)
|
| 367 |
+
cv2.waitKey(0)
|
| 368 |
|
| 369 |
|
| 370 |
|
|
|
|
| 382 |
rd = [pos[2]['x'], pos[2]['y']]
|
| 383 |
ld = [pos[3]['x'], pos[3]['y']]
|
| 384 |
|
| 385 |
+
# 生成 icdar2015 格式的人工标记训练数据(用于训练官方DB)
|
| 386 |
+
gt_txt_list.append( "{},{},{},{},{},{},{},{},{}".format(lu[0], lu[1], ru[0], ru[1], rd[0], rd[1], ld[0], ld[1], word) )
|
| 387 |
|
| 388 |
# 绘制矩形
|
| 389 |
start_point = (x, y) # 矩形的左上角
|
|
|
|
| 395 |
thickness = 2
|
| 396 |
|
| 397 |
# 逐行画框
|
| 398 |
+
img_color = cv2.rectangle(img_color, start_point, end_point, color, thickness)
|
| 399 |
+
cv2.imshow("box", img_color)
|
| 400 |
|
| 401 |
+
cv2.waitKey(0)
|
| 402 |
|
| 403 |
points = [ lu, ru, rd, ld ]
|
| 404 |
|