-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathconvert_bbox_tr2cwh2.py
41 lines (32 loc) · 1.33 KB
/
convert_bbox_tr2cwh2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import os
from os import listdir
from os.path import join
import numpy as np
from PIL import Image
from bbox_tr import *
in_dir = r'D:\ImageSamples\DOTA v1.5\train\images\images'
out_dir = r'D:\ImageSamples\DOTA v1.5\train\images\cwh'
if not os.path.exists(out_dir):
os.makedirs(out_dir)
for i in os.listdir(in_dir):
image_id,image_ext = os.path.splitext(i)
if image_ext.lower() == '.txt':
out_file = open(join(out_dir,i), 'w')
in_file = open(join(in_dir,i), 'r')
for line in in_file:
parts = line.split()
if(len(parts) > 6):
bbox_tr_ = [float(x) for x in parts[1:7]]
bbox_4pt_ = bbox_tr_2_4pt(bbox_tr_)
bbox_cwh_ = bbox_4pt_2_cwh(bbox_4pt_)
img_path = join(in_dir,image_id)+".png"
if bbox_cwh_[2] > 1 and os.path.exists(img_path):
img = Image.open(img_path)
img_w = img.size[0]
img_h = img.size[1]
bbox_cwh_ = [bbox_tr_[0]/img_w,bbox_tr_[1]/img_h,bbox_cwh_[2]/img_w,bbox_cwh_[3]/img_h]
out_file.write(
parts[0] + " "
+ " ".join(['%.7f'%a for a in bbox_cwh_]) + '\n')
out_file.close()
in_file.close()