查找同时包含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'))