切断カットピクチャ原点と元の位置に応じて標識された後に自動的に再ラベリング

*コーディング:UTF-8 *

ETとしてインポートxml.etree.ElementTree
xml.etreeインポートのElementTreeから
の輸入ピクルス
輸入OS
GETCWD OSの輸入LISTDIRから、
はos.pathインポートから参加
インポートCV2の
NPとしてインポートnumpyのを
PILインポートイメージから

クラス= [ "1"]#検出自転車

:DEF(サイズボックス)に変換
DW = 1 /サイズ[0]

dh = 1. / size[1]
x = (box[0] + box[1]) / 2.0
y = (box[2] + box[3]) / 2.0
w = box[1] - box[0]
h = box[3] - box[2]
x = x * dw
w = w * dw
y = y * dh
h = h * dh
return (x, y, w, h)

DEF convert_annotation(image_idは):
もしos.path.exists( 'E:/食品XML /%' %(image_idは)):
#list_file_train =オープン( 'D:/10_14/train.txt'、 '+'、エンコーディング= 'UTF-8')
#list_file_train.write( 'D:/10_14/yoloData/%s.jpg \ n'は%(STR(image_idは)))
#list_file_train.close()

in_file = open('D:/Users/h2410796/Desktop/xml/7/%s.xml' % (image_id), encoding='utf-8')
out_file = open('D:/Users/h2410796/Desktop/txt/7/%s.txt' % (image_id), 'a', encoding='utf-8')  # 生成txt格式文件
tree = ET.parse(in_file)
root = tree.getroot()


for obj in root.iter('object'):
    cls = obj.find('name').text
    if cls not in classes:
        continue
    cls_id = classes.index(cls)
    xmlbox = obj.find('bndbox')
   # print(type(xmlbox.find('xmin').text))
    b = (float(xmlbox.find('xmin').text)-650, float(xmlbox.find('xmax').text)-650, float(xmlbox.find('ymin').text)-1950,
         float(xmlbox.find('ymax').text)-1950)
    bb = convert((w, h), b)
    out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
    # cv2.imwrite(r'./data/duodianpian' + '/' + str(i.split('/')[2].split('\\')[1]), dst)
    # cv2.waitKey(0)

ann_path =「D:/ユーザ/ h2410796 /デスクトップ/ suoyou / 7 /」
ファイル一覧= os.listdir(ann_path)
ファイルリスト中のファイルのための:
filename0 = os.path.splitext(ファイル)[0] [ - 1]
img_name = os.path.splitext(ファイル)[0] + '.JPG'
fileimgpath = ann_path + img_name
IM = Image.open(fileimgpath)
W = INT(im.size [0])
H = INT(im.size [1] )

印刷(filename0)#は、ファイル名を読み取ります

convert_annotation(filename0)
公開された41元の記事 ウォン称賛7 ビュー3680

おすすめ

転載: blog.csdn.net/weixin_43091087/article/details/103538876