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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
| import os import xml.etree.ElementTree as ET import math import cv2 as cv def voc_to_dota(xml_path, xml_name): txt_name = xml_name[:-4] + '.txt' txt_path = xml_path[:-4] + '/txt_label' if not os.path.exists(txt_path): os.makedirs(txt_path) txt_file = os.path.join(txt_path, txt_name) file_path = os.path.join(xml_path, file_list[i]) tree = ET.parse(os.path.join(file_path)) root = tree.getroot() image_path = '../data/DOTA/images/' out_path = '../data/DOTA/outputImg/' filename = image_path + xml_name[:-4] + '.Jpeg' img = cv.imread(filename) with open(txt_file, "w+", encoding='UTF-8') as out_file: for obj in root.findall('object'): name = obj.find('name').text difficult = obj.find('difficult').text robndbox = obj.find('robndbox') cx = float(robndbox.find('cx').text) cy = float(robndbox.find('cy').text) w = float(robndbox.find('w').text) h = float(robndbox.find('h').text) angle = float(robndbox.find('angle').text) p0x, p0y = rotatePoint(cx, cy, cx - w / 2, cy - h / 2, -angle) p1x, p1y = rotatePoint(cx, cy, cx + w / 2, cy - h / 2, -angle) p2x, p2y = rotatePoint(cx, cy, cx + w / 2, cy + h / 2, -angle) p3x, p3y = rotatePoint(cx, cy, cx - w / 2, cy + h / 2, -angle) dict = {p0y:p0x, p1y:p1x, p2y:p2x, p3y:p3x} list = find_topLeftPopint(dict) if list[0] == p0x: list_xy = [p0x, p0y, p1x, p1y, p2x, p2y, p3x, p3y] elif list[0] == p1x: list_xy = [p1x, p1y, p2x, p2y, p3x, p3y, p0x, p0y] elif list[0] == p2x: list_xy = [p2x, p2y, p3x, p3y, p0x, p0y, p1x, p1y] else: list_xy = [p3x, p3y, p0x, p0y, p1x, p1y, p2x, p2y] cv.line(img, (int(list_xy[0]), int(list_xy[1])), (int(list_xy[2]), int(list_xy[3])), color=(255, 0, 0), thickness=3) cv.line(img, (int(list_xy[2]), int(list_xy[3])), (int(list_xy[4]), int(list_xy[5])), color=(0, 255, 0), thickness= 3) cv.line(img, (int(list_xy[4]), int(list_xy[5])), (int(list_xy[6]), int(list_xy[7])), color=(0, 0, 255), thickness = 2) cv.line(img, (int(list_xy[6]), int(list_xy[7])), (int(list_xy[0]), int(list_xy[1])), color=(255, 255, 0), thickness = 2) cv.imwrite(out_path + xml_name[:-4] + '.Jpeg', img) data = str(list_xy[0]) + " " + str(list_xy[1]) + " " + str(list_xy[2]) + " " + str(list_xy[3]) + " " + \ str(list_xy[4]) + " " + str(list_xy[5]) + " " + str(list_xy[6]) + " " + str(list_xy[7]) + " " data = data + name + " " + difficult + "\n" out_file.write(data) def find_topLeftPopint(dict): dict_keys = sorted(dict.keys()) temp = [dict[dict_keys[0]], dict[dict_keys[1]]] minx = min(temp) if minx == temp[0]: miny = dict_keys[0] else: miny = dict_keys[1] return [minx, miny]
def rotatePoint(xc, yc, xp, yp, theta): xoff = xp - xc yoff = yp - yc cosTheta = math.cos(theta) sinTheta = math.sin(theta) pResx = cosTheta * xoff + sinTheta * yoff pResy = - sinTheta * xoff + cosTheta * yoff return float(format(xc + pResx, '.1f')), float(format(yc + pResy, '.1f')) if __name__ == '__main__': root_path = '../data/DOTA/xml' file_list = os.listdir(root_path) for i in range(0, len(file_list)): if ('.xml' in file_list[i]) or ('.XML' in file_list[i]): voc_to_dota(root_path, file_list[i]) print('----------------------------------------{}{}----------------------------------------' .format(file_list[i], ' has Done!')) else: print(file_list[i] + ' is not xml file')
|