如何将label转换成二进制的bin文件

import sys
import os
import xml.etree.ElementTree as ET
from struct import *


root="/home/zhao/xml"
output="labels_obstacle.bin"

def write_labels():
    names = os.listdir(root)
    file = open(output, "wb")
    file.write(pack("i", len(names)))
    for name in names:
        dom = ET.parse(os.path.join(root, name))
        file.write(pack("400s", dom.find("filename").text))
        num = 0
        for obj in dom.iter("object"):
            num = num + 1
       
        file.write(pack("iii", int(dom.find("size").find("width").text), int(dom.find("size").find("height").text), num))
        for obj in dom.iter("object"):
            file.write(pack("ffff", float(obj.find("bndbox").find("xmin").text), float(obj.find("bndbox").find("ymin").text), \
                               float(obj.find("bndbox").find("xmax").text), float(obj.find("bndbox").find("ymax").text)))
            print int(dom.find("size").find("width").text), int(dom.find("size").find("height").text), num, \
                  float(obj.find("bndbox").find("xmin").text), float(obj.find("bndbox").find("ymin").text), \
                  float(obj.find("bndbox").find("xmax").text), float(obj.find("bndbox").find("ymax").text)
    file.close()

def read_labels():
    file = open(output, "r")
    nlabel = unpack("i", file.read(4))[0]
    for i in range(nlabel):
        name = unpack("400s", file.read(400))[0]
        prob = unpack("iii", file.read(12))
        width, height, nbox = prob[0], prob[1], prob[2]
        for j in range(nbox):
            box = unpack("ffff", file.read(16))
            xmin, ymin, xmax, ymax = box[0], box[1], box[2], box[3]
            print name, width, height, nbox,xmin, ymin, xmax, ymax
if __name__ == '__main__':
    write_labels()
    #read_labels()

 

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页