|
import cv2 |
|
import matplotlib.pyplot as plt |
|
import os |
|
|
|
def check_yolo_annotations(image_dir, label_dir): |
|
""" |
|
Checks if the conversion from VOC to YOLO format is correct by plotting bounding boxes on the images. |
|
|
|
Parameters: |
|
image_dir (str): Directory path to the images. |
|
label_dir (str): Directory path to the YOLO format annotations. |
|
""" |
|
|
|
image_files = sorted(os.listdir(image_dir)) |
|
first_image_file = image_files[0] |
|
|
|
|
|
image_path = os.path.join(image_dir, first_image_file) |
|
label_path = os.path.join(label_dir, os.path.splitext(first_image_file)[0] + '.txt') |
|
|
|
|
|
image = cv2.imread(image_path) |
|
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
|
|
|
with open(label_path, 'r') as f: |
|
lines = f.readlines() |
|
|
|
|
|
for line in lines: |
|
|
|
class_id, x_center, y_center, width, height = map(float, line.strip().split()) |
|
img_height, img_width, _ = image.shape |
|
|
|
|
|
x_center *= img_width |
|
y_center *= img_height |
|
width *= img_width |
|
height *= img_height |
|
|
|
|
|
x1 = int(x_center - width / 2) |
|
y1 = int(y_center - height / 2) |
|
x2 = int(x_center + width / 2) |
|
y2 = int(y_center + height / 2) |
|
|
|
|
|
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2) |
|
|
|
|
|
plt.imshow(image) |
|
plt.axis('off') |
|
plt.show() |
|
|
|
if __name__ == "__main__": |
|
check_yolo_annotations('Dataset/images', 'Dataset/yolo_annotations') |
|
|