Serialise Detections to a CSV File¶
This cookbook introduce sv.CSVSink tool designed to write captured object detection data to file from video streams/file
Click the Open in Colab
button to run the cookbook on Google Colab.
In [ ]:
Copied!
!pip install -q inference requests tqdm supervision==0.21.0
!pip install -q inference requests tqdm supervision==0.21.0
In [5]:
Copied!
import csv
from typing import List
from collections import defaultdict
import numpy as np
import pandas as pd
import supervision as sv
from supervision.assets import download_assets, VideoAssets
from inference import InferencePipeline
from inference.core.interfaces.camera.entities import VideoFrame
import csv
from typing import List
from collections import defaultdict
import numpy as np
import pandas as pd
import supervision as sv
from supervision.assets import download_assets, VideoAssets
from inference import InferencePipeline
from inference.core.interfaces.camera.entities import VideoFrame
The parameters defined below are:
SOURCE_VIDEO_PATH
- the path to the input videoCONFIDENCE_THRESHOLD
- do not include detections below this confidence levelIOU_THRESHOLD
- discard detections that overlap with others by more than this IOU ratioFILE_NAME
- write the json output to this fileINFERENCE_MODEL
- model id. This cookbook uses a model alias, but it can also be a fine-tuned model or a model from the Universe.
In [6]:
Copied!
SOURCE_VIDEO_PATH = download_assets(VideoAssets.PEOPLE_WALKING)
CONFIDENCE_THRESHOLD = 0.3
IOU_THRESHOLD = 0.7
FILE_NAME = "detections.csv"
INFERENCE_MODEL = "yolov8n-640"
SOURCE_VIDEO_PATH = download_assets(VideoAssets.PEOPLE_WALKING)
CONFIDENCE_THRESHOLD = 0.3
IOU_THRESHOLD = 0.7
FILE_NAME = "detections.csv"
INFERENCE_MODEL = "yolov8n-640"
people-walking.mp4 asset download complete.
As a result of executing the above download_assets(VideoAssets.PEOPLE_WALKING)
, you will download a video file and save it at the SOURCE_VIDEO_PATH
. Keep in mind that the video preview below works only in the web version of the cookbooks and not in Google Colab.
Read single frame from video¶
The 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.
In [7]:
Copied!
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
frame = next(generator)
sv.plot_image(frame, (12, 12))
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
frame = next(generator)
sv.plot_image(frame, (12, 12))