- Removed specific version constraint for `supervision` package in installation command. - Updated references from `sv.BoundingBoxAnnotator` to `sv.BoxAnnotator` for consistency with the latest library changes. - Removed asset extension of package installation because it is has been included into main package Signed-off-by: Onuralp SEZER <thunderbirdtr@gmail.com>
543 KiB
Vendored
543 KiB
Vendored
In [1]:
!nvidia-smiFri Feb 23 03:15:00 2024
+---------------------------------------------------------------------------------------+
| NVIDIA-SMI 535.104.05 Driver Version: 535.104.05 CUDA Version: 12.2 |
|-----------------------------------------+----------------------+----------------------+
| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|=========================================+======================+======================|
| 0 Tesla V100-SXM2-16GB Off | 00000000:00:04.0 Off | 0 |
| N/A 33C P0 24W / 300W | 0MiB / 16384MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| No running processes found |
+---------------------------------------------------------------------------------------+
In [ ]:
!pip install -q inference-gpu "supervision"In [ ]:
from supervision.assets import download_assets, VideoAssets
# Download a supervision video asset
path_to_video = download_assets(VideoAssets.PEOPLE_WALKING)In [ ]:
import supervision as sv
from inference.models.utils import get_roboflow_model
# Load a yolov8 model from roboflow.
model = get_roboflow_model("yolov8s-640")
# Create a frame generator and video info object from supervision utilities.
frame_generator = sv.get_video_frames_generator(path_to_video)
# Yield a single frame from the generator.
frame = next(frame_generator)
# Run inference on our frame
result = model.infer(frame)[0]
# Parse result into detections data model.
detections = sv.Detections.from_inference(result)
# Pretty Print the resulting detections.
from pprint import pprint
pprint(detections)Detections(xyxy=array([[1140., 951., 1245., 1079.],
[ 666., 648., 745., 854.],
[ 34., 794., 142., 990.],
[1140., 505., 1211., 657.],
[ 260., 438., 332., 612.],
[1413., 702., 1523., 887.],
[1462., 472., 1543., 643.],
[1446., 318., 1516., 483.],
[ 753., 451., 821., 623.],
[ 924., 172., 983., 307.],
[1791., 144., 1852., 275.],
[ 93., 132., 146., 251.],
[ 708., 240., 765., 388.],
[ 200., 44., 267., 161.],
[1204., 131., 1255., 266.],
[ 569., 267., 628., 408.],
[1163., 150., 1210., 280.],
[ 799., 78., 847., 204.],
[1690., 152., 1751., 283.],
[ 344., 495., 396., 641.],
[1722., 77., 1782., 178.]]),
mask=None,
confidence=array([0.83215541, 0.80572134, 0.7919845 , 0.7912274 , 0.77121079,
0.7599591 , 0.75711554, 0.75494027, 0.73076195, 0.71452248,
0.69572842, 0.65269446, 0.63952065, 0.62914598, 0.61361706,
0.5968492 , 0.55311316, 0.5470854 , 0.54070991, 0.52209878,
0.41217673]),
class_id=array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
tracker_id=None,
data={'class_name': array(['person', 'person', 'person', 'person', 'person', 'person',
'person', 'person', 'person', 'person', 'person', 'person',
'person', 'person', 'person', 'person', 'person', 'person',
'person', 'person', 'person'], dtype='<U6')})
In [5]:
# Create a bounding box annotator object.
box_annotator = sv.BoxAnnotator()
# Annotate our frame with detections.
annotated_frame = box_annotator.annotate(scene=frame.copy(), detections=detections)
# Display the frame.
sv.plot_image(annotated_frame)In [ ]:
from tqdm import tqdm
# Create a video_info object for use in the VideoSink.
video_info = sv.VideoInfo.from_video_path(video_path=path_to_video)
# Create a VideoSink context manager to save our frames.
with sv.VideoSink(target_path="output.mp4", video_info=video_info) as sink:
# Iterate through frames yielded from the frame_generator.
for frame in tqdm(frame_generator, total=video_info.total_frames):
# Run inference on our frame.
result = model.infer(frame)[0]
# Parse the result into the detections data model.
detections = sv.Detections.from_inference(result)
# Apply bounding box to detections on a copy of the frame.
annotated_frame = box_annotator.annotate(
scene=frame.copy(),
detections=detections
)
# Write the annotated frame to the video sink.
sink.write_frame(frame=annotated_frame)