Skip to content

Commit af1958f

Browse files
committed
Fix edge cases
1 parent 166d85a commit af1958f

1 file changed

Lines changed: 6 additions & 4 deletions

File tree

  • cinema/segmentation/landmark

cinema/segmentation/landmark/eval.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,10 @@ def draw_landmarks(image: np.ndarray, metrics: dict[str, float], landmarks: list
4242
# draw predictions with blue cross
4343
pred_x, pred_y = metrics[f"pred_x{i}"], metrics[f"pred_y{i}"]
4444
pred_x, pred_y = int(pred_x), int(pred_y)
45-
x1, x2 = max(0, pred_x - 7), min(image.shape[0], pred_x + 8)
46-
y1, y2 = max(0, pred_y - 7), min(image.shape[1], pred_y + 8)
45+
pred_x = min(max(0, pred_x), image.shape[0] - 1)
46+
pred_y = min(max(0, pred_y), image.shape[1] - 1)
47+
x1, x2 = max(0, pred_x - 7), min(image.shape[0] - 1, pred_x + 8)
48+
y1, y2 = max(0, pred_y - 7), min(image.shape[1] - 1, pred_y + 8)
4749
if pred_y < 0 or pred_y >= image.shape[1] or pred_x < 0 or pred_x >= image.shape[0]:
4850
logger.error(f"Predicted landmark {i} out of bounds: ({pred_x}, {pred_y}), for image shape {image.shape}.")
4951

@@ -52,8 +54,8 @@ def draw_landmarks(image: np.ndarray, metrics: dict[str, float], landmarks: list
5254
# draw ground truth with red cross, slightly smaller to not overlap with pred
5355
true_x, true_y = metrics[f"true_x{i}"], metrics[f"true_y{i}"]
5456
true_x, true_y = int(true_x), int(true_y)
55-
x1, x2 = max(0, true_x - 5), min(image.shape[0], true_x + 6)
56-
y1, y2 = max(0, true_y - 5), min(image.shape[1], true_y + 6)
57+
x1, x2 = max(0, true_x - 5), min(image.shape[0] - 1, true_x + 6)
58+
y1, y2 = max(0, true_y - 5), min(image.shape[1] - 1, true_y + 6)
5759
image[true_x, y1:y2] = [255, 0, 0]
5860
image[x1:x2, true_y] = [255, 0, 0]
5961
out_path.parent.mkdir(exist_ok=True, parents=True)

0 commit comments

Comments
 (0)