Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

annotation type #12784

Open
1 task done
faridamousa opened this issue May 17, 2024 · 5 comments
Open
1 task done

annotation type #12784

faridamousa opened this issue May 17, 2024 · 5 comments
Labels
question Further information is requested

Comments

@faridamousa
Copy link

Search before asking

Question

if i have point annotation in json
will it work with yolov8 or does the annotation file have to be in txt?
can yolov8 even work with point annotation or does it have to be bounding box?
i am using yolov8 for object detection

Additional

if i have point annotation in json

@faridamousa faridamousa added the question Further information is requested label May 17, 2024
@glenn-jocher
Copy link
Member

@faridamousa hello! For YOLOv8 with object detection tasks, you'll primarily use bounding box annotations rather than point annotations. The annotation file format should generally be in YAML or plain text format. JSON is not directly supported for this purpose.

If your annotations are in JSON containing point data, you'll need to convert these into bounding box formats that typically consist of x_center, y_center, width, and height relative to image size. Here’s a quick Python snippet on how you might convert your point annotations to a compatible format:

# Example to convert a single point to a bounding box
def point_to_bbox(x, y, box_size=10):
    x1 = x - box_size // 2
    y1 = y - box_size // 2
    return [x1, y1, box_size, box_size]

# Assuming 'x' and 'y' are your point coordinates
bbox = point_to_bbox(x, y)
print("Bounding Box:", bbox)

This will create a small box around the point. Adjust box_size as necessary for your dataset.

For further conversion and usage, consider using the robust tools and documents provided by Ultralytics, or if it's a single common scenario, manually prepare the dataset to match the required input structure for YOLOv8. Happy detecting! 😊

@faridamousa
Copy link
Author

when i normalize the bounding boxes, the bounding boxes move from their correct places, what should i do?

@glenn-jocher
Copy link
Member

Hello @faridamousa,

It sounds like there might be an issue with the normalization process. Ensure that you are correctly dividing the bounding box coordinates by the image dimensions. Here’s a quick example:

# Assuming bbox is [x1, y1, x2, y2] and img_width, img_height are the dimensions of your image
x1, y1, x2, y2 = bbox
x1 /= img_width
y1 /= img_height
x2 /= img_width
y2 /= img_height
normalized_bbox = [x1, y1, x2, y2]

Make sure to apply this normalization consistently across all bounding boxes. If the issue persists, double-check the image dimensions and ensure they match the ones used during normalization.

If you need further assistance, feel free to ask! 😊

@faridamousa
Copy link
Author

i did that but when i visualize them, they appear as line on top of each other
here is my code for visualization:
def visualize_yolo_annotations(image_dir, label_dir):
image_files = glob.glob(os.path.join(image_dir, '*.png'))

for image_file in image_files:
    image = cv2.imread(image_file)
    img_height, img_width, _ = image.shape
    label_file = os.path.join(label_dir, f"{os.path.splitext(os.path.basename(image_file))[0]}.txt")

    if os.path.exists(label_file):
        with open(label_file, 'r') as f:
            lines = f.readlines()

        for line in lines:
            parts = line.strip().split()
            category_id = int(parts[0])
            x_center = float(parts[1]) * img_width
            y_center = float(parts[2]) * img_height
            width = float(parts[3]) * img_width
            height = float(parts[4]) * img_height

            x_min = int(x_center - width / 2)
            y_min = int(y_center - height / 2)
            x_max = int(x_center + width / 2)
            y_max = int(y_center + height / 2)

            # Draw the bounding box
            cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
            cv2.putText(image, str(category_id), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)

        # Convert image to RGB (matplotlib expects RGB images)
        image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

        # Plot image with bounding boxes using matplotlib
        plt.figure(figsize=(10, 10))
        plt.imshow(image_rgb)
        plt.title(os.path.basename(image_file))
        plt.axis('off')  # Turn off axis for cleaner visualization
        plt.show()

@glenn-jocher
Copy link
Member

Hello @faridamousa,

Thank you for sharing your code snippet. It looks like you're on the right track with visualizing the bounding boxes. The issue you're describing, where the boxes appear as lines on top of each other, might be due to incorrect scaling or coordinate calculations.

Here are a few suggestions to troubleshoot and fix this:

  1. Check Normalization: Ensure that the bounding box coordinates are correctly normalized. The values should be between 0 and 1 before scaling them back to the image dimensions.

  2. Verify Coordinates: Double-check the calculations for x_min, y_min, x_max, and y_max. Here’s a slightly modified version of your code to ensure the calculations are correct:

    def visualize_yolo_annotations(image_dir, label_dir):
        image_files = glob.glob(os.path.join(image_dir, '*.png'))
    
        for image_file in image_files:
            image = cv2.imread(image_file)
            img_height, img_width, _ = image.shape
            label_file = os.path.join(label_dir, f"{os.path.splitext(os.path.basename(image_file))[0]}.txt")
    
            if os.path.exists(label_file):
                with open(label_file, 'r') as f:
                    lines = f.readlines()
    
                for line in lines:
                    parts = line.strip().split()
                    category_id = int(parts[0])
                    x_center = float(parts[1]) * img_width
                    y_center = float(parts[2]) * img_height
                    width = float(parts[3]) * img_width
                    height = float(parts[4]) * img_height
    
                    x_min = int(x_center - width / 2)
                    y_min = int(y_center - height / 2)
                    x_max = int(x_center + width / 2)
                    y_max = int(y_center + height / 2)
    
                    # Draw the bounding box
                    cv2.rectangle(image, (x_min, y_min), (x_max, y_max), (0, 255, 0), 2)
                    cv2.putText(image, str(category_id), (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36, 255, 12), 2)
    
                # Convert image to RGB (matplotlib expects RGB images)
                image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    
                # Plot image with bounding boxes using matplotlib
                plt.figure(figsize=(10, 10))
                plt.imshow(image_rgb)
                plt.title(os.path.basename(image_file))
                plt.axis('off')  # Turn off axis for cleaner visualization
                plt.show()
  3. Debugging: Add print statements to check the values of x_center, y_center, width, and height before and after scaling. This will help ensure that the values are as expected.

If the issue persists, feel free to share more details, and we can further investigate. Happy coding! 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants