aliocr_IC15_convert done.
Browse files- .gitignore +1 -0
- aliocr_IC15_convert.py +182 -158
.gitignore
CHANGED
|
@@ -1 +1,2 @@
|
|
| 1 |
icdar2015_aliocr/
|
|
|
|
|
|
| 1 |
icdar2015_aliocr/
|
| 2 |
+
poly.jpg
|
aliocr_IC15_convert.py
CHANGED
|
@@ -1,6 +1,7 @@
|
|
| 1 |
|
| 2 |
# pip install numpy==1.26.4 opencv-python==4.6.0.66
|
| 3 |
|
|
|
|
| 4 |
|
| 5 |
"""
|
| 6 |
|
|
@@ -8,21 +9,18 @@
|
|
| 8 |
|
| 9 |
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
|
| 10 |
|
| 11 |
-
给 mmocr 训练用。格式是 icdar2015 的格式,文件夹的组织方式是按照 mmocr 的要求创建的
|
| 12 |
-
|
| 13 |
"""
|
| 14 |
|
| 15 |
|
| 16 |
"""
|
| 17 |
|
| 18 |
-
! unzip ./GD500.zip -d DB/datasets
|
| 19 |
-
|
| 20 |
icdar2015 文本检测数据集
|
| 21 |
标注格式: x1,y1,x2,y2,x3,y3,x4,y4,text
|
| 22 |
|
| 23 |
其中, x1,y1为左上角坐标,x2,y2为右上角坐标,x3,y3为右下角坐标,x4,y4为左下角坐标。
|
| 24 |
|
| 25 |
### 表示text难以辨认。
|
|
|
|
| 26 |
"""
|
| 27 |
|
| 28 |
|
|
@@ -132,12 +130,12 @@ def cutPoly(img, pts):
|
|
| 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 |
-
|
| 140 |
-
|
| 141 |
|
| 142 |
if os.path.exists(gt):
|
| 143 |
|
|
@@ -174,7 +172,7 @@ if __name__ == "__main__":
|
|
| 174 |
poly = np.array(poly)
|
| 175 |
poly = poly.astype(np.int32)
|
| 176 |
|
| 177 |
-
#cv2.fillPoly(img, pts=[ poly ], color=(0, 0, 255))
|
| 178 |
|
| 179 |
b = random.randint(0, 255) # 用来生成[a,b]之间的随意整数,包括两个边界值。
|
| 180 |
g = random.randint(0, 255)
|
|
@@ -184,16 +182,18 @@ if __name__ == "__main__":
|
|
| 184 |
cv2.polylines(img, [poly], isClosed=True,
|
| 185 |
color=(b, g, r), thickness=1)
|
| 186 |
|
| 187 |
-
|
| 188 |
|
| 189 |
-
cv2.imshow("poly", img)
|
| 190 |
-
cv2.waitKey()
|
| 191 |
|
| 192 |
# 开始转换
|
| 193 |
|
| 194 |
out_dir = 'icdar2015_aliocr'
|
| 195 |
-
|
| 196 |
-
|
|
|
|
|
|
|
| 197 |
|
| 198 |
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
|
| 199 |
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
|
@@ -215,21 +215,21 @@ if __name__ == "__main__":
|
|
| 215 |
|
| 216 |
base = Path(json_path).stem
|
| 217 |
|
| 218 |
-
|
| 219 |
|
| 220 |
-
if not os.path.exists(
|
| 221 |
continue
|
| 222 |
|
| 223 |
jsn = load_json(json_path)
|
| 224 |
|
| 225 |
-
with open(
|
| 226 |
imgdata = fp.read()
|
| 227 |
imgdata = base64.b64decode(imgdata)
|
| 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)
|
|
@@ -242,199 +242,222 @@ if __name__ == "__main__":
|
|
| 242 |
img_color_origin2 = img_color.copy()
|
| 243 |
|
| 244 |
|
| 245 |
-
# 生成1000 张一模一样的图
|
| 246 |
-
for i in range(1, 2): # 1000+1
|
| 247 |
|
| 248 |
-
|
|
|
|
| 249 |
|
| 250 |
-
|
| 251 |
-
|
| 252 |
|
| 253 |
-
|
| 254 |
-
# 85% 的概率是训练图
|
| 255 |
|
| 256 |
-
|
|
|
|
|
|
|
|
|
|
| 257 |
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
# num_img += 1
|
| 263 |
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
|
|
|
|
|
|
| 270 |
|
| 271 |
-
|
| 272 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 273 |
|
| 274 |
-
if not os.path.exists(dir2):
|
| 275 |
-
os.makedirs(dir2)
|
| 276 |
|
| 277 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 278 |
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
jo = wordsInfo[j]
|
| 282 |
-
word = jo["word"]
|
| 283 |
-
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
| 284 |
-
angle = jo['angle']
|
| 285 |
-
|
| 286 |
-
img_color = img_color_origin.copy()
|
| 287 |
|
| 288 |
-
|
| 289 |
-
|
| 290 |
-
word_width = jo['width']
|
| 291 |
-
word_height = jo['height']
|
| 292 |
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
|
|
|
|
|
|
|
|
|
|
| 297 |
|
| 298 |
-
|
| 299 |
-
|
|
|
|
| 300 |
|
| 301 |
-
|
| 302 |
-
|
| 303 |
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
[word_x + word_width, word_y], # 右上
|
| 308 |
-
[word_x + word_width, word_y + word_height], # 右下
|
| 309 |
-
[word_x, word_y + word_height], # 左下
|
| 310 |
-
])
|
| 311 |
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 315 |
|
| 316 |
-
|
| 317 |
-
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
|
|
|
|
|
|
|
|
|
| 330 |
|
| 331 |
-
|
| 332 |
-
cv2.waitKey(0)
|
| 333 |
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
# 如何得到移动后的坐标点
|
| 337 |
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
# 右上
|
| 341 |
-
[word_x + word_width, word_y],
|
| 342 |
-
[word_x + word_width, word_y + \
|
| 343 |
-
word_height], # 右下
|
| 344 |
-
[word_x, word_y + word_height], # 左下
|
| 345 |
-
])
|
| 346 |
-
# add ones
|
| 347 |
-
ones = np.ones(shape=(len(points), 1))
|
| 348 |
|
| 349 |
-
|
|
|
|
| 350 |
|
| 351 |
-
# transform points
|
| 352 |
-
transformed_points = M.dot(points_ones.T).T
|
| 353 |
|
| 354 |
-
|
| 355 |
-
|
| 356 |
|
| 357 |
-
|
| 358 |
-
|
|
|
|
|
|
|
| 359 |
|
| 360 |
|
| 361 |
-
cv2.polylines(img_color_origin, [points], isClosed=True, color=(
|
| 362 |
-
random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
|
| 363 |
|
| 364 |
-
|
| 365 |
|
| 366 |
-
|
| 367 |
-
|
|
|
|
|
|
|
| 368 |
|
|
|
|
|
|
|
| 369 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 370 |
|
|
|
|
|
|
|
| 371 |
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
x = int(pos[0]["x"]) # 左上
|
| 375 |
-
y = int(pos[0]["y"])
|
| 376 |
|
| 377 |
-
|
| 378 |
-
y2 = int(pos[2]["y"])
|
| 379 |
|
| 380 |
-
|
| 381 |
-
ru = [pos[1]['x'], pos[1]['y']]
|
| 382 |
-
rd = [pos[2]['x'], pos[2]['y']]
|
| 383 |
-
ld = [pos[3]['x'], pos[3]['y']]
|
| 384 |
|
| 385 |
-
|
| 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 |
-
|
|
|
|
|
|
|
| 390 |
|
| 391 |
-
|
| 392 |
|
| 393 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 394 |
|
| 395 |
-
|
| 396 |
|
| 397 |
-
|
| 398 |
-
img_color = cv2.rectangle(img_color, start_point, end_point, color, thickness)
|
| 399 |
-
cv2.imshow("box", img_color)
|
| 400 |
|
| 401 |
-
|
| 402 |
|
| 403 |
-
points = [ lu, ru, rd, ld ]
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
-
points0 = np.array([[word_x, word_y], # 左上
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
points1 = np.array( [ lu, ru, rd, ld ] )
|
| 415 |
|
| 416 |
|
| 417 |
-
if not (abs(angle) == 90 or abs(angle) == 270) and angle != 0:
|
| 418 |
-
|
| 419 |
-
else:
|
| 420 |
-
|
| 421 |
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
|
| 426 |
-
|
| 427 |
|
| 428 |
-
|
| 429 |
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
# g_count += 1
|
| 439 |
|
| 440 |
# cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全
|
|
@@ -580,5 +603,6 @@ if __name__ == "__main__":
|
|
| 580 |
# fp.write(gt_txt)
|
| 581 |
|
| 582 |
|
|
|
|
| 583 |
|
| 584 |
|
|
|
|
| 1 |
|
| 2 |
# pip install numpy==1.26.4 opencv-python==4.6.0.66
|
| 3 |
|
| 4 |
+
# see doc\lang\programming\pytorch\文本检测\DBNET 论文代码都有
|
| 5 |
|
| 6 |
"""
|
| 7 |
|
|
|
|
| 9 |
|
| 10 |
将阿里OCR 的识别结果(图片和标注)转换成 icdar2015 格式 (注意:它的文本是含 utf8 bom 的)
|
| 11 |
|
|
|
|
|
|
|
| 12 |
"""
|
| 13 |
|
| 14 |
|
| 15 |
"""
|
| 16 |
|
|
|
|
|
|
|
| 17 |
icdar2015 文本检测数据集
|
| 18 |
标注格式: x1,y1,x2,y2,x3,y3,x4,y4,text
|
| 19 |
|
| 20 |
其中, x1,y1为左上角坐标,x2,y2为右上角坐标,x3,y3为右下角坐标,x4,y4为左下角坐标。
|
| 21 |
|
| 22 |
### 表示text难以辨认。
|
| 23 |
+
|
| 24 |
"""
|
| 25 |
|
| 26 |
|
|
|
|
| 130 |
if __name__ == "__main__":
|
| 131 |
|
| 132 |
# 验证原版的文本标记框
|
| 133 |
+
# im = './datasets/icdar2015/train_images/img_1.jpg'
|
| 134 |
+
# gt = './datasets/icdar2015/train_gts/gt_img_1.txt'
|
| 135 |
|
| 136 |
# 验证自已生成的标记框
|
| 137 |
+
im = './icdar2015_aliocr/train_images/img_000001.jpg'
|
| 138 |
+
gt = './icdar2015_aliocr/train_gts/gt_img_000001.txt'
|
| 139 |
|
| 140 |
if os.path.exists(gt):
|
| 141 |
|
|
|
|
| 172 |
poly = np.array(poly)
|
| 173 |
poly = poly.astype(np.int32)
|
| 174 |
|
| 175 |
+
# cv2.fillPoly(img, pts=[ poly ], color=(0, 0, 255))
|
| 176 |
|
| 177 |
b = random.randint(0, 255) # 用来生成[a,b]之间的随意整数,包括两个边界值。
|
| 178 |
g = random.randint(0, 255)
|
|
|
|
| 182 |
cv2.polylines(img, [poly], isClosed=True,
|
| 183 |
color=(b, g, r), thickness=1)
|
| 184 |
|
| 185 |
+
cv2.imwrite("poly.jpg", img)
|
| 186 |
|
| 187 |
+
# cv2.imshow("poly", img)
|
| 188 |
+
# cv2.waitKey()
|
| 189 |
|
| 190 |
# 开始转换
|
| 191 |
|
| 192 |
out_dir = 'icdar2015_aliocr'
|
| 193 |
+
if os.path.exists(out_dir):
|
| 194 |
+
import shutil
|
| 195 |
+
shutil.rmtree(out_dir)
|
| 196 |
+
|
| 197 |
|
| 198 |
# https://help.aliyun.com/document_detail/294540.html 阿里云ocr结果字段定义
|
| 199 |
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
|
|
|
| 215 |
|
| 216 |
base = Path(json_path).stem
|
| 217 |
|
| 218 |
+
img_train_path = os.path.join(dir_img, '{}.txt'.format(base))
|
| 219 |
|
| 220 |
+
if not os.path.exists(img_train_path): # 没有相应的图片,可能被删除了
|
| 221 |
continue
|
| 222 |
|
| 223 |
jsn = load_json(json_path)
|
| 224 |
|
| 225 |
+
with open(img_train_path, "r", encoding="utf-8") as fp:
|
| 226 |
imgdata = fp.read()
|
| 227 |
imgdata = base64.b64decode(imgdata)
|
| 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)
|
|
|
|
| 242 |
img_color_origin2 = img_color.copy()
|
| 243 |
|
| 244 |
|
|
|
|
|
|
|
| 245 |
|
| 246 |
+
img_name = "img_{:06d}.jpg".format(g_count)
|
| 247 |
+
gt_name = "gt_img_{:06d}.txt".format(g_count)
|
| 248 |
|
| 249 |
+
is_train_img = random.choices([0, 1], weights=[0.15, 0.85])[0]
|
| 250 |
+
# 85% 的概率是训练图
|
| 251 |
|
| 252 |
+
gt_txt_list = []
|
|
|
|
| 253 |
|
| 254 |
+
img_train_path = os.path.join(out_dir, 'train_images', img_name)
|
| 255 |
+
img_train_gt_path = os.path.join(out_dir, 'train_gts', gt_name)
|
| 256 |
+
img_test_path = os.path.join(out_dir, 'test_images', img_name)
|
| 257 |
+
img_test_gt_path = os.path.join(out_dir, 'test_gts', gt_name)
|
| 258 |
|
| 259 |
+
dir1 = os.path.dirname(img_train_path)
|
| 260 |
+
dir2 = os.path.dirname(img_train_gt_path)
|
| 261 |
+
dir3 = os.path.dirname(img_test_path)
|
| 262 |
+
dir4 = os.path.dirname(img_test_gt_path)
|
|
|
|
| 263 |
|
| 264 |
+
if not os.path.exists(dir1):
|
| 265 |
+
os.makedirs(dir1)
|
| 266 |
+
if not os.path.exists(dir2):
|
| 267 |
+
os.makedirs(dir2)
|
| 268 |
+
if not os.path.exists(dir3):
|
| 269 |
+
os.makedirs(dir3)
|
| 270 |
+
if not os.path.exists(dir4):
|
| 271 |
+
os.makedirs(dir4)
|
| 272 |
|
| 273 |
+
if is_train_img:
|
| 274 |
+
train_list.append(img_name)
|
| 275 |
+
cv2.imwrite(img_train_path, img)
|
| 276 |
+
else:
|
| 277 |
+
test_list.append(img_name)
|
| 278 |
+
cv2.imwrite(img_test_path, img)
|
| 279 |
|
|
|
|
|
|
|
| 280 |
|
| 281 |
+
wordsInfo = jsn['prism_wordsInfo']
|
| 282 |
+
for j in range(len(wordsInfo)):
|
| 283 |
+
jo = wordsInfo[j]
|
| 284 |
+
word = jo["word"]
|
| 285 |
+
# prism-wordsInfo 里的 angle 文字块的角度,这个角度只影响width和height,当角度为-90、90、-270、270,width和height的值需要自行互换
|
| 286 |
+
angle = jo['angle']
|
| 287 |
+
|
| 288 |
+
img_color = img_color_origin.copy()
|
| 289 |
+
|
| 290 |
+
"""
|
| 291 |
+
x y 宽高全部不靠谱, pos 里是对的
|
| 292 |
+
"""
|
| 293 |
+
# word_x = jo['x']
|
| 294 |
+
# word_y = jo['y']
|
| 295 |
+
# word_width = jo['width']
|
| 296 |
+
# word_height = jo['height']
|
| 297 |
+
|
| 298 |
+
# if abs(angle) == 90 or abs(angle) == 270:
|
| 299 |
+
# word_width = jo['height']
|
| 300 |
+
# word_height = jo['width']
|
| 301 |
+
|
| 302 |
+
# elif angle != 0:
|
| 303 |
|
| 304 |
+
# # 变换前画出绿框,方便追踪点的前后变化
|
| 305 |
+
# img_color = cv2.rectangle(img_color, (word_x, word_y), (word_x + word_width, word_y + word_height), (0, 255, 0), 2) # 矩形的左上角, 矩形的右下角
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 306 |
|
| 307 |
+
# cv2.imshow("green", img_color)
|
| 308 |
+
# cv2.waitKey(0)
|
|
|
|
|
|
|
| 309 |
|
| 310 |
+
# # 变换前的多边形蓝框
|
| 311 |
+
# points = np.array([
|
| 312 |
+
# [word_x, word_y], # 左上
|
| 313 |
+
# [word_x + word_width, word_y], # 右上
|
| 314 |
+
# [word_x + word_width, word_y + word_height], # 右下
|
| 315 |
+
# [word_x, word_y + word_height], # 左下
|
| 316 |
+
# ])
|
| 317 |
|
| 318 |
+
# # cv2.fillPoly(img_color, pts=[points], color=(255, 0, 0)) # 填充
|
| 319 |
+
# cv2.polylines(img_color, [points], isClosed=True, color=(
|
| 320 |
+
# 255, 0, 0), thickness=1) # 只画线,不填充
|
| 321 |
|
| 322 |
+
# cv2.imshow("polys", img_color)
|
| 323 |
+
# cv2.waitKey(0)
|
| 324 |
|
| 325 |
+
# # 获取图像的维度,并计算中心
|
| 326 |
+
# (h, w) = img_color.shape[:2]
|
| 327 |
+
# (cX, cY) = (w // 2, h // 2)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 328 |
|
| 329 |
+
# # - (cX,cY): 旋转的中心点坐标
|
| 330 |
+
# # - 180: 旋转的度数,正度数表示逆时针旋转,而负度数表示顺时针旋转。
|
| 331 |
+
# # - 1.0:旋转后图像的大小,1.0原图,2.0变成原来的2倍,0.5变成原来的0.5倍
|
| 332 |
+
# # 1° = π/180弧度 1 弧度 = 180 / 3.1415926 // 0.0190033 是Mathematica 算出来的弧度,先转换成角度 // -0.0190033 * (180 / 3.1415926)
|
| 333 |
+
# M = cv2.getRotationMatrix2D((cX, cY), angle, 1.0)
|
| 334 |
+
# img_color = cv2.warpAffine(img_color, M, (w, h))
|
| 335 |
+
# img_color_transform = img_color.copy()
|
| 336 |
|
| 337 |
+
# cv2.imshow("after trans", img_color)
|
| 338 |
+
# cv2.waitKey(0)
|
| 339 |
|
| 340 |
+
# # https://docs.opencv.org/2.4/doc/tutorials/imgproc/imgtrans/warp_affine/warp_affine.html # 原理
|
| 341 |
+
# # 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?
|
| 342 |
+
# # 如何得到移动后的坐标点
|
| 343 |
|
| 344 |
+
# # points 算出四个点变换后移动到哪里了
|
| 345 |
+
# points = np.array([[word_x, word_y], # 左上
|
| 346 |
+
# # 右上
|
| 347 |
+
# [word_x + word_width, word_y],
|
| 348 |
+
# [word_x + word_width, word_y + \
|
| 349 |
+
# word_height], # 右下
|
| 350 |
+
# [word_x, word_y + word_height], # 左下
|
| 351 |
+
# ])
|
| 352 |
+
# # add ones
|
| 353 |
+
# ones = np.ones(shape=(len(points), 1))
|
| 354 |
|
| 355 |
+
# points_ones = np.hstack([points, ones])
|
|
|
|
| 356 |
|
| 357 |
+
# # transform points
|
| 358 |
+
# transformed_points = M.dot(points_ones.T).T
|
|
|
|
| 359 |
|
| 360 |
+
# transformed_points_int = np.round(
|
| 361 |
+
# transformed_points, decimals=0).astype(np.int32) # 批量四舍五入
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 362 |
|
| 363 |
+
# cv2.polylines(img_color, [transformed_points_int], isClosed=True, color=(
|
| 364 |
+
# 0, 0, 255), thickness=2) # 画转换后的点
|
| 365 |
|
|
|
|
|
|
|
| 366 |
|
| 367 |
+
# cv2.polylines(img_color_origin, [points], isClosed=True, color=(
|
| 368 |
+
# random.randint(0, 255), random.randint(0, 255), random.randint(0, 255)), thickness=2) # 画转换前的点
|
| 369 |
|
| 370 |
+
|
| 371 |
+
|
| 372 |
+
# cv2.imshow("orgin", img_color_origin)
|
| 373 |
+
# cv2.waitKey(0)
|
| 374 |
|
| 375 |
|
|
|
|
|
|
|
| 376 |
|
|
|
|
| 377 |
|
| 378 |
+
# 四个角的位置 # 左上、右上、右下、左下,当NeedRotate为true��,如果最外层的angle不为0,需要按照angle矫正图片后,坐标才准确
|
| 379 |
+
pos = jo["pos"]
|
| 380 |
+
x = int(pos[0]["x"]) # 左上
|
| 381 |
+
y = int(pos[0]["y"])
|
| 382 |
|
| 383 |
+
x2 = int(pos[2]["x"]) # 右下
|
| 384 |
+
y2 = int(pos[2]["y"])
|
| 385 |
|
| 386 |
+
lu = [pos[0]['x'], pos[0]['y']] # left up 四个角顺时针方向数
|
| 387 |
+
ru = [pos[1]['x'], pos[1]['y']]
|
| 388 |
+
rd = [pos[2]['x'], pos[2]['y']]
|
| 389 |
+
ld = [pos[3]['x'], pos[3]['y']]
|
| 390 |
|
| 391 |
+
# 生成 icdar2015 格式的人工标记训练数据(用于训练官方DB)
|
| 392 |
+
gt_txt_list.append( "{},{},{},{},{},{},{},{},{}".format(lu[0], lu[1], ru[0], ru[1], rd[0], rd[1], ld[0], ld[1], word) )
|
| 393 |
|
| 394 |
+
# 绘制矩形
|
| 395 |
+
start_point = (x, y) # 矩形的左上角
|
|
|
|
|
|
|
| 396 |
|
| 397 |
+
end_point = (x2, y2) # 矩形的右下角
|
|
|
|
| 398 |
|
| 399 |
+
color = (0, 0, 255) # BGR
|
|
|
|
|
|
|
|
|
|
| 400 |
|
| 401 |
+
thickness = 2
|
|
|
|
| 402 |
|
| 403 |
+
# 逐行画框
|
| 404 |
+
img_color = cv2.rectangle(img_color, start_point, end_point, color, thickness)
|
| 405 |
+
# cv2.imshow("box", img_color)
|
| 406 |
+
# cv2.waitKey(0)
|
| 407 |
|
| 408 |
+
gt_txt = "\n".join(gt_txt_list)
|
| 409 |
|
| 410 |
+
if is_train_img:
|
| 411 |
+
with open(img_train_gt_path, 'w', encoding='utf-8') as f:
|
| 412 |
+
f.write(gt_txt)
|
| 413 |
+
else:
|
| 414 |
+
with open(img_test_gt_path, 'w', encoding='utf-8') as f:
|
| 415 |
+
f.write(gt_txt)
|
| 416 |
+
|
| 417 |
+
|
| 418 |
+
print(f'### one task one. {g_count} / {len(json_paths)}')
|
| 419 |
|
| 420 |
+
g_count += 1
|
| 421 |
|
| 422 |
+
|
|
|
|
|
|
|
| 423 |
|
| 424 |
+
|
| 425 |
|
| 426 |
+
# points = [ lu, ru, rd, ld ]
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
+
# points0 = np.array([[word_x, word_y], # 左上
|
| 431 |
+
# # 右上
|
| 432 |
+
# [word_x + word_width, word_y],
|
| 433 |
+
# [word_x + word_width, word_y + \
|
| 434 |
+
# word_height], # 右下
|
| 435 |
+
# [word_x, word_y + word_height], # 左下
|
| 436 |
+
# ])
|
| 437 |
+
# points1 = np.array( [ lu, ru, rd, ld ] )
|
| 438 |
|
| 439 |
|
| 440 |
+
# if not (abs(angle) == 90 or abs(angle) == 270) and angle != 0:
|
| 441 |
+
# points = transform( points, M )
|
| 442 |
+
# else:
|
| 443 |
+
# points = np.array(points)
|
| 444 |
|
| 445 |
+
# ps3 = np.array(
|
| 446 |
+
# [
|
| 447 |
+
# [min( points[0][0], points1[0][0] ), min( points[0][1], points1[0][1] )], # 左上(取最两者中最小的)
|
| 448 |
|
| 449 |
+
# [max( points[1][0], points1[1][0] ), min( points[1][1], points1[1][1] )], # 右上
|
| 450 |
|
| 451 |
+
# [max( points[2][0], points1[2][0] ), max( points[2][1], points1[2][1] )], # 右下
|
| 452 |
|
| 453 |
+
# [min( points[3][0], points1[3][0] ), max( points[3][1], points1[3][1] )] # 左下
|
| 454 |
+
# ]
|
| 455 |
+
# )
|
| 456 |
|
| 457 |
+
# img_cuted = cutPoly(img, ps3)
|
| 458 |
+
# cv2.imwrite(f'./tmp/{g_count}.jpg', img_cuted)
|
| 459 |
+
# with open(f'./tmp/{g_count}.txt', 'w', encoding='utf-8') as f:
|
| 460 |
+
# f.write(word)
|
| 461 |
# g_count += 1
|
| 462 |
|
| 463 |
# cv2.polylines(img_color, [points], isClosed=True, color=( # 多边形,框得比较全
|
|
|
|
| 603 |
# fp.write(gt_txt)
|
| 604 |
|
| 605 |
|
| 606 |
+
print('### all task done.')
|
| 607 |
|
| 608 |
|