4.0 MiB
Vendored
4.0 MiB
Vendored
In [1]:
!nvidia-smiMon Feb 12 13:03:38 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 T4 Off | 00000000:00:04.0 Off | 0 |
| N/A 48C P8 10W / 70W | 0MiB / 15360MiB | 0% Default |
| | | N/A |
+-----------------------------------------+----------------------+----------------------+
+---------------------------------------------------------------------------------------+
| Processes: |
| GPU GI CI PID Type Process name GPU Memory |
| ID ID Usage |
|=======================================================================================|
| No running processes found |
+---------------------------------------------------------------------------------------+
In [2]:
import os
HOME = os.getcwd()
print(HOME)/content
In [ ]:
!pip install -q ultralytics supervisionIn [4]:
import numpy as np
import supervision as sv
from ultralytics import YOLO
from supervision.assets import download_assets, VideoAssetsIn [ ]:
download_assets(VideoAssets.VEHICLES)In [6]:
SOURCE_VIDEO_PATH = f"{HOME}/vehicles.mp4"In [7]:
generator = sv.get_video_frames_generator(SOURCE_VIDEO_PATH)
frame = next(generator)
sv.plot_image(frame, (12, 12))In [8]:
sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)Out [8]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
In [7]:
model = YOLO("yolo11x.pt")
results = model(frame, verbose=False)[0]
detections = sv.Detections.from_ultralytics(results)Downloading https://github.com/ultralytics/assets/releases/download/v8.3.0/yolo11x.pt to 'yolo11x.pt'...
100%|██████████| 109M/109M [00:10<00:00, 11.5MB/s]
In [10]:
bounding_box_annotator = sv.BoxAnnotator(thickness=6)
annotated_frame = bounding_box_annotator.annotate(frame.copy(), detections)
sv.plot_image(annotated_frame, (12, 12))In [11]:
labels = [
f"{results.names[class_id]} {confidence:0.2f}"
for class_id, confidence
in zip(detections.class_id, detections.confidence)
]
box_annotator = sv.BoxAnnotator(thickness=6)
label_annotator = sv.LabelAnnotator(text_thickness=4, text_scale=2)
annotated_frame = frame.copy()
annotated_frame = box_annotator.annotate(annotated_frame, detections)
annotated_frame = label_annotator.annotate(annotated_frame, detections, labels)
sv.plot_image(annotated_frame, (12, 12))In [14]:
sv.VideoInfo.from_video_path(SOURCE_VIDEO_PATH)Out [14]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
In [22]:
START = sv.Point(0, 1500)
END = sv.Point(3840, 1500)
line_zone = sv.LineZone(start=START, end=END)
line_zone_annotator = sv.LineZoneAnnotator(
thickness=4,
text_thickness=4,
text_scale=2)
annotated_frame = frame.copy()
annotated_frame = line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)
sv.plot_image(annotated_frame, (12, 12))In [18]:
byte_tracker = sv.ByteTrack()In [17]:
bounding_box_annotator = sv.BoxAnnotator(thickness=4)
label_annotator = sv.LabelAnnotator(text_thickness=4, text_scale=2)
trace_annotator = sv.TraceAnnotator(thickness=4)In [19]:
def callback(frame: np.ndarray, index:int) -> np.ndarray:
results = model(frame, verbose=False)[0]
detections = sv.Detections.from_ultralytics(results)
detections = byte_tracker.update_with_detections(detections)
labels = [
f"#{tracker_id} {model.model.names[class_id]} {confidence:0.2f}"
for confidence, class_id, tracker_id
in zip(detections.confidence, detections.class_id, detections.tracker_id)
]
annotated_frame = frame.copy()
annotated_frame = trace_annotator.annotate(
scene=annotated_frame,
detections=detections)
annotated_frame = bounding_box_annotator.annotate(
scene=annotated_frame,
detections=detections)
annotated_frame = label_annotator.annotate(
scene=annotated_frame,
detections=detections,
labels=labels)
line_zone.trigger(detections)
return line_zone_annotator.annotate(annotated_frame, line_counter=line_zone)In [21]:
TARGET_VIDEO_PATH = f"{HOME}/count-objects-crossing-the-line-result.mp4"In [20]:
sv.process_video(
source_path = SOURCE_VIDEO_PATH,
target_path = TARGET_VIDEO_PATH,
callback=callback
)