Augly не может увеличить набор данных COCOPython

Программы на Python
Ответить Пред. темаСлед. тема
Anonymous
 Augly не может увеличить набор данных COCO

Сообщение Anonymous »

У меня есть изображение, которое я хочу увеличить. Его разрешение - 8192x4230. Я назвал его этим инструментом и экспортировал аннотации в формате Coco. Теперь мне нужно передать это изображение и аннотации в функции Augly, но когда я это делаю, оно бросает мне эту ошибку < /p>

traceback (самый последний вызов последним):
file "c: \ users \ admin \ source \ repos \ augpy \ aug.py", line 114, in
apply_random_pathmentation (inpute_pathmentation (inpute_pathmentation (intovertaition_pathmentation \pyr count+1)
file "c: \ users \ admin \ source \ repos \ augpy \ aug.py", line 72, в Apply_random_augmentation
img = aug_func (img, bboxes, metadata)
file "c: \ users \ source \ repos \ augpy \ aug.py. imaugs.random_noise (x, bboxes = bboxes, bbox_format = 'coco', metadata = meta),
file "d: \ python39 \ lib \ site-packages \ augly \ image \ functional.py", строка 1981, in random_noise
right_metadata. "D: \ python39 \ lib \ site-packages \ augly \ image \ utils \ metadata.py", строка 190, в get_metadata
transform_bboxes (
file "d: \ python39 \ lib \ site-packages \ augly \ image \ utils \ metadata.py.py", line 128, in transform_bboxe \ image \ utils \ metadata.py. validate_and_normalize_bboxes (dst_bboxes, bbox_format, src_w, src_h)
file "d: \ python39 \ lib \ site-packages \ augly \ image \ utils \ metadata.py", строка 46, в validate_and_normale_bboxes
asserse (
assertion assertion. [569.0, 2868.0, 207.0, 264.0] является недействительным или не находится в формате Coco < /p>
< /blockquote>
в произвольном точке времени (то есть, это может быть брошено на 1 -й итерации по всему миру или в этом Advance. < /p>
код: < /p>

Код: Выделить всё

import os, random, copy, json
from PIL import Image
import augly.image.functional as imaugs
from pycocotools.coco import COCO

# List of augmentations
augmentations = [
lambda x, bboxes, meta: imaugs.blur(x, bboxes=bboxes, bbox_format='coco', metadata=meta, radius=random.uniform(0.5, 2.0)),
lambda x, bboxes, meta: imaugs.brightness(x, bboxes=bboxes, bbox_format='coco', metadata=meta, factor=random.uniform(0.7, 1.3)),
lambda x, bboxes, meta: imaugs.contrast(x, bboxes=bboxes, bbox_format='coco', metadata=meta, factor=random.uniform(0.7, 1.5)),
lambda x, bboxes, meta: imaugs.hflip(x, bboxes=bboxes, bbox_format='coco', metadata=meta),
lambda x, bboxes, meta: imaugs.vflip(x, bboxes=bboxes, bbox_format='coco', metadata=meta),
lambda x, bboxes, meta: imaugs.rotate(x, bboxes=bboxes, bbox_format='coco', metadata=meta, degrees=random.randint(-30, 30)),
lambda x, bboxes, meta: imaugs.saturation(x, bboxes=bboxes, bbox_format='coco', metadata=meta, factor=random.uniform(0.5, 1.5)),
lambda x, bboxes, meta: imaugs.scale(x, bboxes=bboxes, bbox_format='coco', metadata=meta, factor=random.uniform(0.7, 1.3)),
lambda x, bboxes, meta: imaugs.random_noise(x, bboxes=bboxes, bbox_format='coco', metadata=meta),
lambda x, bboxes, meta: imaugs.overlay_emoji(x, bboxes=bboxes, bbox_format='coco', metadata=meta, emoji_size=0.1),
lambda x, bboxes, meta:  imaugs.overlay_text(x, bboxes=bboxes, bbox_format='coco', metadata=meta, opacity=0.5),
]

# Annotation ID
ann_id = 0

# Calculate segmentation from bbox
def bbox_to_segmentation(bbox):
x, y, w, h = bbox
segmentation = [
[x, y,
x + w, y,
x + w, y + h,
x, y + h]
]
return segmentation

input_dir = "input_images"
output_dir = "augmented_images"
os.makedirs(output_dir, exist_ok=True)

annotation_file = input_dir + '/my_coco.json'
coco = COCO(annotation_file)

annotation_file_output = output_dir + '/my_coco.json'
coco_output = copy.deepcopy(coco.dataset)
annotation_template = copy.deepcopy(coco_output['annotations'][0])
img_template = copy.deepcopy(coco_output['images'][0])
coco_output['annotations'] = []
coco_output['images'] = []

image_id = 1 # Only one image
annotation_ids = coco.getAnnIds(imgIds=image_id)
annotations = coco.loadAnns(annotation_ids)

original_bboxes = [ann['bbox'] for ann in annotations]

input_images = [f for f in os.listdir(input_dir) if f.endswith(('.jpg', '.jpeg', '.png'))]
if not input_images:
raise FileNotFoundError("No images in input_images!")

def apply_random_augmentation(image_path, output_path, img_num):
global ann_id
img = Image.open(image_path)
num_augs = random.randint(1, 3)
bboxes = [[float(num) for num in bbox] for bbox in copy.deepcopy(original_bboxes)]

# Apply from 1 to 3 augs
for _ in range(num_augs):
aug_func = random.choice(augmentations)
metadata = []
img = aug_func(img, bboxes, metadata)
new_bboxes = [[int(x) for x in bbox] for bbox in metadata[0]['dst_bboxes']]
print(f'{img_num} {str(new_bboxes) == str(bboxes)}')

# Fill in resulting annotation
for bbox in new_bboxes:
new_annotation = copy.deepcopy(annotation_template)
new_annotation['area'] = bbox[2] * bbox[3]
new_annotation['segmentation'] = bbox_to_segmentation(bbox)
new_annotation['iscrowd'] = 0
new_annotation['bbox'] = bbox
new_annotation['id'] = ann_id + 1
new_annotation['image_id'] = img_num
new_annotation['category_id'] = coco.anns[(ann_id % len(coco.anns)) + 1]['category_id']
coco_output['annotations'].append(new_annotation)
ann_id+=1

# Fill in resulting image metainfo
new_img = copy.deepcopy(img_template)
new_img['id'] = img_num
new_img['file_name'] = output_path
new_img['height'] = img.height
new_img['width'] = img.width
coco_output['images'].append(new_img)

img.save(output_path)

total_augmentations = 100
aug_per_image = total_augmentations

count = 0
for img_name in input_images:
if count >= total_augmentations:
break
for i in range(aug_per_image):
if count >= total_augmentations:
break
input_path = os.path.join(input_dir, img_name)
output_path = os.path.join(output_dir, f"aug_{count+1}_{img_name}")
apply_random_augmentation(input_path, output_path, count+1)
count += 1

with open(annotation_file_output, "w") as f:
json.dump(coco_output, f, indent=4)
print("Done")
Примечание: этот код работает хорошо, если ни один не передается в виде метаданных для функций увеличения. Я также намеренно преобразовываю значения Bbox из INTS, чтобы плавать на всякий случай. По -видимому, нет никакого тега «Augly». Теперь вопрос в том, как заставить их работать правильно?


Подробнее здесь: https://stackoverflow.com/questions/796 ... ed-dataset
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «Python»