首页 > 编程知识 正文

xml怎么转换成excel,怎样把文件保存为xml格式

时间:2023-05-06 14:26:06 阅读:175693 作者:3625

#-*-coding : utf-8-*-import OS,globimportcv2from scipy.ioimportloadmatfromcollectionsimportdefaultdictimportimportnumportimpon objectifydefvbb _ an NO2 dict (vbb _ file, 从cam_id(: ) os.path.basename到路径的最后一部分(“文件名.扩展名”)从os.path.splitext到文件名filename=OS.path.splitext oadmat ) vbb_file ) # object info in each frame: id,pos,occlusion, 定义lock,posvobjlists=vbb [ ' a ' ] [0] [0] [1] [0] obj LBL=[ str [0] ] forvinvbb [ ' a ' ] [0] [0] [0] personindexperson _ index _ list=NP.where (NP.array (obj LBL )=='person ' ) ) # 仅选择类别为“‘person”的xmlforframe objinenumerate (obj lists ) :iflen ) obj ) 0:frame_name=str ) cam_id ) _ 03360=defaultdict(list ) annos [ frame _ name ] [ ' id ' ]=frame _ name annos [ frame _ name ] [ ' label ' ]=' person ' force ' formatlabstartfrom1not0ifnotidinperson _ index _ list 3360 # onlyusebboxwhoselabelispersoncontinuepos=pos [0].to list (annos [ frame _ name ] [ ' bbox ' ].append [ pos ] if not annos [ frame _ name ] [ ' box ' ] : delannos [ frame _ name ] cam_id ) : cap=cv2.video capture (seq _ file ) index=1# capturedframelistv _ id=OS.path.splitext ) OS.path.splitext cap _ frames _ index=NP.sort ([ int (OS.OS.path.path.spath ) ) ) . read(print(ret ) if ret : ifnotindexincap _ frames _ index 3360 index=1continueifnotos.path.exists 3360 OS.makedinde str ) cam_id ) v_id ) _str ) index ).str(index ) ) cv2.imwrite ) outname,frame ) height,width,_=frameshape else: break index += 1 img_size = (width, height) return img_sizedef instance2xml_base(anno, bbox_type='xyxy'): """bbox_type: xyxy (xmin, ymin, xmax, ymax); xywh (xmin, ymin, width, height)""" assert bbox_type in ['xyxy', 'xywh'] E = objectify.ElementMaker(annotate=False) anno_tree = E.annotation( E.folder('VOC2014_instance/person'), E.filename(anno['id']), E.source( E.database('Caltech pedestrian'), E.annotation('Caltech pedestrian'), E.image('Caltech pedestrian'), E.url('None') ), E.size( E.width(640), E.height(480), E.depth(3) ), E.segmented(0), ) for index, bbox in enumerate(anno['bbox']): bbox = [float(x) for x in bbox] if bbox_type == 'xyxy': xmin, ymin, w, h = bbox xmax = xmin+w ymax = ymin+h else: xmin, ymin, xmax, ymax = bbox E = objectify.ElementMaker(annotate=False) anno_tree.append( E.object( E.name(anno['label']), E.bndbox( E.xmin(xmin), E.ymin(ymin), E.xmax(xmax), E.ymax(ymax) ), E.difficult(0), E.occlusion(anno["occlusion"][index]) ) ) return anno_treedef parse_anno_file(vbb_inputdir,vbb_outputdir): # annotation sub-directories in hda annotation input directory assert os.path.exists(vbb_inputdir) sub_dirs = os.listdir(vbb_inputdir) #对应set00,set01... for sub_dir in sub_dirs: print ("Parsing annotations of camera: ", sub_dir) cam_id = sub_dir #获取某一个子set下面的所有vbb文件 vbb_files = glob.glob(os.path.join(vbb_inputdir, sub_dir, "*.vbb")) for vbb_file in vbb_files: #返回一个vbb文件中所有的帧的标注结果 annos = vbb_anno2dict(vbb_file, cam_id) if annos: #组成xml文件的存储文件夹,形如“/Users/chenguanghao/Desktop/Caltech/xmlresult/” vbb_outdir = vbb_outputdir #如果不存在 if not os.path.exists(vbb_outdir): os.makedirs(vbb_outdir) for filename, anno in sorted(annos.items(), key=lambda x: x[0]): if "bbox" in anno: anno_tree = instance2xml_base(anno) outfile = os.path.join(vbb_outdir, os.path.splitext(filename)[0]+".xml") print ("Generating annotation xml file of picture: ", filename) #生成最终的xml文件,对应一张图片 etree.ElementTree(anno_tree).write(outfile, pretty_print=True) def visualize_bbox(xml_file, img_file): import cv2 tree = etree.parse(xml_file) # load image image = cv2.imread(img_file) origin = cv2.imread(img_file) # 获取一张图片的所有bbox for bbox in tree.xpath('//bndbox'): coord = [] for corner in bbox.getchildren(): coord.append(int(float(corner.text))) print (coord) cv2.rectangle(image, (coord[0], coord[1]), (coord[2], coord[3]), (0, 0, 255), 2) # visualize image cv2.imshow("test", image) cv2.imshow('origin', origin) cv2.waitKey(0)def main(): vbb_inputdir = "C:/Users/dell/Desktop/annotations" vbb_outputdir = "C:/Users/dell/Desktop/annotations" parse_anno_file(vbb_inputdir, vbb_outputdir) """ 下面这段是测试代码 """ """ xml_file = "/Users/chenguanghao/Desktop/Caltech/xmlresult/set07/bbox/set07_V000_4.xml" img_file = "/Users/chenguanghao/Desktop/Caltech/JPEG/set07/V000/4.jpg" visualize_bbox(xml_file, img_file) """if __name__ == "__main__": main()

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。