#Tools#VOC xml annotation 解析


import numpy as np 
import pandas as pd 
import copy
import os
import sys
import glob
import cv2
try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
    print("xml.etree.ElementTree imported failed") 


def parse_voc(input_dir,out_file):
    """
    func:
        parse voc_xml  
    
    params:
        input_dir:directory of VOC Format annotations
        out_file: txt file

    """
    #gain all xml file in current dir
    file_list = glob.glob(input_dir+'**/*xml')
    # print(file_list)
    rep = r'\anno'
    # print(file_list)
    with open(out_file,'w') as fo:
        for voc_xml in file_list:
            print(voc_xml)
            
            # open xml document
            xml_tree = ET.parse(voc_xml)
            # print(type(xml_tree))
            tree_root = xml_tree.getroot()

            ##file_path
            file_path = tree_root.find('path').text
            print(file_path)
            path_info =file_path.split('\\')[-2:]
            file_path = os.path.join(os.path.join(rep,path_info[0]),path_info[1])
            print(file_path)
            # sys.exit()

            ##file_name
            file_name = tree_root.find('filename').text
            
            ##object
            hand_pos = tree_root.find('object')

            gesture = hand_pos.find("name").text
            print(gesture)
            
            bbox = hand_pos.find("bndbox")
            xmin = bbox.find("xmin").text
            ymin = bbox.find("ymin").text
            xmax = bbox.find("xmax").text
            ymax = bbox.find("ymax").text
            
            fo.write(','.join([file_path,xmin,ymin,xmax,ymax,gesture])+'\n' )
            # loc=[xmin,ymin,xmax,ymax]
            # print(loc)

def corp_handgesture(input_file,out_file):
    """
    func:
        crop hand from origin pic
    params:
        file_path: file list contain  imagepath and bbox loc and label
    """
    with open(input_file,'r') as fi ,open(out_file,'w') as fo:
        for line in fi.readlines():
            info_list = line.strip().split(',')
            img_path = info_list[0]
            
        
            bbox = [int(v) for v in info_list[1:-1]]
            print(line)
            print(bbox)
            
           
            # if info_list[-1]=="hold":
            if info_list[-1]=="bottle" or info_list[-1]=='phone':
                
                img = cv2.imread(img_path)
                
                # wi,he,_ = img.shape
                he,wi,_ = img.shape
                print(wi,he)
                print(img.shape)
                top = bbox[1]
                buttom = bbox[3]
                left = bbox[0]
                right = bbox[2]
                
                left = 0 if left<0 else left
                top = 0 if top<0 else top
                right = wi if right>=wi else right
                buttom = he if buttom>=he else buttom
                print(top,buttom,left,right)
                
                region = img[int(top):int(buttom), int(left):int(right)]
                # cv2.imshow('11',region)
                # cv2.waitKey(-1)
                resized = cv2.resize(region,(128,128))
                # cv2.imshow('11',resized)
                # cv2.waitKey(-1)
                
                ###path save resized pic
                img_dir ,img_name = os.path.split(img_path)
                new_path = img_dir.replace("a","b")
                # print(new_path)
                # print(img_name)
                if not os.path.exists(new_path):
                    os.makedirs(new_path)
                new_imgpath = os.path.join(new_path,img_name)
                print(new_imgpath)
                # sys.exit()
                if cv2.imwrite(os.path.join(new_path,img_name),resized):
                    print("save Success!",os.path.join(new_path,img_name))
                
                fo.write(os.path.split(img_path)[-1]+' 1\n')
                


def create_similar(xml_dir,out_xml_dir):
    """
    func:
        parse current xml and gain similar picture's bounding box
    params:
        in_xml:     The benchmark xml file
        # pic_dir:    directory contain all image to be dealed
        out_xml_dir:The path where to save new xml file
    """
    ##gain file_list
    xml_list = glob.glob(xml_dir+"*xml")
    # print(xml_list)
    #parse xml_tree
    for in_xml in xml_list:
        xml_tree = ET.parse(in_xml)
        tree_root = xml_tree.getroot()
        file_path = tree_root.find('path').text

        pic_dir = os.path.split(file_path)[0]
    
        for root,dirs,files in os.walk(pic_dir):
            for each in files:
                if '.jpeg' in each:
                    print(each)
                    new_xml_tree = copy.deepcopy(xml_tree)
                    # print(type(new_xml_tree))
                    new_xml_tree.write(out_xml_dir+'\\'+each.split('.')[0]+'.xml')

            

def change_xml_elem(xml_dir):
    """
    func:
        parse current xml and gain similar picture's bounding box
    params:
        in_xml:     The benchmark xml file
       
       
    """
    ##gain file_list
    xml_list = glob.glob(xml_dir+"*xml")
    # print(xml_list)
    #parse xml_tree
    for in_xml in xml_list:
        xml_tree = ET.parse(in_xml)
        tree_root = xml_tree.getroot()
        file_path = tree_root.find('path').text
        folder = tree_root.find('folder').text
        tree_root.find('path').text = os.path.join( r'g4',os.path.split(file_path)[1])
        tree_root.find('folder').text = 'g4'
        print(file_path,folder)
        out_xml = xml_dir.replace('final_anno','anno')+in_xml.split('\\')[-1]
        xml_tree.write(out_xml)
    
        # for root,dirs,files in os.walk(pic_dir):
        #     for each in files:
        #         if '.jpeg' in each:
        #             print(each)
        #             new_xml_tree = copy.deepcopy(xml_tree)
        #             # print(type(new_xml_tree))
        #             new_xml_tree.write(out_xml_dir+'\\'+each.split('.')[0]+'.xml') 



if __name__ == "__main__":
    input_dir = r"\anno"
   
    out_file = r"1.txt"
    out_file_label =r'1.txt'
    
    corp_handgesture(out_file,out_file_label)



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值