查询并提取包含circle格式的json图片

查找同时包含polygon和circle的json图片

提取到另外的一个文件夹中

用于后续把circle转换成polygon(转换请看我下一篇博客)

代码在后面

import os
import json
import cv2
import numpy as np
import shutil

# json目录
fileDir = r'E:/ultralytics-main/train_biaozhi'
output_dir = r'C:/Users/yuanzebin/Desktop/new'


# 使用os.listdir()来列出目录中的所有文件
file_names = os.listdir(fileDir)

# 获取该目录下的.json 文件名称
json_files = [file for file in file_names if file.endswith('.json')]


# 遍历指定目录下的.JSON文件名称
for json_file in json_files:
    file_name = os.path.splitext(json_file)[0]

    # 检查对应的图片文件是否存在
    img_path = os.path.join(fileDir, f'{file_name }.jpg')
    if not os.path.exists(img_path):
        continue
    with open(os.path.join(fileDir, json_file), 'r', encoding='utf8') as fp:
        json_data = json.load(fp)

    # 创建需要返回的字典  my_dict{'分类名称:[point xy坐标]'}
    shaixuan = json_data['shapes']
    has_non_polygon = False
    for element in shaixuan:
        if 'shape_type' in element and element['shape_type'] != 'polygon':
            has_non_polygon = True
            break
    else:
        print("All elements have shape_type 'polygon'")
    if has_non_polygon:
        shutil.move(os.path.join(fileDir, json_file), os.path.join(output_dir, json_file))
        shutil.move(img_path, os.path.join(output_dir, f'{file_name}.jpg'))

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值