Skip to content

Commit

Permalink
新增普通json转yolo格式代码
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaohangguo committed Jan 7, 2024
1 parent 79fbc1a commit a103aed
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 13 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,18 @@



## JSON2YOLO
## JSON2YOLO_ignore_0

直接执行即可,默认代码是放在COCOdataset 数据集根目录下执行。

程序会跳过标签为0的标签,然后导出标签会-1 ,如果不需要这部分功能需要调整。

这部分是针对公开数据集[SeaDronesSee](https://github.com/Ben93kie/SeaDronesSee)v2开发的。

https://seadronessee.cs.uni-tuebingen.de/dataset



## JSON2YOLO

直接执行即可,默认代码是放在COCOdataset 数据集根目录下执行。
15 changes: 3 additions & 12 deletions json2yolo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import json
import os
from PIL import Image
Expand All @@ -10,17 +9,11 @@ def process_annotations(json_file, images_folder, labels_folder):
data = json.load(file)

annotations = data['annotations']
ignore_count = 0

for annotation in tqdm(annotations):
category_id = annotation['category_id']
image_id = annotation['image_id']

# 跳过ignore标签
if category_id == 0:
ignore_count += 1
continue

# 获取图片尺寸
file_path = os.path.join(images_folder, str(image_id) + '.jpg')
try:
Expand All @@ -44,8 +37,6 @@ def process_annotations(json_file, images_folder, labels_folder):
with open(label_file_path, 'a') as file:
file.write(label_content + '\n')

return ignore_count

def create_labels_folder():
# 创建labels文件夹及其子文件夹
labels_folder = 'labels'
Expand All @@ -59,10 +50,10 @@ def main():
train_labels_folder, val_labels_folder = create_labels_folder()

# 处理训练和验证数据
ignore_count_train = process_annotations('annotations/instances_train.json', 'images/train', train_labels_folder)
ignore_count_val = process_annotations('annotations/instances_val.json', 'images/val', val_labels_folder)
process_annotations('annotations/instances_train.json', 'images/train', train_labels_folder)
process_annotations('annotations/instances_val.json', 'images/val', val_labels_folder)

print(f"完成处理。忽略的标签数量:训练集 = {ignore_count_train}, 验证集 = {ignore_count_val}")
print("完成处理。")

if __name__ == "__main__":
main()
68 changes: 68 additions & 0 deletions json2yolo_ignore_0.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@

import json
import os
from PIL import Image
from tqdm import tqdm

def process_annotations(json_file, images_folder, labels_folder):
# 读取JSON文件
with open(json_file, 'r') as file:
data = json.load(file)

annotations = data['annotations']
ignore_count = 0

for annotation in tqdm(annotations):
category_id = annotation['category_id']
image_id = annotation['image_id']

# 跳过ignore标签
if category_id == 0:
ignore_count += 1
continue

# 获取图片尺寸
file_path = os.path.join(images_folder, str(image_id) + '.jpg')
try:
with Image.open(file_path) as img:
width, height = img.size
except FileNotFoundError:
continue

# 计算归一化的坐标
bbox = annotation['bbox']
x_center = (bbox[0] + bbox[2] / 2) / width
y_center = (bbox[1] + bbox[3] / 2) / height
w_norm = bbox[2] / width
h_norm = bbox[3] / height

# 写入标签文件,标签编号减1
label = category_id - 1
label_content = f"{label} {x_center} {y_center} {w_norm} {h_norm}"

label_file_path = os.path.join(labels_folder, str(image_id) + '.txt')
with open(label_file_path, 'a') as file:
file.write(label_content + '\n')

return ignore_count

def create_labels_folder():
# 创建labels文件夹及其子文件夹
labels_folder = 'labels'
train_labels_folder = os.path.join(labels_folder, 'train')
val_labels_folder = os.path.join(labels_folder, 'val')
os.makedirs(train_labels_folder, exist_ok=True)
os.makedirs(val_labels_folder, exist_ok=True)
return train_labels_folder, val_labels_folder

def main():
train_labels_folder, val_labels_folder = create_labels_folder()

# 处理训练和验证数据
ignore_count_train = process_annotations('annotations/instances_train.json', 'images/train', train_labels_folder)
ignore_count_val = process_annotations('annotations/instances_val.json', 'images/val', val_labels_folder)

print(f"完成处理。忽略的标签数量:训练集 = {ignore_count_train}, 验证集 = {ignore_count_val}")

if __name__ == "__main__":
main()

0 comments on commit a103aed

Please sign in to comment.