Reference for ultralytics/solutions/analytics.py
Note
This file is available at https://github.com/ultralytics/ultralytics/blob/main/ultralytics/solutions/analytics.py. If you spot a problem please help fix it by contributing a Pull Request 🛠️. Thank you 🙏!
ultralytics.solutions.analytics.Analytics
Bases: BaseSolution
A class for creating and updating various types of charts for visual analytics.
This class extends BaseSolution to provide functionality for generating line, bar, pie, and area charts based on object detection and tracking data.
Attributes:
Name | Type | Description |
---|---|---|
type | str | The type of analytics chart to generate ('line', 'bar', 'pie', or 'area'). |
x_label | str | Label for the x-axis. |
y_label | str | Label for the y-axis. |
bg_color | str | Background color of the chart frame. |
fg_color | str | Foreground color of the chart frame. |
title | str | Title of the chart window. |
max_points | int | Maximum number of data points to display on the chart. |
fontsize | int | Font size for text display. |
color_cycle | cycle | Cyclic iterator for chart colors. |
total_counts | int | Total count of detected objects (used for line charts). |
clswise_count | Dict[str, int] | Dictionary for class-wise object counts. |
fig | Figure | Matplotlib figure object for the chart. |
ax | Axes | Matplotlib axes object for the chart. |
canvas | FigureCanvasAgg | Canvas for rendering the chart. |
Methods:
Name | Description |
---|---|
process_data | Processes image data and updates the chart. |
update_graph | Updates the chart with new data points. |
Examples:
>>> analytics = Analytics(analytics_type="line")
>>> frame = cv2.imread("image.jpg")
>>> processed_frame = analytics.process_data(frame, frame_number=1)
>>> cv2.imshow("Analytics", processed_frame)
Source code in ultralytics/solutions/analytics.py
process_data
Processes image data and runs object tracking to update analytics charts.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
im0 | ndarray | Input image for processing. | required |
frame_number | int | Video frame number for plotting the data. | required |
Returns:
Type | Description |
---|---|
ndarray | Processed image with updated analytics chart. |
Raises:
Type | Description |
---|---|
ModuleNotFoundError | If an unsupported chart type is specified. |
Examples:
>>> analytics = Analytics(analytics_type="line")
>>> frame = np.zeros((480, 640, 3), dtype=np.uint8)
>>> processed_frame = analytics.process_data(frame, frame_number=1)
Source code in ultralytics/solutions/analytics.py
update_graph
Updates the graph with new data for single or multiple classes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame_number | int | The current frame number. | required |
count_dict | Dict[str, int] | None | Dictionary with class names as keys and counts as values for multiple classes. If None, updates a single line graph. | None |
plot | str | Type of the plot. Options are 'line', 'bar', 'pie', or 'area'. | 'line' |
Returns:
Type | Description |
---|---|
ndarray | Updated image containing the graph. |
Examples:
>>> analytics = Analytics()
>>> frame_number = 10
>>> count_dict = {"person": 5, "car": 3}
>>> updated_image = analytics.update_graph(frame_number, count_dict, plot="bar")
Source code in ultralytics/solutions/analytics.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
|