Skip to content

Commit

Permalink
add spacing
Browse files Browse the repository at this point in the history
  • Loading branch information
Louis-Dupont committed Aug 10, 2023
1 parent 8b3d7de commit 958bfb2
Showing 1 changed file with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,8 @@
import numpy as np


def draw_label(image: np.ndarray, label: str, confidence: float) -> np.ndarray:
"""Draw a label and confidence on an image.
:param image: The image on which to draw the label and confidence, in RGB format, and Channel Last (H, W, C)
:param label: The label to draw.
:param confidence: The confidence of the label.
"""
def draw_label(image: np.ndarray, label: str, confidence: str) -> np.ndarray:
"""Draw a label and confidence on an image."""

# Format confidence as a percentage
confidence_str = f"{confidence * 100:.3f}%"
Expand All @@ -16,13 +12,16 @@ def draw_label(image: np.ndarray, label: str, confidence: float) -> np.ndarray:
fontScale = 0.8
thickness = 1

# Define additional spacing between the two lines
line_spacing = 5

# Determine the size of the label and confidence text
label_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, fontScale, thickness)[0]
confidence_size = cv2.getTextSize(confidence_str, cv2.FONT_HERSHEY_SIMPLEX, fontScale, thickness)[0]

# Determine the size of the bounding rectangle
text_width = max(label_size[0], confidence_size[0])
text_height = label_size[1] + confidence_size[1] + thickness * 3
text_height = label_size[1] + confidence_size[1] + thickness * 3 + line_spacing

# Calculate the position to draw the label, centered horizontally and at the top
start_x = (image.shape[1] - text_width) // 2
Expand All @@ -38,7 +37,7 @@ def draw_label(image: np.ndarray, label: str, confidence: float) -> np.ndarray:
alpha = 0.6
cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)

# Center the label and confidence text within the bounding rectangle
# Center the label and confidence text within the bounding rectangle, with additional spacing
text_color = (0, 0, 0) # Black
cv2.putText(
image,
Expand All @@ -53,7 +52,7 @@ def draw_label(image: np.ndarray, label: str, confidence: float) -> np.ndarray:
cv2.putText(
image,
confidence_str,
(start_x + (text_width - confidence_size[0]) // 2, start_y + label_size[1] + confidence_size[1] + thickness),
(start_x + (text_width - confidence_size[0]) // 2, start_y + label_size[1] + confidence_size[1] + thickness + line_spacing),
cv2.FONT_HERSHEY_SIMPLEX,
fontScale,
text_color,
Expand Down

0 comments on commit 958bfb2

Please sign in to comment.