最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實現(xiàn)的批量分析xml標簽中各個類別個數(shù)功能示例

 更新時間:2019年12月30日 11:00:07   作者:團長sama  
這篇文章主要介紹了python實現(xiàn)的批量分析xml標簽中各個類別個數(shù)功能,涉及Python針對xml文件的遍歷、讀取、解析等相關(guān)操作技巧,需要的朋友可以參考下

本文實例講述了python實現(xiàn)的批量分析xml標簽中各個類別個數(shù)功能。分享給大家供大家參考,具體如下:

文章目錄

需要個腳本分析下各個目標的數(shù)目 順帶練習下多進程,自用,直接上代碼:

# -*- coding: utf-8 -*-
# @Time  : 2019/06/10 18:56
# @Author : TuanZhangSama
import os
import xml.etree.ElementTree as ET
from multiprocessing import Pool,freeze_support,cpu_count
import imghdr
import logging
def get_all_xml_path(xml_dir:str,filter=['.xml']):
  #遍歷文件夾下所有xml
  result=[]
  #maindir是當前搜索的目錄 subdir是當前目錄下的文件夾名 file是目錄下文件名
  for maindir,subdir,file_name_list in os.walk(xml_dir):
    for filename in file_name_list:
      ext=os.path.splitext(filename)[1]#返回擴展名
      if ext in filter:
        result.append(os.path.join(maindir,filename))
  return result
def analysis_xml(xml_path:str):
  tree=ET.parse(xml_path)
  root=tree.getroot()
  result_dict={}
  for obj in root.findall('object'):
    obj_name = obj.find('name').text
    obj_num=result_dict.get(obj_name,0)+1
    result_dict[obj_name]=obj_num
  if imghdr.what(xml_path.replace('.xml','.jpg')) != 'jpeg':
    print(xml_path.replace('.xml','.jpg'),'is worng')
    # logging.info(xml_path.replace('.xml','.jpg'))
  if is_valid_jpg(xml_path.replace('.xml','.jpg')):
    pass
  return result_dict
def analysis_xmls_batch(xmls_path_list:list):
  result_list=[]
  for i in xmls_path_list:
    result_list.append(analysis_xml(i))
  return result_list
def collect_result(result_list:list):
  all_result_dict={}
  for result_dict in result_list:
    for key,values in result_dict.items():
      obj_num=all_result_dict.get(key,0)+values
      all_result_dict[key]=obj_num
  return all_result_dict
def main(xml_dir:str,result_save_path:str =None):
  r'''根據(jù)xml文件統(tǒng)計所有樣本的數(shù)目.對于文件不完整的圖片和有xml但無圖片的樣本,直接進行刪除.默認跑滿所有的cpu核心
  Parameters
  ----------
  xml_dir : str
    xml所在的文件夾.用的遞歸形式,因此只需保證xml在此目錄的子目錄下即可.對應(yīng)的圖片和其xml要在同一目錄
  result_save_path : str
    分析結(jié)果的日志保存路徑.默認 None 無日志
  '''
  if result_save_path is not None:
    assert isinstance(result_save_path,str),'{} is illegal path'.format(result_save_path)
  else:
    logging.basicConfig(filename=result_save_path,filemode='w',level=logging.INFO)
  freeze_support()#windows 上用
  xmls_path=get_all_xml_path(xml_dir)
  worker_num=cpu_count()
  print('your CPU num is',cpu_count())
  length=float(len(xmls_path))/float(worker_num)
  #計算下標,盡可能均勻地劃分輸入文件的列表
  indices=[int(round(i*length)) for i in range(worker_num+1)]
  #生成每個進程要處理的子文件列表
  sublists=[xmls_path[indices[i]:indices[i+1]] for i in range(worker_num)]
  pool=Pool(processes=worker_num)
  all_process_result_list=[]
  for i in range(worker_num):
    all_process_result_list.append(pool.apply_async(analysis_xmls_batch,args=(sublists[i],)))
  pool.close()
  pool.join()
  print('analysis done!')
  _temp_list=[]
  for i in all_process_result_list:
    _temp_list=_temp_list+i.get()
  result=collect_result(_temp_list)
  logging.info(result)
  print(result)
def is_valid_jpg(jpg_file):
  """判斷JPG文件下載是否完整   """
  if not os.path.exists(jpg_file):
    print(jpg_file,'is not existes')
    os.remove(jpg_file.replace('.jpg','.xml'))
  with open(jpg_file, 'rb') as fr:
    fr.seek(-2, 2)
    if fr.read() == b'\xff\xd9':
      return True
    else:
      os.remove(jpg_file)
      os.remove(jpg_file.replace('.jpg','.xml'))
      print(jpg_file)
      logging.error(jpg_file,'is imperfect img')
      return False
if __name__=='__main__':
  test_dir='/home/chiebotgpuhq/Share/winshare/origin'
  save_path='/home/chiebotgpuhq/MyCode/python/pytorch/mmdetection-master/result.log'
  main(test_dir,save_path)

PS:這里再為大家提供幾款關(guān)于xml操作的在線工具供大家參考使用:

在線XML/JSON互相轉(zhuǎn)換工具:
http://tools.jb51.net/code/xmljson

在線格式化XML/在線壓縮XML
http://tools.jb51.net/code/xmlformat

XML在線壓縮/格式化工具:
http://tools.jb51.net/code/xml_format_compress

XML代碼在線格式化美化工具:
http://tools.jb51.net/code/xmlcodeformat

更多關(guān)于Python相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Python操作xml數(shù)據(jù)技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python Socket編程技巧總結(jié)》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進階經(jīng)典教程》及《Python文件與目錄操作技巧匯總

希望本文所述對大家Python程序設(shè)計有所幫助。

相關(guān)文章

  • Django數(shù)據(jù)庫遷移常見使用方法

    Django數(shù)據(jù)庫遷移常見使用方法

    這篇文章主要介紹了Django數(shù)據(jù)庫遷移常見使用方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習或者工作具有一定的參考學(xué)習價值,需要的朋友可以參考下
    2020-11-11
  • 基于Python實現(xiàn)語音合成小工具

    基于Python實現(xiàn)語音合成小工具

    TTS(Text To Speech)是一種語音合成技術(shù),可以讓機器將輸入文本以語音的方式播放出來,實現(xiàn)機器說話的效果。本文將使用pyttsx3庫作為示范,編寫一個語音合成小工具,感興趣的可以了解一下
    2022-12-12
  • Python的加密模塊md5、sha、crypt使用實例

    Python的加密模塊md5、sha、crypt使用實例

    這篇文章主要介紹了Python的加密模塊md5、sha、crypt使用實例,本文給出了MD5和crypt模塊的代碼實例,需要的朋友可以參考下
    2014-09-09
  • Pytorch dataloader在加載最后一個batch時卡死的解決

    Pytorch dataloader在加載最后一個batch時卡死的解決

    這篇文章主要介紹了Pytorch dataloader在加載最后一個batch時卡死的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-05-05
  • 如何在python中使用selenium的示例

    如何在python中使用selenium的示例

    這篇文章主要介紹了如何在python中使用selenium的示例,selenium提供了一個通用的接口,可模擬用戶來操作瀏覽器,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • python批量讀取txt文件為DataFrame的方法

    python批量讀取txt文件為DataFrame的方法

    下面小編就為大家分享一篇python批量讀取txt文件為DataFrame的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-04-04
  • Python實現(xiàn)ssh批量登錄并執(zhí)行命令

    Python實現(xiàn)ssh批量登錄并執(zhí)行命令

    本篇文章主要是介紹了Python實現(xiàn)ssh批量登錄并執(zhí)行命令,有一些任務(wù)可以進行批量完成,Python就可以完成,有需要的同學(xué)可以了解一下。
    2016-10-10
  • 使用Python實現(xiàn)不同需求的排行榜功能

    使用Python實現(xiàn)不同需求的排行榜功能

    這篇文章主要為大家介紹了Python實現(xiàn)不同需求的排行榜功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • python數(shù)據(jù)可視化JupyterLab實用擴展程序Mito

    python數(shù)據(jù)可視化JupyterLab實用擴展程序Mito

    這篇文章主要為大家介紹了python數(shù)據(jù)可視化JupyterLab實用擴展程序Mito的功能應(yīng)用示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助
    2021-11-11
  • Python matplotlib如何刪除subplots中多余的空白子圖

    Python matplotlib如何刪除subplots中多余的空白子圖

    這篇文章主要介紹了Python matplotlib如何刪除subplots中多余的空白子圖問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評論

舟曲县| 四平市| 凌源市| 西丰县| 水富县| 唐海县| 会昌县| 张家口市| 安庆市| 济阳县| 青河县| 神农架林区| 普宁市| 上高县| 深圳市| 叶城县| 邵阳市| 庆城县| 柏乡县| 萍乡市| 汕尾市| 正宁县| 布拖县| 秀山| 荔浦县| 额敏县| 苍南县| 沂南县| 翁源县| 婺源县| 南雄市| 阿城市| 香港 | 澎湖县| 合肥市| 镇平县| 楚雄市| 吴旗县| 察雅县| 图木舒克市| 建昌县|