python 比较两个文件夹中的文件名(Annotation标注完全检查)

原因

Annotations和JPEGS的文件夹,有部分图片的标注缺失。

代码
import os
def compare_folders(folder1, folder2):
    # files1 = set(os.listdir(folder1))
    # files2 = set(os.listdir(folder2))

    files1 = set(os.path.splitext(file)[0] for file in os.listdir(folder1))
    files2 = set(os.path.splitext(file)[0] for file in os.listdir(folder2))
    unique_files = files1.symmetric_difference(files2)

    return unique_files


folder1 = './Annotations'
folder2 = './Object_split'

unique_files = compare_folders(folder1, folder2)

def get_files_with_extension(folder, extension):
    files = []
    for file in os.listdir(folder):
        if file.endswith(extension):
            files.append(file)
    return files
print(len(unique_files))
# for file in unique_files:
#     print(file)


for file in get_files_with_extension(folder1, ".xml"):
    if os.path.splitext(file)[0] in unique_files:
        print(file)

for file in get_files_with_extension(folder2, ".jpg"):
    if os.path.splitext(file)[0] in unique_files:
        print(file)

python中使用Set函数可以清楚list中的重复项
splitext分割文件名
endswith获取拓展名
symmetric_difference进行list比较

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个实现将labelme标注的json文件转换为yolo格式的xml文件的Python代码。该代码会遍历指定的文件夹的所有json文件,并将其转换为与原文件名相同的xml文件,并将xml文件保存在指定的新文件夹。对于shape_type为circle的标注,本代码会将其转换为外切矩形。 ```python import os import json import math from xml.etree.ElementTree import Element, SubElement, tostring from xml.dom.minidom import parseString def json_to_xml(json_path, xml_path): with open(json_path, 'r') as f: data = json.load(f) # 获取图像的尺寸 image_width = data['imageWidth'] image_height = data['imageHeight'] # 创建XML根节点 root = Element('annotation') # 创建XML子节点并填充图像信息 SubElement(root, 'folder').text = os.path.dirname(json_path) SubElement(root, 'filename').text = os.path.basename(json_path).split('.')[0] + '.jpg' SubElement(root, 'path').text = json_path size_node = SubElement(root, 'size') SubElement(size_node, 'width').text = str(image_width) SubElement(size_node, 'height').text = str(image_height) SubElement(size_node, 'depth').text = '3' # 处理每个标注 for shape in data['shapes']: label = shape['label'] if shape['shape_type'] == 'circle': # 获取圆的外切矩形 x1 = shape['points'][0][0] - shape['points'][1][1] y1 = shape['points'][0][1] - shape['points'][1][1] x2 = shape['points'][0][0] + shape['points'][1][1] y2 = shape['points'][0][1] + shape['points'][1][1] else: # 获取多边形的外切矩形 x1 = min(point[0] for point in shape['points']) y1 = min(point[1] for point in shape['points']) x2 = max(point[0] for point in shape['points']) y2 = max(point[1] for point in shape['points']) # 将标注信息写入XML文件 object_node = SubElement(root, 'object') SubElement(object_node, 'name').text = label bndbox_node = SubElement(object_node, 'bndbox') SubElement(bndbox_node, 'xmin').text = str(x1) SubElement(bndbox_node, 'ymin').text = str(y1) SubElement(bndbox_node, 'xmax').text = str(x2) SubElement(bndbox_node, 'ymax').text = str(y2) # 保存XML文件 with open(xml_path, 'w') as f: xml_string = parseString(tostring(root)).toprettyxml() f.write(xml_string) # 指定原标注文件夹和新标注文件夹 json_folder = 'path/to/json/folder' xml_folder = 'path/to/xml/folder' # 遍历原标注文件夹的所有json文件 for json_file in os.listdir(json_folder): if json_file.endswith('.json'): json_path = os.path.join(json_folder, json_file) xml_path = os.path.join(xml_folder, os.path.splitext(json_file)[0] + '.xml') json_to_xml(json_path, xml_path) ``` 请注意,在本代码处理shape_type为circle的标注时,是将其转换为外切矩形,而不是真正的矩形。如果需要将其转换为真正的矩形,可以根据圆的半径计算出其左上角和右下角的坐标。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值