refactor(typing): Typing error has been fixed for draw and geometry files. (#2120)

This commit is contained in:
Kader Miyanyedi 2026-02-02 09:57:24 +03:00 committed by GitHub
parent dcce0e4807
commit 4c1f33189b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 18 deletions

View File

@ -232,11 +232,6 @@ module = [
"supervision.detection.utils.masks",
"supervision.detection.utils.polygons",
"supervision.detection.vlm",
"supervision.draw.base",
"supervision.draw.color",
"supervision.draw.utils",
"supervision.geometry.core",
"supervision.geometry.utils",
"supervision.key_points.annotators",
"supervision.key_points.core",
"supervision.key_points.skeletons",

View File

@ -1,6 +1,7 @@
from __future__ import annotations
from dataclasses import dataclass
from typing import Any, cast
import matplotlib.pyplot as plt
@ -53,7 +54,7 @@ LEGACY_COLOR_PALETTE = [
ROBOFLOW_COLOR_PALETTE = ["C28DFC", "A351FB", "8315F9", "6706CE", "5905B3", "4D049A"]
def _validate_color_hex(color_hex: str):
def _validate_color_hex(color_hex: str) -> None:
color_hex = color_hex.lstrip("#")
if not all(c in "0123456789abcdefABCDEF" for c in color_hex):
raise ValueError("Invalid characters in color hash")
@ -259,10 +260,10 @@ class Color:
def ROBOFLOW(cls) -> Color:
return Color.from_hex("#A351FB")
def __hash__(self):
def __hash__(self) -> int:
return hash((self.r, self.g, self.b))
def __eq__(self, other):
def __eq__(self, other: Any) -> bool:
return (
isinstance(other, Color)
and self.r == other.r
@ -423,5 +424,7 @@ def unify_to_bgr(color: tuple[int, int, int] | Color) -> tuple[int, int, int]:
Tuple[int, int, int]: The color in BGR format as a tuple of three integers.
"""
if issubclass(type(color), Color):
color = cast(Color, color)
return color.as_bgr()
color = cast(tuple[int, int, int], color)
return color

View File

@ -1,6 +1,7 @@
from __future__ import annotations
import os
from typing import cast
import cv2
import numpy as np
@ -312,26 +313,31 @@ def draw_image(
if not 0.0 <= opacity <= 1.0:
raise ValueError("Opacity must be between 0.0 and 1.0.")
rect_x = int(rect.x)
rect_y = int(rect.y)
rect_width = int(rect.width)
rect_height = int(rect.height)
# Validate rectangle dimensions
if (
rect.x < 0
or rect.y < 0
or rect.x + rect.width > scene.shape[1]
or rect.y + rect.height > scene.shape[0]
rect_x < 0
or rect_y < 0
or rect_x + rect_width > scene.shape[1]
or rect_y + rect_height > scene.shape[0]
):
raise ValueError("Invalid rectangle dimensions.")
# Resize and isolate alpha channel
image = cv2.resize(image, (rect.width, rect.height))
image = cv2.resize(image, (rect_width, rect_height))
image = cast(np.ndarray, image)
alpha_channel = (
image[:, :, 3]
if image.shape[2] == 4
else np.ones((rect.height, rect.width), dtype=image.dtype) * 255
else np.ones((rect_height, rect_width), dtype=image.dtype) * 255
)
alpha_scaled = cv2.convertScaleAbs(alpha_channel * opacity)
# Perform blending
scene_roi = scene[rect.y : rect.y + rect.height, rect.x : rect.x + rect.width]
scene_roi = scene[rect_y : rect_y + rect_height, rect_x : rect_x + rect_width]
alpha_float = alpha_scaled.astype(np.float32) / 255.0
blended_roi = cv2.convertScaleAbs(
(1 - alpha_float[..., np.newaxis]) * scene_roi
@ -339,7 +345,7 @@ def draw_image(
)
# Update the scene
scene[rect.y : rect.y + rect.height, rect.x : rect.x + rect.width] = blended_roi
scene[rect_y : rect_y + rect_height, rect_x : rect_x + rect_width] = blended_roi
return scene

View File

@ -22,7 +22,7 @@ class Position(Enum):
CENTER_OF_MASS = "CENTER_OF_MASS"
@classmethod
def list(cls):
def list(cls) -> list[str]:
return list(map(lambda c: c.value, cls))
@ -110,7 +110,7 @@ class Rect:
def bottom_right(self) -> Point:
return Point(x=self.x + self.width, y=self.y + self.height)
def pad(self, padding) -> Rect:
def pad(self, padding: int) -> Rect:
return Rect(
x=self.x - padding,
y=self.y - padding,