Tidy up Sink cookbook text

This commit is contained in:
LinasKo 2024-06-24 13:31:35 +03:00
parent 0df56b1d66
commit 9333460ee9
2 changed files with 65 additions and 81 deletions

View File

@ -58,7 +58,12 @@
"id": "d4s3Ndsowl81"
},
"source": [
"For reference to how to choose inference model for `INFERENCE_MODEL` to set `yolov8n-640` is in inference documentation aliases section https://inference.roboflow.com/quickstart/aliases/#supported-pre-trained-models"
"The parameters defined below are:\n",
"* `SOURCE_VIDEO_PATH` - the path to the input video\n",
"* `CONFIDENCE_THRESHOLD` - do not include detections below this confidence level\n",
"* `IOU_THRESHOLD` - discard detections that overlap with others by more than this [IOU](https://blog.roboflow.com/how-to-code-non-maximum-suppression-nms-in-plain-numpy/) ratio\n",
"* `FILE_NAME` - write the json output to this file\n",
"* `INFERENCE_MODEL` - model id. This cookbook uses a [model alias](https://inference.roboflow.com/quickstart/aliases/), but it can also be a [fine-tuned model](https://inference.roboflow.com/quickstart/explore_models/) or a model from the [Universe](https://inference.roboflow.com/quickstart/load_from_universe/)."
]
},
{
@ -138,7 +143,7 @@
"source": [
"## Initialize ByteTrack\n",
"\n",
"Initialize the [ByteTrack](https://supervision.roboflow.com/latest/trackers/#bytetrack) object."
"[ByteTrack](https://supervision.roboflow.com/latest/trackers/#bytetrack) is a multi-object tracking algorithm used by Supervision to track and link detected objects across multiple frames, providing consistent IDs for each object.Initialize the object."
]
},
{
@ -159,9 +164,11 @@
"id": "IjYwj2vbTLBd"
},
"source": [
"## Initialize CSVSink and open sink\n",
"## Initialize CSVSink\n",
"\n",
"To save detections to a .CSV file, open our [`sv.CSVSink`](https://supervision.roboflow.com/latest/how_to/save_detections/#save-detections-as-csv) and then pass the [`sv.Detections`](https://supervision.roboflow.com/latest/detection/core/#detections) object resulting from the inference to it. Its fields are parsed and saved on disk."
"To save detections to a `CSV` file, open our [`sv.CSVSink`](https://supervision.roboflow.com/latest/how_to/save_detections/#save-detections-as-csv) and then pass the [`sv.Detections`](https://supervision.roboflow.com/latest/detection/core/#detections) object resulting from the inference to it.\n",
"\n",
"Note that empty detections will be skipped."
]
},
{
@ -184,12 +191,14 @@
"source": [
"## Process video and save detections to csv file\n",
"\n",
"The [`InferencePipeline`](https://inference.roboflow.com/using_inference/inference_pipeline/) interface is made for streaming and is likely the best route to go for real time use cases. It is an asynchronous interface that can consume many different video sources including local devices (like webcams), RTSP video streams, video files, etc. With this interface, you define the source of a video stream and sinks.\n",
"\n",
"All the operations we plan to perform for each frame of our video - detection, tracking, annotation, and write to csv are encapsulated in a function named `callback`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": null,
"metadata": {
"id": "eRQiVkblapCk"
},
@ -202,17 +211,6 @@
" csv_sink.append(detections, custom_data={'frame_number': frame.frame_id})"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "2lwYiCgJ-lqo"
},
"source": [
"##\u00a0Initilize InferencePipeline\n",
"\n",
"The [`InferencePipeline`](https://inference.roboflow.com/using_inference/inference_pipeline/) interface is made for streaming and is likely the best route to go for real time use cases. It is an asynchronous interface that can consume many different video sources including local devices (like webcams), RTSP video streams, video files, etc. With this interface, you define the source of a video stream and sinks."
]
},
{
"cell_type": "code",
"execution_count": null,
@ -456,7 +454,7 @@
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>8851 rows \u00d7 9 columns</p>\n",
"<p>8851 rows × 9 columns</p>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
@ -704,7 +702,7 @@
],
"source": [
"df = pd.read_csv(FILE_NAME)\n",
"df # show data"
"df"
]
},
{
@ -735,28 +733,17 @@
" with open(csv_file, 'r') as f:\n",
" reader = csv.DictReader(f)\n",
" for row in reader:\n",
" xyxy.append([float(row[\"x_min\"]), float(row[\"y_min\"]), float(row[\"x_max\"]), float(row[\"y_max\"])])\n",
" class_id.append(int(row[\"class_id\"]))\n",
" confidence.append(float(row[\"confidence\"]))\n",
" tracker_id.append(int(row[\"tracker_id\"]))\n",
" frame_number.append(int(row[\"frame_number\"]))\n",
" class_name.append(row[\"class_name\"])\n",
" xyxy.append([float(row[\"x_min\"]), float(row[\"y_min\"]), float(row[\"x_max\"]), float(row[\"y_max\"])])\n",
" class_id.append(int(row[\"class_id\"]))\n",
" confidence.append(float(row[\"confidence\"]))\n",
" tracker_id.append(int(row[\"tracker_id\"]))\n",
" frame_number.append(int(row[\"frame_number\"]))\n",
" class_name.append(row[\"class_name\"])\n",
"\n",
" custom_data = {\"frame_number\": np.array(frame_number), \"class_name\": np.array(class_name)}\n",
" return sv.Detections(xyxy=np.array(xyxy), class_id=np.array(class_id), confidence=np.array(confidence), tracker_id=np.array(tracker_id), data=custom_data)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "pR47TRpx0qRA"
},
"outputs": [],
"source": [
"csv_detections = csv_to_detections(FILE_NAME)"
]
},
{
"cell_type": "code",
"execution_count": 15,
@ -788,6 +775,7 @@
}
],
"source": [
"csv_detections = csv_to_detections(FILE_NAME)\n",
"csv_detections"
]
},
@ -797,7 +785,7 @@
"id": "2pyVFVWI096b"
},
"source": [
"###\u00a0Annotate First Frame\n",
"### Annotate First Frame\n",
"\n",
"Visualize the first frame of a video alongside the initial detections obtained by parsing CSV data into [`sv.Detections`](https://supervision.roboflow.com/latest/detection/core/#detections) objects. The annotated image will show the original video frame, marked with the first bounding box detected from the parsed data, providing a visual representation of the identified object(s) in the scene."
]
@ -819,7 +807,10 @@
},
"outputs": [],
"source": [
"first_frame_detections = csv_detections[csv_detections.data.get(\"frame_number\") == 4] # first detected frame number is 4"
"# First detected frame number is 4\n",
"first_frame_detections = csv_detections[\n",
" csv_detections.data.get(\"frame_number\") == 4\n",
"]"
]
},
{
@ -864,7 +855,7 @@
"id": "oIK2K9zM1Kn9"
},
"source": [
"###\u00a0Annotate Image with Detections\n",
"### Annotate Image with Detections\n",
"\n",
"Finally, we can annotate the image with the predictions. Since we are working with an object detection model, we will use the [`sv.BoundingBoxAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.BoundingBoxAnnotator) and [`sv.LabelAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.LabelAnnotator) classes."
]
@ -909,12 +900,13 @@
"id": "YRPW1JHK1VW-"
},
"source": [
"##\u00a0References \ud83d\udcda\n",
"## References 📚\n",
"\n",
"* Supervision: https://supervision.roboflow.com\n",
"* https://supervision.roboflow.com/develop/detection/core/#detections\n",
"* sv.Detections: https://supervision.roboflow.com/develop/detection/core/#detections\n",
"* Save Detections to CSV: https://supervision.roboflow.com/develop/how_to/save_detections/#save-detections-as-csv\n",
"* CSV Custom fields: https://supervision.roboflow.com/develop/how_to/save_detections/#custom-fields\n",
"* Custom fields: https://supervision.roboflow.com/develop/how_to/save_detections/#custom-fields\n",
"* ByteTrack: https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack\n",
"* Inference: https://inference.roboflow.com/\n",
"* Inference Pipeline: https://inference.roboflow.com/using_inference/inference_pipeline/\n",
"* Inference Aliases: https://inference.roboflow.com/quickstart/aliases/\n",

View File

@ -58,7 +58,12 @@
"id": "95av545ydC1O"
},
"source": [
"For reference to how to choose inference model for `INFERENCE_MODEL` to set `yolov8n-640` is in inference documentation aliases section https://inference.roboflow.com/quickstart/aliases/#supported-pre-trained-models"
"The parameters defined below are:\n",
"* `SOURCE_VIDEO_PATH` - the path to the input video\n",
"* `CONFIDENCE_THRESHOLD` - do not include detections below this confidence level\n",
"* `IOU_THRESHOLD` - discard detections that overlap with others by more than this [IOU](https://blog.roboflow.com/how-to-code-non-maximum-suppression-nms-in-plain-numpy/) ratio\n",
"* `FILE_NAME` - write the json output to this file\n",
"* `INFERENCE_MODEL` - model id. This cookbook uses a [model alias](https://inference.roboflow.com/quickstart/aliases/), but it can also be a [fine-tuned model](https://inference.roboflow.com/quickstart/explore_models/) or a model from the [Universe](https://inference.roboflow.com/quickstart/load_from_universe/)."
]
},
{
@ -95,7 +100,7 @@
"id": "HZ20JXd4gYdw"
},
"source": [
"##\u00a0Read single frame from video\n",
"## Read single frame from video\n",
"\n",
"The [`get_video_frames_generator`](https://supervision.roboflow.com/develop/utils/video/#supervision.utils.video.get_video_frames_generator) enables us to easily iterate over video frames. Let's create a video generator for our sample input file and display its first frame on the screen.\n",
"\n"
@ -173,7 +178,7 @@
"source": [
"## Initialize ByteTrack\n",
"\n",
"Initialize the [ByteTrack](https://supervision.roboflow.com/latest/trackers/#bytetrack) object."
"[ByteTrack](https://supervision.roboflow.com/latest/trackers/#bytetrack) is a multi-object tracking algorithm used by Supervision to track and link detected objects across multiple frames, providing consistent IDs for each object."
]
},
{
@ -196,7 +201,9 @@
"source": [
"## Initialize sv.JSONSink\n",
"\n",
"Initialize the [sv.JSONSink](https://supervision.roboflow.com/latest/how_to/save_detections/#save-detections-as-json) object."
"To save detections to a `JSON` file, open our [`sv.JSONSink`](https://supervision.roboflow.com/latest/how_to/save_detections/#save-detections-as-json) and then pass the [`sv.Detections`](https://supervision.roboflow.com/latest/detection/core/#detections) object resulting from the inference to it.\n",
"\n",
"Note that empty detections will be skipped."
]
},
{
@ -219,6 +226,8 @@
"source": [
"## Process video and save detections to json file\n",
"\n",
"The [`InferencePipeline`](https://inference.roboflow.com/using_inference/inference_pipeline/) interface is made for streaming and is likely the best route to go for real time use cases. It is an asynchronous interface that can consume many different video sources including local devices (like webcams), RTSP video streams, video files, etc. With this interface, you define the source of a video stream and sinks.\n",
"\n",
"All the operations we plan to perform for each frame of our video - detection, tracking, annotation, and write to json - are encapsulated in a function named `callback`."
]
},
@ -237,17 +246,6 @@
" json_sink.append(detections, custom_data={'frame_number': frame.frame_id})"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "rOef_41bdOuh"
},
"source": [
"##\u00a0Initilize InferencePipeline\n",
"\n",
"The [`InferencePipeline`](https://inference.roboflow.com/using_inference/inference_pipeline/) interface is made for streaming and is likely the best route to go for real time use cases. It is an asynchronous interface that can consume many different video sources including local devices (like webcams), RTSP video streams, video files, etc. With this interface, you define the source of a video stream and sinks."
]
},
{
"cell_type": "code",
"execution_count": null,
@ -491,7 +489,7 @@
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>8851 rows \u00d7 9 columns</p>\n",
"<p>8851 rows × 9 columns</p>\n",
"</div>\n",
" <div class=\"colab-df-buttons\">\n",
"\n",
@ -771,29 +769,17 @@
" data = json.load(f)\n",
"\n",
" for row in data:\n",
" xyxy.append([float(row[key]) for key in [\"x_min\", \"y_min\", \"x_max\", \"y_max\"]])\n",
" class_id.append(int(row[\"class_id\"]))\n",
" confidence.append(float(row[\"confidence\"]))\n",
" tracker_id.append(int(row[\"tracker_id\"]))\n",
" frame_number.append(int(row[\"frame_number\"]))\n",
" class_name.append(row[\"class_name\"])\n",
" xyxy.append([float(row[key]) for key in [\"x_min\", \"y_min\", \"x_max\", \"y_max\"]])\n",
" class_id.append(int(row[\"class_id\"]))\n",
" confidence.append(float(row[\"confidence\"]))\n",
" tracker_id.append(int(row[\"tracker_id\"]))\n",
" frame_number.append(int(row[\"frame_number\"]))\n",
" class_name.append(row[\"class_name\"])\n",
"\n",
" custom_data = {\"frame_number\": np.array(frame_number), \"class_name\": np.array(class_name)}\n",
"\n",
" return sv.Detections(xyxy=np.array(xyxy), class_id=np.array(class_id), confidence=np.array(confidence), tracker_id=np.array(tracker_id), data=custom_data)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"id": "YrYvgBRIicj5"
},
"outputs": [],
"source": [
"json_detections = json_to_detections(FILE_NAME)"
]
},
{
"cell_type": "code",
"execution_count": 15,
@ -825,6 +811,7 @@
}
],
"source": [
"json_detections = json_to_detections(FILE_NAME)\n",
"json_detections"
]
},
@ -834,7 +821,7 @@
"id": "ZlvzmExpqdvI"
},
"source": [
"###\u00a0Annotate First Frame\n",
"### Annotate First Frame\n",
"\n",
"Visualize the first frame of a video alongside the initial detections obtained by parsing JSON data into [`sv.Detections`](https://supervision.roboflow.com/latest/detection/core/#detections) objects. The annotated image will show the original video frame, marked with the first bounding box detected from the parsed data, providing a visual representation of the identified object(s) in the scene."
]
@ -856,7 +843,10 @@
},
"outputs": [],
"source": [
"first_frame_detections = json_detections[json_detections.data.get(\"frame_number\") == 4] # first detected frame number is 4"
"# First detected frame number is 4\n",
"first_frame_detections = json_detections[\n",
" json_detections.data.get(\"frame_number\") == 4\n",
"]"
]
},
{
@ -901,7 +891,7 @@
"id": "FK5VfyJurhEY"
},
"source": [
"###\u00a0Annotate Image with Detections\n",
"### Annotate Image with Detections\n",
"\n",
"Finally, we can annotate the image with the predictions. Since we are working with an object detection model, we will use the [`sv.BoundingBoxAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.BoundingBoxAnnotator) and [`sv.LabelAnnotator`](https://supervision.roboflow.com/latest/detection/annotators/#supervision.annotators.core.LabelAnnotator) classes."
]
@ -946,12 +936,13 @@
"id": "975K127Regdr"
},
"source": [
"##\u00a0References \ud83d\udcda\n",
"## References 📚\n",
"\n",
"* Supervision: https://supervision.roboflow.com\n",
"* https://supervision.roboflow.com/develop/detection/core/#detections\n",
"* sv.Detections: https://supervision.roboflow.com/develop/detection/core/#detections\n",
"* Save Detections to JSON: https://supervision.roboflow.com/develop/how_to/save_detections/#save-detections-as-json\n",
"* Custom fields: https://supervision.roboflow.com/develop/how_to/save_detections/#custom-fields\n",
"* ByteTrack: https://supervision.roboflow.com/trackers/#supervision.tracker.byte_tracker.core.ByteTrack\n",
"* Inference: https://inference.roboflow.com/\n",
"* Inference Pipeline: https://inference.roboflow.com/using_inference/inference_pipeline/\n",
"* Inference Aliases: https://inference.roboflow.com/quickstart/aliases/\n",
@ -970,7 +961,8 @@
"name": "python3"
},
"language_info": {
"name": "python"
"name": "python",
"version": "3.10.14"
}
},
"nbformat": 4,