- 抽取coco格式训练集中的一部分作为子训练集,用快速测试算法
from pycocotools.coco import COCO
import random
import copy
import json
original_annotations_path = '/workspace/datasets/soccer6/yolo/annotations/train.json'
new_annotations_path = './mini_train.json'
coco_original = COCO(original_annotations_path)
all_image_ids = coco_original.getImgIds()
selected_image_ids = random.sample(all_image_ids, 1000)
coco_new = copy.deepcopy(coco_original)
coco_new.dataset['images'] = [img for img in coco_original.dataset['images'] if img['id'] in selected_image_ids]
coco_new.dataset['annotations'] = [ann for ann in coco_original.dataset['annotations'] if ann['image_id'] in selected_image_ids]
image_id_mapping = {old_id: new_id for new_id, old_id in enumerate(selected_image_ids, start=1)}
annotation_id_mapping = {ann['id']: i + 1 for i, ann in enumerate(coco_new.dataset['annotations'])}
for img in coco_new.dataset['images']:
img['id'] = image_id_mapping[img['id']]
for ann in coco_new.dataset['annotations']:
ann['image_id'] = image_id_mapping[ann['image_id']]
ann['id'] = annotation_id_mapping[ann['id']]
coco_new_path = new_annotations_path
with open(coco_new_path, 'w') as f:
json.dump(coco_new.dataset, f)
- 测试新保存的json文件格式是否正确
from pycocotools.coco import COCO
import cv2
import os
coco_data_path = '/workspace/datasets/soccer6/yolo/'
annotations_file_path = './mini_train.json'
coco = COCO(annotations_file_path)
output_directory = './output'
os.makedirs(output_directory, exist_ok=True)
for image_id in coco.getImgIds():
image_info = coco.loadImgs(image_id)[0]
image_path = coco_data_path + '/images/' + image_info['file_name']
image = cv2.imread(image_path)
ann_ids = coco.getAnnIds(imgIds=image_id)
annotations = coco.loadAnns(ann_ids)
for annotation in annotations:
bbox = annotation['bbox']
category_id = annotation['category_id']
category_name = coco.loadCats(category_id)[0]['name']
if category_name == 'person':
cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
(int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])), (255, 0, 0), 2)
cv2.putText(image, category_name, (int(bbox[0]), int(bbox[1]) - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
else:
cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
(int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])), (0, 0, 255), 2)
cv2.putText(image, category_name, (int(bbox[0]), int(bbox[1]) - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
output_path = os.path.join(output_directory, f'annotated_{image_info["file_name"]}')
cv2.imwrite(output_path, image)
- 读取指定图片的标注
from pycocotools.coco import COCO
import cv2
coco_data_path = '/workspace/datasets/soccer6/yolo/'
coco = COCO('/workspace/datasets/soccer6/yolo/annotations/train.json')
image_filename = 'groundball_373.jpg'
image_info = [img for img in coco.dataset['images'] if img['file_name'] == image_filename][0]
image_path = coco_data_path + '/images/' + image_info['file_name']
image = cv2.imread(image_path)
ann_ids = coco.getAnnIds(imgIds=image_info['id'])
annotations = coco.loadAnns(ann_ids)
for annotation in annotations:
bbox = annotation['bbox']
category_id = annotation['category_id']
category_name = coco.loadCats(category_id)[0]['name']
cv2.rectangle(image, (int(bbox[0]), int(bbox[1])),
(int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3])), (255, 0, 0), 2)
cv2.putText(image, category_name, (int(bbox[0]), int(bbox[1]) - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 0), 2)
output_path = 'annotated_image.jpg'
cv2.imwrite(output_path, image)