使用selcetive search方法生成伪标签,并写入xml文件

使用selcetive search的方法生成伪标签,写入xml文件

# coding:utf-8
# selective search生成伪标签+修改xml文件
import os
import os.path
import xml.dom.minidom
import xml.etree.ElementTree as et
from PIL import Image
import skimage.data
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import selectivesearch
import numpy as np
import cv2
path = 'E:\\Datasets\OWOD\OWOD\VOC2007\Annotations/'
files = os.listdir(path)  # 得到文件夹下所有文件名称
s = []

for xmlFile in files:  # 遍历文件夹
    if not os.path.isdir(xmlFile):  # 判断是否是文件夹,不是文件夹才打开

        label = xmlFile.strip('.xml')
        image_path = 'E:\\Dataset\\VOC2007\\JPEGImages' + '/' + str(label) + '.jpg'
        # image = cv2.imread(image_path)
        img = Image.open(image_path)
        original_image = np.asarray(img)
        H, W = original_image.shape[:2]

        doc = et.parse(os.path.join(path, xmlFile))
        # root is <configuration>
        root_doc = doc.getroot()
        if original_image.shape[-1] != 3:
            doc.write('Annotations2' + '/' + xmlFile)
            print(xmlFile, 'error image')
            continue
        # 生成随机框操作
        img_lbl, regions = selectivesearch.selective_search(original_image, scale=500, sigma=0.9, min_size=200)
        candidates = set()
        for r in regions:
            if r['rect'] in candidates:  # 排除重复的候选区
                continue
            if r['size'] < 2000:  # 排除小于 2000 pixels的候选区域(并不是bounding box中的区域大小)
                continue
            x, y, w, h = r['rect']
            if w / h > 2 or h / w > 2:  # 排除扭曲的候选区域边框  即只保留近似正方形的
                continue
            candidates.add(r['rect'])
        search_region = {}
        i = 0
        for x, y, w, h in candidates:
            norm_x, norm_y, norm_w, norm_h = x / W, y / H, w / W, h / H
            search_region['selective_region_' + str(i)] = (x, y, x + w, y + h)
            i = i + 1
        # new '<selective_region>'
        ns = et.SubElement(root_doc, 'selective_region', attrib={})
        for j in range(len(search_region)):

            # add new '<bndbox>' in <object>
            nb = et.SubElement(ns, 'bndbox', attrib={})
            nxmin = et.SubElement(nb, 'xmin', attrib={})
            nxmin.text = str(search_region['selective_region_' + str(j)][0])

            nymin = et.SubElement(nb, 'ymin', attrib={})
            nymin.text = str(search_region['selective_region_' + str(j)][1])

            nxmax = et.SubElement(nb, 'xmax', attrib={})
            nxmax.text = str(search_region['selective_region_' + str(j)][2])

            nymax = et.SubElement(nb, 'ymax', attrib={})
            nymax.text = str(search_region['selective_region_' + str(j)][3])
            
            et.dump(ns)


        doc.write('Annotations2' + '/' + xmlFile)
        print(xmlFile,'OK')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值