ultralytics / yolov5

YOLOv5 πŸš€ in PyTorch > ONNX > CoreML > TFLite
https://docs.ultralytics.com
GNU Affero General Public License v3.0
49.67k stars 16.11k forks source link

How to reduce the size of label and fontsize #13199

Open Kelly02140 opened 2 months ago

Kelly02140 commented 2 months ago

Search before asking

Question

Hello, I am trying to reduce the labeling size since my images are only 240*240 and I could not see clearly without doing this. However, I have read that some others have posted this question before and I could not find neither 'plot_one_box' nor 'box_label'. Could anyone help me with it? image

Additional

No response

github-actions[bot] commented 2 months ago

πŸ‘‹ Hello @Kelly02140, thank you for your interest in YOLOv5 πŸš€! Please visit our ⭐️ Tutorials to get started, where you can find quickstart guides for simple tasks like Custom Data Training all the way to advanced concepts like Hyperparameter Evolution.

If this is a πŸ› Bug Report, please provide a minimum reproducible example to help us debug it.

If this is a custom training ❓ Question, please provide as much information as possible, including dataset image examples and training logs, and verify you are following our Tips for Best Training Results.

Requirements

Python>=3.8.0 with all requirements.txt installed including PyTorch>=1.8. To get started:

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

Environments

YOLOv5 may be run in any of the following up-to-date verified environments (with all dependencies including CUDA/CUDNN, Python and PyTorch preinstalled):

Status

YOLOv5 CI

If this badge is green, all YOLOv5 GitHub Actions Continuous Integration (CI) tests are currently passing. CI tests verify correct operation of YOLOv5 training, validation, inference, export and benchmarks on macOS, Windows, and Ubuntu every 24 hours and on every commit.

Introducing YOLOv8 πŸš€

We're excited to announce the launch of our latest state-of-the-art (SOTA) object detection model for 2023 - YOLOv8 πŸš€!

Designed to be fast, accurate, and easy to use, YOLOv8 is an ideal choice for a wide range of object detection, image segmentation and image classification tasks. With YOLOv8, you'll be able to quickly and accurately detect objects in real-time, streamline your workflows, and achieve new levels of accuracy in your projects.

Check out our YOLOv8 Docs for details and get started with:

pip install ultralytics
glenn-jocher commented 2 months ago

@Kelly02140 hello,

Thank you for reaching out! To adjust the label size and font size in YOLOv5, you'll need to modify the plot_one_box function, which is responsible for drawing the bounding boxes and labels on the images. This function is located in the utils/plots.py file.

Here's a step-by-step guide to help you:

  1. Locate the plot_one_box function: Open the utils/plots.py file and find the plot_one_box function.

  2. Modify the font size: Inside the plot_one_box function, you can adjust the font size by modifying the cv2.FONT_HERSHEY_SIMPLEX parameters. Look for the line that sets the font scale and thickness, which typically looks like this:

    font_scale = 0.5
    font_thickness = 1

    You can reduce these values to make the text smaller. For example:

    font_scale = 0.3
    font_thickness = 1
  3. Adjust the label size: The label size is also influenced by the rectangle drawn around the text. You can adjust the size of this rectangle by modifying the padding around the text. Look for the following lines:

    p1 = (int(x1), int(y1))
    p2 = (int(x1 + w), int(y1 - h - 3))

    You can adjust the w and h values to change the size of the label box.

Here is an example of how you might modify the plot_one_box function:

def plot_one_box(x, img, color=None, label=None, line_thickness=None):
    # ... existing code ...

    # Font scale and thickness
    font_scale = 0.3  # Adjust this value
    font_thickness = 1  # Adjust this value

    # ... existing code ...

    if label:
        # Adjust the label size
        (w, h), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
        p2 = (int(x1 + w), int(y1 - h - 3))
        cv2.rectangle(img, p1, p2, color, -1, cv2.LINE_AA)  # filled
        cv2.putText(img, label, (int(x1), int(y1 - 2)), cv2.FONT_HERSHEY_SIMPLEX, font_scale, [225, 255, 255], font_thickness, cv2.LINE_AA)

    # ... existing code ...

After making these changes, save the file and run your script again to see the updated label sizes and font sizes.

If you encounter any issues or if the problem persists, please ensure you are using the latest version of YOLOv5 by pulling the latest changes from the repository. If the issue is still reproducible, feel free to provide additional details, and we'll be happy to assist further.

Kelly02140 commented 2 months ago

@Kelly02140 hello,

Thank you for reaching out! To adjust the label size and font size in YOLOv5, you'll need to modify the plot_one_box function, which is responsible for drawing the bounding boxes and labels on the images. This function is located in the utils/plots.py file.

Here's a step-by-step guide to help you:

  1. Locate the plot_one_box function: Open the utils/plots.py file and find the plot_one_box function.
  2. Modify the font size: Inside the plot_one_box function, you can adjust the font size by modifying the cv2.FONT_HERSHEY_SIMPLEX parameters. Look for the line that sets the font scale and thickness, which typically looks like this:

    font_scale = 0.5
    font_thickness = 1

    You can reduce these values to make the text smaller. For example:

    font_scale = 0.3
    font_thickness = 1
  3. Adjust the label size: The label size is also influenced by the rectangle drawn around the text. You can adjust the size of this rectangle by modifying the padding around the text. Look for the following lines:

    p1 = (int(x1), int(y1))
    p2 = (int(x1 + w), int(y1 - h - 3))

    You can adjust the w and h values to change the size of the label box.

Here is an example of how you might modify the plot_one_box function:

def plot_one_box(x, img, color=None, label=None, line_thickness=None):
    # ... existing code ...

    # Font scale and thickness
    font_scale = 0.3  # Adjust this value
    font_thickness = 1  # Adjust this value

    # ... existing code ...

    if label:
        # Adjust the label size
        (w, h), baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, font_scale, font_thickness)
        p2 = (int(x1 + w), int(y1 - h - 3))
        cv2.rectangle(img, p1, p2, color, -1, cv2.LINE_AA)  # filled
        cv2.putText(img, label, (int(x1), int(y1 - 2)), cv2.FONT_HERSHEY_SIMPLEX, font_scale, [225, 255, 255], font_thickness, cv2.LINE_AA)

    # ... existing code ...

After making these changes, save the file and run your script again to see the updated label sizes and font sizes.

If you encounter any issues or if the problem persists, please ensure you are using the latest version of YOLOv5 by pulling the latest changes from the repository. If the issue is still reproducible, feel free to provide additional details, and we'll be happy to assist further.

I am sorry but I can't find the 'plot_one_box' function in plot.py

glenn-jocher commented 2 months ago

Hello @Kelly02140,

Thank you for your patience! If you are unable to locate the plot_one_box function in the utils/plots.py file, it might be due to changes in the file structure or function names in different versions of YOLOv5. Let's ensure you are using the latest version of YOLOv5 and guide you through the process again.

  1. Update YOLOv5: First, make sure you have the latest version of YOLOv5. You can update your local repository by running:

    git pull
  2. Locate the Drawing Function: In the latest versions, the function responsible for drawing bounding boxes and labels might be named differently or located in a different file. You can search for the function by looking for keywords like cv2.putText or cv2.rectangle in the utils directory. Here’s a command to help you search:

    grep -rnw 'utils/' -e 'cv2.putText'
  3. Modify the Font and Label Size: Once you locate the correct function, you can adjust the font size and label size as described earlier. For example, if the function is named annotate_image or something similar, you can follow the same steps to modify the font scale and thickness.

If you still have trouble finding the function, please provide the version of YOLOv5 you are using, and we can give more specific guidance.

Feel free to reach out with any further questions or details, and we'll be happy to assist you! 😊