Ограничивающие рамки из аннотаций LabelImg смещаются при отображении с помощью OpenCV в Python.Python

Программы на Python
Anonymous
Ограничивающие рамки из аннотаций LabelImg смещаются при отображении с помощью OpenCV в Python.

Сообщение Anonymous »

Я работаю над проектом по визуализации аннотаций, сделанных с помощью LabelImg к набору изображений. Однако когда я показываю эти изображения с аннотированными ограничивающими рамками с помощью OpenCV в Python, ограничивающие рамки кажутся постоянно смещенными от их правильного положения.
Я проверил, что координаты, напечатанные из файлы JSON верны и соответствуют аннотациям, как показано в LabelImg. Но когда эти координаты используются для рисования ограничивающих рамок на изображениях, происходит заметное смещение.
Вот образец данных JSON из LabelImg:

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

[
{
"image": "rtu.JPG",
"annotations": [
{
"label": "fan",
"coordinates": {
"x": 608.6441717791412,
"y": 243.26993865030676,
"width": 135.0,
"height": 108.0
}
}
]
}
]
И скриншот изнутри инструмента labellmg с правильными координатами вокруг веера.
Изображение
А вот код Python, который я использую для загрузки изображений и рисования ограничивающих рамок:

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

import os
import json
import numpy as np
import cv2
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import load_img, img_to_array

# Define the directory path
directory = r'C:\Users\data\ahu'

# Function to load data with annotations
def load_data_with_annotations(src_dir):
images = []
annotations = []

for file in os.listdir(src_dir):
if file.endswith('.json'):
json_path = os.path.join(src_dir, file)
with open(json_path, 'r') as f:
annotation_data = json.load(f)

for annotation in annotation_data:
img_file = annotation['image']
img_path = os.path.join(src_dir, img_file)

if not os.path.exists(img_path):
print(f"Image {img_file} not found.")
continue

# Load image
img = load_img(img_path)
img_array = img_to_array(img)
img_cv2 = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)

# Store images and annotations
images.append(img_cv2)
annotations.append(annotation['annotations'])

return images, annotations

images, annotations = load_data_with_annotations(directory)

# Display images with annotations
def display_images_with_annotations(images, annotations):
num_images = len(images)
for i in range(num_images):
original_img = images[i]

plt.figure(figsize=(10, 10))

# Display the original image with annotations
plt.subplot(1, 1, 1)
if annotations[i]:
for item in annotations[i]:
label = item['label']
x = int(item['coordinates']['x'])
y = int(item['coordinates']['y'])
width = int(item['coordinates']['width'])
height = int(item['coordinates']['height'])

# Debugging: Print the coordinates and label
print(f"Image {i+1} - Label: {label}, Coordinates: (x={x}, y={y}, width={width}, height={height})")

cv2.rectangle(original_img, (x, y), (x + width, y + height), (0, 255, 0), 2)
cv2.putText(original_img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

plt.imshow(cv2.cvtColor(original_img, cv2.COLOR_BGR2RGB))
plt.title(f"Annotated Image {i+1}")
plt.axis('off')

plt.show()

display_images_with_annotations(images, annotations)
И где-то в моем коде аннотация смещается примерно на 40 пикселей в направлениях X и Y. Любые советы, которые можно попробовать, очень ценятся. Этот фрагмент снимка экрана ниже отображается в ячейке блокнота IPython с помощью opencv.
Изображение


Подробнее здесь: https://stackoverflow.com/questions/786 ... h-opencv-i

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