2020/6/21
Hey,拖更了两天,非常抱歉,不过最近这两天终于解决了一个困扰了我很久的难题,那就是如何将人体关键点数据集xml标注格式转换成json格式。也许是因为我第一次这样处理标注文件,又或是因为这个数据集的规模达到了与coco数据集类似的程度,总之为了编写这个转换的脚本我用了很多时间。
Xml的格式:
json格式采用的是crowdpose数据集类似的格式具体可以参考:https://github.com/Jeff-sjtu/CrowdPose
实现思路:首先将数据集的图片重命名,并与xml文件一一对应,然后获取xml文件中的相关内容,放入到json文件中。代码如下:
import xml.etree.ElementTree as ET # 读取xml。
import os
import random
import json
KEY_POINT_NAME_LIST = ['Nose', 'Chest', 'Right-shoulder', 'Right-elbow', 'Right-wrist',
'Left-shoulder', 'Left-elbow', 'Left-wrist', 'Right-hip','Right-knee',
'Right-ankle', 'Left-hip', 'Left-knee', 'Left-ankle']
K_P_CONNETCTION = [[0, 1], [1, 5], [1, 2], [5, 6], [6, 7], [2, 3], [3, 4], [1, 11], [1, 8],
[11, 12], [8, 9], [12, 13], [9, 10]]
basepath = '/Users/sqx/Downloads/HIE/xml/'
xmlname = os.listdir(basepath)
random_xml = random.sample(xmlname, 10000)
# print(xmlname)
n = len(random_xml)
# print(n)
# print(xmlname)
random_xml.sort(key=lambda x: int(x[:-4]))
# print(xmlname)
# print(xmlname)
annotations = {}
objects = []
img_dir = []
image = []
annotations["images"] = []
annotations["annotation"] = []
annotations["categories"] = []
for xml in random_xml:
filename = os.path.join(basepath, xml)
# print(filename)
# print(xml)
tree = ET.parse(filename) # 解析读取xml函数
# print(tree)
id = os.path.splitext(xml)[0]
imgname = os.path.join(id + ".jpg" )
print(imgname)
for image_size in tree.findall('size'):
image_size_scale = {}
image_size_scale['height'] = image_size.find('height').text
image_size_scale['width'] = image_size.find('width').text
# image.append(image_size_scal
height = image_size_scale['height']
width = image_size_scale["width"]
#
# font = cv2.FONT_HERSHEY_SIMPLEX
#
# imgname = tree.find('filename').text
# imgnumber = os.path.splitext(imgname)[0]
# imgnumber = imgnumber.zfill(6)
# imgname = os.path.join(imgnumber + '.jpg')
# print(imgname)
# id = os.path.splitext(imgname)[0]
# print(id)
# print(height)
# print(width)
annotations["images"].append(
{
"file_name": imgname,
"id": id,
"height": height,
"width": width,
"crowdIndex": 0.0
}
)
for obj in tree.findall('object'):
obj_struct = {}
person_keypoints = []
keypoints = []
personbbox = []
object_value = []
key_point = []
obj_struct['name'] = obj.find('name').text
obj_struct['pose'] = obj.find('pose').text
obj_struct['truncated'] = int(obj.find('truncated').text)
obj_struct['difficult'] = int(obj.find('difficult').text)
bbox = obj.find('bndbox')
personbbox = [int(bbox.find('xmin').text),
int(bbox.find('ymin').text),
int(bbox.find('xmax').text),
int(bbox.find('ymax').text)]
for i, item in enumerate(bbox):
if i > 3:
keypoint_value = item.text
key_point = []
key_point.append(int(keypoint_value.split(',')[0]))
key_point.append(int(keypoint_value.split(',')[1]))
key_point.append(int(keypoint_value.split(',')[2]))
# print(key_point)
object_value.extend(key_point)
print(object_value)
person_keypoints = [
str(bbox.find('Nose').text),
str(bbox.find('Chest').text),
str(bbox.find('Right-shoulder').text),
str(bbox.find('Right-elbow').text),
str(bbox.find('Right-wrist').text),
str(bbox.find('Left-shoulder').text),
str(bbox.find('Left-elbow').text),
str(bbox.find('Left-wrist').text),
str(bbox.find('Right-hip').text),
str(bbox.find('Right-knee').text),
str(bbox.find('Right-ankle').text),
str(bbox.find('Left-hip').text),
str(bbox.find('Left-knee').text),
str(bbox.find('Left-ankle').text)
]
count = 0
number_keypoints = 0
for i, item in enumerate(person_keypoints):
itemlist = item.split(",")
if int(itemlist[2]) == -1:
count = count + 1
number_keypoints = 14 - count
annotations["annotation"].append(
{
"number_keypoints": number_keypoints,
"keypooints": object_value,
"image_id": id,
"bbox": personbbox,
"category_id": 1
}
)
annotations["categories"].append(
{
"supercategory": "person",
"id": 1,
"name": "person",
"keypoints": KEY_POINT_NAME_LIST,
"skeleton": K_P_CONNETCTION
}
)
json_path = "/Users/sqx/Downloads/train_json/val.json"
with open(json_path, "w") as f:
json.dump(annotations, f)
print("saved")
经过对这次数据格式的转换,收获了很多,对于数据集,python语言,以及ACM竞赛也有了更深刻的认识。
今天看到了一则新闻‘九月份以前高校毕业生再次返校可能性很小’下半年就研三的我瑟瑟发抖。希望能早日返校,用上实验室的电脑,现在我们只能用远程服务器做实验,师兄给推荐的这家环境都是配置好了的,几乎不用担心环境的问题,直接就可以跑实验,有同样需求的朋友可以参考:智星云官网: http://www.ai-galaxy.cn/,淘宝店:https://shop36573300.taobao.com/公众号: 智星AI,
前两天又发烧了,身处北京不敢耽搁,赶紧去做了核酸检测,没有中招,就是普通感冒。今天终于好差不多了,下午的时候听说有日环食,两幅墨镜戴在一起去看太阳,还真是能看到太阳缺了一块,不过很快就没有了,主要是时间长了眼睛也受不了,放张人家的图,纪念一下。
假如生活欺骗了你
假如生活欺骗了你,
不要悲伤,不要心急!
忧郁的日子里需要镇静:
相信吧,快乐的日子将会来临!
心儿永远向往着未来;
现在却常是忧郁。
一切都是瞬息,一切都将会过去;
而那过去了的,就会成为亲切的怀恋。
——普希金
PEACE
参考资料: