Last active
July 1, 2021 08:01
-
-
Save Nannigalaxy/8985f74335bb02a6d03ca201306ab3bd to your computer and use it in GitHub Desktop.
VOC dataset manipulation scripts. XML to CSV, Rename labels, Resizes Images.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
import cv2 as cv | |
import gc | |
from tqdm import tqdm | |
import shutil | |
from PIL import Image | |
import xml.etree.ElementTree as ET | |
def rename_label(): | |
xml_list = [] | |
anno_path = '../input/B_mask/train/annotations/' | |
img_path = '../input/B_mask/train/images/' | |
anno_dst = '../input/B_mask/renamed/train/annotations/' | |
img_dst = '../input/B_mask/renamed/train/images/' | |
os.makedirs(anno_dst) | |
os.makedirs(img_dst) | |
f_name = os.listdir(anno_path) | |
for xml_file in tqdm(f_name): | |
tree = ET.parse(os.path.join(anno_path,xml_file)) | |
img_file = xml_file.replace('xml','jpg') | |
root = tree.getroot() | |
size = root.find('size') | |
xml = ['<annotation>'] | |
xml.append('<folder> images </folder>') | |
xml.append('<filename>'+img_file+'</filename>') | |
xml.extend(('<size>', | |
'<width>'+str(size.find('width').text)+'</width>', | |
'<height>'+str(size.find('height').text)+'</height>', | |
'<depth>'+str(size.find('depth').text)+'</depth>', | |
'</size>')) | |
for member in root.findall('object'): | |
bbx = member.find('bndbox') | |
# label = member.find('name').text | |
label = "face" | |
xml.extend(('<object>', | |
'<name>'+label+'</name>', | |
'<pose>Unspecified</pose>', | |
'<truncated>0</truncated>', | |
'<difficult>0</difficult>', | |
'<bndbox>', | |
'<xmin>'+bbx.find('xmin').text+'</xmin>', | |
'<ymin>'+bbx.find('ymin').text+'</ymin>', | |
'<xmax>'+bbx.find('xmax').text+'</xmax>', | |
'<ymax>'+bbx.find('ymax').text+'</ymax>', | |
'</bndbox>', | |
'</object>')) | |
xml.append('</annotation>') | |
res = '\n'.join(xml) | |
with open(anno_dst+xml_file, 'w') as f: | |
f.write(res) | |
shutil.copy(os.path.join(img_path,xml_file.replace('xml','jpg')), img_dst+img_file) | |
rename_label() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
import os | |
import cv2 as cv | |
import gc | |
from tqdm import tqdm | |
import shutil | |
from PIL import Image | |
import xml.etree.ElementTree as ET | |
def scale_image(img, factor=1): | |
return cv.resize(img,(int(img.shape[1]*factor), int(img.shape[0]*factor))) | |
def xml_to_xml(): | |
xml_list = [] | |
anno_path = '../input/our/train/annotations/' | |
img_path = '../input/our/train/images/' | |
anno_dst = '../input/our/new/annotations/' | |
img_dst = '../input/our/new/images/' | |
os.makedirs(anno_dst) | |
os.makedirs(img_dst) | |
f_name = os.listdir(anno_path) | |
for xml_file in tqdm(f_name): | |
tree = ET.parse(os.path.join(anno_path,xml_file)) | |
img_file = xml_file.replace('xml','jpg') | |
img = cv.imread(os.path.join(img_path,xml_file.replace('xml','jpg'))) | |
o_h, o_w, _ = img.shape | |
re_img = scale_image(img, 0.2) | |
h, w, _ = re_img.shape | |
root = tree.getroot() | |
xml = ['<annotation>'] | |
xml.append('<folder> images </folder>') | |
xml.append('<filename>'+img_file+'</filename>') | |
xml.extend(('<size>', | |
'<width>'+str(re_img.shape[1])+'</width>', | |
'<height>'+str(re_img.shape[0])+'</height>', | |
'<depth>'+str(re_img.shape[2])+'</depth>', | |
'</size>')) | |
for member in root.findall('object'): | |
bbx = member.find('bndbox') | |
xmin = int(bbx.find('xmin').text)/o_w | |
ymin = int(bbx.find('ymin').text)/o_h | |
xmax = int(bbx.find('xmax').text)/o_w | |
ymax = int(bbx.find('ymax').text)/o_h | |
label = member.find('name').text | |
xml.extend(('<object>', | |
'<name>'+label+'</name>', | |
'<pose>Unspecified</pose>', | |
'<truncated>0</truncated>', | |
'<difficult>0</difficult>', | |
'<bndbox>', | |
'<xmin>'+str(int(xmin*w))+'</xmin>', | |
'<ymin>'+str(int(ymin*h))+'</ymin>', | |
'<xmax>'+str(int(xmax*w))+'</xmax>', | |
'<ymax>'+str(int(ymax*h))+'</ymax>', | |
'</bndbox>', | |
'</object>')) | |
xml.append('</annotation>') | |
res = '\n'.join(xml) | |
with open(anno_dst+xml_file, 'w') as f: | |
f.write(res) | |
cv.imwrite(img_dst+img_file, re_img) | |
xml_to_xml() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import glob | |
import pandas as pd | |
from tqdm import tqdm | |
import xml.etree.ElementTree as ET | |
def xml_to_csv(path,f_name): | |
xml_list = [] | |
for xml_file in tqdm(f_name): | |
tree = ET.parse(os.path.join(path,xml_file)) | |
root = tree.getroot() | |
for member in root.findall('object'): | |
bbx = member.find('bndbox') | |
xmin = int(bbx.find('xmin').text) | |
ymin = int(bbx.find('ymin').text) | |
xmax = int(bbx.find('xmax').text) | |
ymax = int(bbx.find('ymax').text) | |
label = member.find('name').text | |
value = (root.find('filename').text, | |
int(root.find('size')[0].text), | |
int(root.find('size')[1].text), | |
label, | |
xmin, | |
ymin, | |
xmax, | |
ymax | |
) | |
xml_list.append(value) | |
column_name = ['filename', 'width', 'height', | |
'classname', 'xmin', 'ymin', 'xmax', 'ymax'] | |
xml_df = pd.DataFrame(xml_list, columns=column_name) | |
return xml_df | |
def main(): | |
set_type = 'train' # train or validation | |
path = './wider/'+set_type+'/annotations' | |
datasets = os.listdir(path) | |
f_name = os.listdir(path) | |
xml_df = xml_to_csv(path,f_name) | |
xml_df.to_csv('labels_{}.csv'.format(set_type), index=None) | |
print('Successfully converted xml to csv.') | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment