Save Detections¶
CSV Sink
A utility class for saving detection data to a CSV file. This class is designed to
efficiently serialize detection objects into a CSV format, allowing for the
inclusion of bounding box coordinates and additional attributes like confidence
,
class_id
, and tracker_id
.
Tip
CSVSink allow to pass custom data alongside the detection fields, providing flexibility for logging various types of information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
The name of the CSV file where the detections will be stored. Defaults to 'output.csv'. |
'output.csv'
|
Example
import supervision as sv
from ultralytics import YOLO
model = YOLO(<SOURCE_MODEL_PATH>)
csv_sink = sv.CSVSink(<RESULT_CSV_FILE_PATH>)
frames_generator = sv.get_video_frames_generator(<SOURCE_VIDEO_PATH>)
with csv_sink as sink:
for frame in frames_generator:
result = model(frame)[0]
detections = sv.Detections.from_ultralytics(result)
sink.append(detections, custom_data={'<CUSTOM_LABEL>':'<CUSTOM_DATA>'})
Source code in supervision/detection/tools/csv_sink.py
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 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 |
|
Functions¶
__init__(file_name='output.csv')
¶
Initialize the CSVSink instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
The name of the CSV file. |
'output.csv'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/csv_sink.py
append(detections, custom_data=None)
¶
Append detection data to the CSV file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detections |
Detections
|
The detection data. |
required |
custom_data |
Dict[str, Any]
|
Custom data to include. |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/csv_sink.py
close()
¶
open()
¶
Open the CSV file for writing.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/csv_sink.py
JSON Sink
A utility class for saving detection data to a JSON file. This class is designed to
efficiently serialize detection objects into a JSON format, allowing for the
inclusion of bounding box coordinates and additional attributes like confidence
,
class_id
, and tracker_id
.
Tip
JSONsink allow to pass custom data alongside the detection fields, providing flexibility for logging various types of information.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
The name of the JSON file where the detections will be stored. Defaults to 'output.json'. |
'output.json'
|
Example
import supervision as sv
from ultralytics import YOLO
model = YOLO(<SOURCE_MODEL_PATH>)
json_sink = sv.JSONSink(<RESULT_JSON_FILE_PATH>)
frames_generator = sv.get_video_frames_generator(<SOURCE_VIDEO_PATH>)
with json_sink as sink:
for frame in frames_generator:
result = model(frame)[0]
detections = sv.Detections.from_ultralytics(result)
sink.append(detections, custom_data={'<CUSTOM_LABEL>':'<CUSTOM_DATA>'})
Source code in supervision/detection/tools/json_sink.py
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 |
|
Functions¶
__init__(file_name='output.json')
¶
Initialize the JSONSink instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name |
str
|
The name of the JSON file. |
'output.json'
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/json_sink.py
append(detections, custom_data=None)
¶
Append detection data to the JSON file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
detections |
Detections
|
The detection data. |
required |
custom_data |
Dict[str, Any]
|
Custom data to include. |
None
|
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/json_sink.py
open()
¶
Open the JSON file for writing.
Returns:
Type | Description |
---|---|
None
|
None |
Source code in supervision/detection/tools/json_sink.py
write_and_close()
¶
Write and close the JSON file.
Returns:
Type | Description |
---|---|
None
|
None |