728x90
반응형
https://pypi.org/project/polygon-pascalvoc-writer/
polygon-pascalvoc-writer
For generating Pascal VOC XML image annotation files
pypi.org
응용 코드
import glob
from tqdm import tqdm
import os
import json
import xmltodict
import cv2
import codecs
import numpy as np
from shutil import copyfile
from polygon_pascalvoc_writer import VocWriter
CLASS_label = ['CLASS_A', 'CLASS_B', ...]
#https://blog.actorsfit.com/a?ID=01750-6e1ed359-f624-468b-88ee-2cb557b10625
def get_files(path):
files = glob.glob(path+"/*")
gt_list = [file for file in files if file.endswith(".json")]
img_lst = [file for file in files if file.endswith((".png", ".tif", ".tiff", ".TIF", ".png", ".jpeg", ".jpg", ".PNG", ".JPG"))]
return sorted(gt_list), sorted(img_lst)
def get_files_from_file(f_path):
f = open(f_path, "r")
img_lst = f.readlines()
img_lst = [val.rstrip("\n") for val in img_lst]
json_lst = [os.path.splitext(img_nm)[0]+".json" for img_nm in img_lst]
return sorted(json_lst), sorted(img_lst)
def make_Detection(gtfileList, imgfileList, target_path, saved_path, saved_img_path):
for gtfile, imgfile in tqdm(zip(gtfileList, imgfileList)):
filename = gtfile.split("/")[-1][:-5]
imgname = imgfile.split("/")[-1]
# VocWriter
# saved_img_path : "dir/images/"
# saved_path : "dir/annotations/"
writer = VocWriter(saved_img_path, saved_path, imgname, database="DATABASENAME")
if filename != '.'.join(imgname.split(".")[:-1]):
print("different file name between img, gt")
import pdb; pdb.set_trace()
with open(gtfile, "r", encoding="utf-8-sig") as json_file:
json_data = json.load(json_file)
for multi in json_data["shapes"]:
label = multi["label"]
if label not in CLASS_label:
print("%s:%s"%(label, gtfile))
import pdb; pdb.set_trace()
box_name = label
points = np.array(multi["points"])
# Polygon bounding box + Also bndbox
polygon = points.tolist()
writer.addPolygon(box_name, polygon)
writer.save()
if __name__ == "__main__":
target_path = "DATA_PATH"
saved_dir = "SAVED_PATH"
for purpose in ["train", "val", "test"]:
saved_path = "./%s/%s/xml/"%(saved_dir, purpose)
saved_img_path = "./%s/%s/images/"%(saved_dir, purpose)
os.makedirs(saved_path, exist_ok=True)
os.makedirs(saved_img_path, exist_ok=True)
lst_f = "./%s/%s.txt"%(saved_dir, purpose)
gtfileList, imgfileList = get_files_from_file(lst_f)
make_Detection(gtfileList, imgfileList, target_path, saved_path, saved_img_path)
for img_path in imgfileList:
copyfile(img_path.rstrip("\n"), saved_img_path+os.path.basename(img_path))
728x90
반응형
'머신러닝 > Computer Vision' 카테고리의 다른 글
[matplotlib] hatches, pattern (0) | 2023.04.03 |
---|---|
[Computer Vision] Data set(image) Normalize value 계산(mean, std) (0) | 2023.03.20 |
[Labelme2COCO] (0) | 2023.02.23 |
[PIL] 이미지 위에 반투명 도형 그리기, 글자 쓰기 / Drawing transparent Polygon on image (2) | 2023.02.15 |
[cv2] Python image crop (0) | 2023.01.19 |
728x90
반응형
https://pypi.org/project/polygon-pascalvoc-writer/
polygon-pascalvoc-writer
For generating Pascal VOC XML image annotation files
pypi.org
응용 코드
import glob
from tqdm import tqdm
import os
import json
import xmltodict
import cv2
import codecs
import numpy as np
from shutil import copyfile
from polygon_pascalvoc_writer import VocWriter
CLASS_label = ['CLASS_A', 'CLASS_B', ...]
#https://blog.actorsfit.com/a?ID=01750-6e1ed359-f624-468b-88ee-2cb557b10625
def get_files(path):
files = glob.glob(path+"/*")
gt_list = [file for file in files if file.endswith(".json")]
img_lst = [file for file in files if file.endswith((".png", ".tif", ".tiff", ".TIF", ".png", ".jpeg", ".jpg", ".PNG", ".JPG"))]
return sorted(gt_list), sorted(img_lst)
def get_files_from_file(f_path):
f = open(f_path, "r")
img_lst = f.readlines()
img_lst = [val.rstrip("\n") for val in img_lst]
json_lst = [os.path.splitext(img_nm)[0]+".json" for img_nm in img_lst]
return sorted(json_lst), sorted(img_lst)
def make_Detection(gtfileList, imgfileList, target_path, saved_path, saved_img_path):
for gtfile, imgfile in tqdm(zip(gtfileList, imgfileList)):
filename = gtfile.split("/")[-1][:-5]
imgname = imgfile.split("/")[-1]
# VocWriter
# saved_img_path : "dir/images/"
# saved_path : "dir/annotations/"
writer = VocWriter(saved_img_path, saved_path, imgname, database="DATABASENAME")
if filename != '.'.join(imgname.split(".")[:-1]):
print("different file name between img, gt")
import pdb; pdb.set_trace()
with open(gtfile, "r", encoding="utf-8-sig") as json_file:
json_data = json.load(json_file)
for multi in json_data["shapes"]:
label = multi["label"]
if label not in CLASS_label:
print("%s:%s"%(label, gtfile))
import pdb; pdb.set_trace()
box_name = label
points = np.array(multi["points"])
# Polygon bounding box + Also bndbox
polygon = points.tolist()
writer.addPolygon(box_name, polygon)
writer.save()
if __name__ == "__main__":
target_path = "DATA_PATH"
saved_dir = "SAVED_PATH"
for purpose in ["train", "val", "test"]:
saved_path = "./%s/%s/xml/"%(saved_dir, purpose)
saved_img_path = "./%s/%s/images/"%(saved_dir, purpose)
os.makedirs(saved_path, exist_ok=True)
os.makedirs(saved_img_path, exist_ok=True)
lst_f = "./%s/%s.txt"%(saved_dir, purpose)
gtfileList, imgfileList = get_files_from_file(lst_f)
make_Detection(gtfileList, imgfileList, target_path, saved_path, saved_img_path)
for img_path in imgfileList:
copyfile(img_path.rstrip("\n"), saved_img_path+os.path.basename(img_path))
728x90
반응형
'머신러닝 > Computer Vision' 카테고리의 다른 글
[matplotlib] hatches, pattern (0) | 2023.04.03 |
---|---|
[Computer Vision] Data set(image) Normalize value 계산(mean, std) (0) | 2023.03.20 |
[Labelme2COCO] (0) | 2023.02.23 |
[PIL] 이미지 위에 반투명 도형 그리기, 글자 쓰기 / Drawing transparent Polygon on image (2) | 2023.02.15 |
[cv2] Python image crop (0) | 2023.01.19 |