Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#807 Feature/sg 747 add full pipeline with preprocessing

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

Press p or to see the previous file or, n or to see the next file