1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
|
- from abc import ABC, abstractmethod
- from typing import List, Optional, Tuple
- from dataclasses import dataclass
- from matplotlib import pyplot as plt
- import numpy as np
- from super_gradients.training.utils.detection_utils import DetectionVisualization
- from super_gradients.training.models.predictions import Prediction, DetectionPrediction
- @dataclass
- class Result(ABC):
- """Results of a given computer vision task (detection, classification, etc.).
- :attr image: Input image
- :attr predictions: Predictions of the model
- :attr class_names: List of the class names to predict
- """
- image: np.ndarray
- predictions: Prediction
- class_names: List[str]
- @abstractmethod
- def draw(self) -> np.ndarray:
- """Draw the predictions on the image."""
- pass
- @abstractmethod
- def show(self) -> None:
- """Display the predictions on the image."""
- pass
- @dataclass
- class Results(ABC):
- """List of results of a given computer vision task (detection, classification, etc.).
- :attr results: List of results of the run
- """
- results: List[Result]
- @abstractmethod
- def draw(self) -> List[np.ndarray]:
- """Draw the predictions on the image."""
- pass
- @abstractmethod
- def show(self) -> None:
- """Display the predictions on the image."""
- pass
- @dataclass
- class DetectionResult(Result):
- """Result of a detection task.
- :attr image: Input image
- :attr predictions: Predictions of the model
- :attr class_names: List of the class names to predict
- """
- image: np.ndarray
- predictions: DetectionPrediction
- class_names: List[str]
- def draw(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int]]] = None) -> np.ndarray:
- """Draw the predicted bboxes on the image.
- :param box_thickness: Thickness of bounding boxes.
- :param show_confidence: Whether to show confidence scores on the image.
- :param color_mapping: List of tuples representing the colors for each class.
- Default is None, which generates a default color mapping based on the number of class names.
- :return: Image with predicted bboxes. Note that this does not modify the original image.
- """
- image_np = self.image.copy()
- color_mapping = color_mapping or DetectionVisualization._generate_color_mapping(len(self.class_names))
- for pred_i in range(len(self.predictions)):
- image_np = DetectionVisualization._draw_box_title(
- color_mapping=color_mapping,
- class_names=self.class_names,
- box_thickness=box_thickness,
- image_np=image_np,
- x1=int(self.predictions.bboxes_xyxy[pred_i, 0]),
- y1=int(self.predictions.bboxes_xyxy[pred_i, 1]),
- x2=int(self.predictions.bboxes_xyxy[pred_i, 2]),
- y2=int(self.predictions.bboxes_xyxy[pred_i, 3]),
- class_id=int(self.predictions.labels[pred_i]),
- pred_conf=self.predictions.confidence[pred_i] if show_confidence else None,
- )
- return image_np
- def show(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int]]] = None) -> None:
- """Display the image with predicted bboxes.
- :param box_thickness: Thickness of bounding boxes.
- :param show_confidence: Whether to show confidence scores on the image.
- :param color_mapping: List of tuples representing the colors for each class.
- Default is None, which generates a default color mapping based on the number of class names.
- """
- image_np = self.draw(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)
- plt.imshow(image_np, interpolation="nearest")
- plt.axis("off")
- plt.show()
- @dataclass
- class DetectionResults(Results):
- """Results of a detection task.
- :attr results: List of the predictions results
- """
- def __init__(self, images: List[np.ndarray], predictions: List[DetectionPrediction], class_names: List[str]):
- self.results: List[DetectionResult] = []
- for image, prediction in zip(images, predictions):
- self.results.append(DetectionResult(image=image, predictions=prediction, class_names=class_names))
- def draw(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int]]] = None) -> List[np.ndarray]:
- """Draw the predicted bboxes on the images.
- :param box_thickness: Thickness of bounding boxes.
- :param show_confidence: Whether to show confidence scores on the image.
- :param color_mapping: List of tuples representing the colors for each class.
- Default is None, which generates a default color mapping based on the number of class names.
- :return: List of Images with predicted bboxes for each image. Note that this does not modify the original images.
- """
- return [prediction.draw(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping) for prediction in self.results]
- def show(self, box_thickness: int = 2, show_confidence: bool = True, color_mapping: Optional[List[Tuple[int]]] = None) -> None:
- """Display the predicted bboxes on the images.
- :param box_thickness: Thickness of bounding boxes.
- :param show_confidence: Whether to show confidence scores on the image.
- :param color_mapping: List of tuples representing the colors for each class.
- Default is None, which generates a default color mapping based on the number of class names.
- """
- for prediction in self.results:
- prediction.show(box_thickness=box_thickness, show_confidence=show_confidence, color_mapping=color_mapping)
|