• 大小: 3KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-09
  • 语言: 其他
  • 标签: caffe-ssd  K-means  

资源简介

两个程序,一个程序从 xml 文件中批量获取标注目标的宽与高,另一个为 K-means 维度聚类程序(可视化绘图)

资源截图

代码片段和文件信息

# -*- coding:utf-8 -*-
import os
import xml.etree.ElementTree as ET
import numpy as np
np.set_printoptions(suppress=True threshold=np.nan)
import matplotlib
from PIL import Image
 
def parse_obj(xml_path filename):
  tree=ET.parse(xml_path+filename)
  objects=[]
  for obj in tree.findall(‘object‘):
    obj_struct={}
    obj_struct[‘name‘]=obj.find(‘name‘).text
    bbox=obj.find(‘bndbox‘)
    obj_struct[‘bbox‘]=[int(bbox.find(‘xmin‘).text)
                        int(bbox.find(‘ymin‘).text)
                         int(bbox.find(‘xmax‘).text)
                        int(bbox.find(‘ymax‘).text)]
    objects.append(obj_struct)
  return objects
 
 
def read_image(image_path filename):
  im=Image.open(image_path+filename)
  W=im.size[0]
  H=im.size[1]
  area=W*H
  im_info=[WHarea]
  return im_info
 
 
if __name__ == ‘__main__‘:
  image_path=‘./JPEGImages/‘
  xml_path=‘./Annotations/‘
  filenamess=os.listdir(xml_path)
  filenames=[]
  for name in filenamess:
    name=name.replace(‘.xml‘‘‘)
    filenames.append(name)
 # print(filenames)
  recs={}
  ims_info={}
  obs_shape={}
  classnames=[]
  num_objs={}
  obj_avg={}
  for iname in enumerate(filenames):
#    print(‘正在处理 {}.xml ‘.format(name))
    recs[name]=parse_obj(xml_path name+ ‘.xml‘ )
#    print(‘正在处理 {}.jpg ‘.format(name))
    ims_info[name]=read_image(image_path name + ‘.jpg‘ )
#  print(‘所有信息收集完毕。‘)
#  print(‘正在处理信息......‘)
  for name in filenames:
    im_w = ims_info[name][0]
    im_h = ims_info[name][1]
    im_area = ims_info[name][2]
    for object in recs[name]:
      if object[‘name‘] not in num_objs.keys():
         num_objs[object[‘name‘]]=1
      else:
         num_objs[object[‘name‘]]+=1
      #num_objs+=1
      ob_w = object[‘bbox‘][2] - object[‘bbox‘][0]
      ob_h = object[‘bbox‘][3] - object[‘bbox‘][1]
      print(ob_h)
      ob_area = ob_w*ob_h
      w_rate= ob_w / im_w
      h_rate= ob_h / im_h
      area_rate= ob_area / im_area
      if not object[‘name‘] in obs_shape.keys():
        obs_shape[object[‘name‘]]=( [[ob_w
                                           ob_h
                                           ob_area
                                           w_rate
                                           h_rate
                                           area_rate]] )
      else:
        obs_shape[object[‘name‘]].append( [ob_w
                                           ob_h
                                           ob_area
                                           w_rate
                                           h_rate
                                           area_rate] )
    if object[‘name‘] not in classnames:
        classnames.append(object[‘name‘])#求平均
 
  for name in classnames:
    obj_avg[name]=(np.array(obs_shape[name]).sum(axis=0))/num_objs[name]
#    print(‘{}的情况如下:*******\n‘.format(name))
#    print(‘  目标平均W={}‘.format(obj_avg[name][0]))
#    print(‘  目标平均H={}‘.format(obj_avg[name][1]))
#    print(‘  目标平均area={}‘.format(obj_avg[name][2]))
#

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        4325  2019-02-24 04:01  Caffe-SSD的K-means宽高比聚类\K-means.py
     文件        3516  2019-02-24 03:53  Caffe-SSD的K-means宽高比聚类\get_w_h.py
     目录           0  2019-02-25 09:18  Caffe-SSD的K-means宽高比聚类\

评论

共有 条评论