-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7e87e06
commit 5bbcfba
Showing
2 changed files
with
156 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import glob | ||
import json | ||
import os | ||
from PIL import Image | ||
|
||
def yolo_to_coco_for_subset(yolo_images_folder, yolo_labels_folder, categories): | ||
# Initialize COCO dataset structure for the subset | ||
coco_format = { | ||
"images": [], | ||
"annotations": [], | ||
"categories": [] | ||
} | ||
|
||
# Add category information | ||
for i, category in enumerate(categories): | ||
coco_format["categories"].append({ | ||
"id": i + 1, | ||
"name": category, | ||
"supercategory": "none" | ||
}) | ||
|
||
image_id = 0 | ||
annotation_id = 0 | ||
|
||
for image_file in glob.glob(f"{yolo_images_folder}/*.jpg"): | ||
# Read image to get width and height | ||
with Image.open(image_file) as img: | ||
width, height = img.size | ||
|
||
# Add image information with size | ||
coco_format["images"].append({ | ||
"id": image_id + 1, | ||
"file_name": os.path.basename(image_file), | ||
"width": width, | ||
"height": height | ||
}) | ||
|
||
# Corresponding annotation file | ||
yolo_annotation_file = os.path.join(yolo_labels_folder, os.path.basename(image_file).replace(".jpg", ".txt")) | ||
if os.path.exists(yolo_annotation_file): | ||
with open(yolo_annotation_file, "r") as file: | ||
for line in file: | ||
category_id, x_center, y_center, bbox_width, bbox_height = map(float, line.split()) | ||
|
||
# Convert YOLO format to COCO format | ||
x_min = (x_center - bbox_width / 2) * width | ||
y_min = (y_center - bbox_height / 2) * height | ||
coco_bbox_width = bbox_width * width | ||
coco_bbox_height = bbox_height * height | ||
|
||
# Add annotation information | ||
coco_format["annotations"].append({ | ||
"id": annotation_id + 1, | ||
"image_id": image_id + 1, | ||
"category_id": int(category_id) + 1, | ||
"bbox": [x_min, y_min, coco_bbox_width, coco_bbox_height], | ||
"area": coco_bbox_width * coco_bbox_height, | ||
"segmentation": [], # Optional | ||
"iscrowd": 0 | ||
}) | ||
annotation_id += 1 | ||
image_id += 1 | ||
|
||
return coco_format | ||
|
||
def save_coco_format(coco_format, output_file): | ||
with open(output_file, "w") as file: | ||
json.dump(coco_format, file, indent=4) | ||
|
||
# Example usage | ||
yolo_base_folder = "/public/home/lvshuhang/pea_od/yolov8-main/mydata" | ||
categories = ["pea"] | ||
|
||
# Convert train set | ||
train_coco_format = yolo_to_coco_for_subset( | ||
os.path.join(yolo_base_folder, "images/train"), | ||
os.path.join(yolo_base_folder, "labels/train"), | ||
categories | ||
) | ||
save_coco_format(train_coco_format, "train_coco_format.json") | ||
|
||
# Convert val set | ||
val_coco_format = yolo_to_coco_for_subset( | ||
os.path.join(yolo_base_folder, "images/val"), | ||
os.path.join(yolo_base_folder, "labels/val"), | ||
categories | ||
) | ||
save_coco_format(val_coco_format, "val_coco_format.json") |