- 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>
3.6 MiB
Vendored
3.6 MiB
Vendored
In [1]:
!nvidia-smiWed Jul 17 14:51:30 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 NVIDIA L4 Off | 00000000:00:03.0 Off | 0 |
| N/A 63C P8 14W / 72W | 1MiB / 23034MiB | 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 [3]:
!mkdir {HOME}/imagesIn [4]:
%cd {HOME}/images
!wget -q https://media.roboflow.com/notebooks/examples/dog.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-2.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-3.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-4.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-5.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-6.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-7.jpeg
!wget -q https://media.roboflow.com/notebooks/examples/dog-8.jpeg/content/images
In [5]:
!pip install -q supervision
import supervision as sv
print(sv.__version__)[?25l [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m0.0/135.7 kB[0m [31m?[0m eta [36m-:--:--[0m [2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m135.7/135.7 kB[0m [31m3.9 MB/s[0m eta [36m0:00:00[0m [?25h0.22.0
In [6]:
import cv2
IMAGE_PATH = f"{HOME}/images/dog.jpeg"
image = cv2.imread(IMAGE_PATH)In [ ]:
!pip install -q ultralyticsIn [ ]:
from ultralytics import YOLO
model = YOLO("yolov8s.pt")
result = model(image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(result)In [9]:
"detections", len(detections)Out [9]:
('detections', 4)In [ ]:
!pip install -q inferenceIn [ ]:
from inference import get_model
model = get_model(model_id="yolov8s-640")
result = model.infer(image)[0]
detections = sv.Detections.from_inference(result)In [12]:
"detections", len(detections)Out [12]:
('detections', 4)In [ ]:
!pip install -q super-gradients
!pip install --upgrade urllib3In [ ]:
from super_gradients.training import models
model = models.get("yolo_nas_s", pretrained_weights="coco")
result = model.predict(image)
detections = sv.Detections.from_yolo_nas(result)In [15]:
"detections", len(detections)Out [15]:
('detections', 7)In [ ]:
from ultralytics import YOLO
model = YOLO("yolo11x.pt")
result = model(image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(result)In [31]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = image.copy()
annotated_image = box_annotator.annotate(annotated_image, detections=detections)
annotated_image = label_annotator.annotate(annotated_image, detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [32]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = [
f"{model.model.names[class_id]} {confidence:.2f}"
for class_id, confidence in zip(detections.class_id, detections.confidence)
]
annotated_image = image.copy()
annotated_image = box_annotator.annotate(annotated_image, detections=detections)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections, labels=labels)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
from ultralytics import YOLO
model = YOLO("yolo11x-seg.pt")
result = model(image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(result)In [34]:
mask_annotator = sv.MaskAnnotator()
annotated_image = image.copy()
annotated_image = mask_annotator.annotate(annotated_image, detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [35]:
detections_index = detections[0]
detections_index_list = detections[[0, 1, 3]]
detections_index_slice = detections[:2]In [36]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
images = []
for d in [detections_index, detections_index_list, detections_index_slice]:
annotated_image = box_annotator.annotate(image.copy(), detections=d)
annotated_image = label_annotator.annotate(annotated_image, detections=d)
images.append(annotated_image)
titles = [
"by index - detections[0]",
"by index list - detections[[0, 1, 3]]",
"by index slice - detections[:2]",
]
sv.plot_images_grid(images=images, titles=titles, grid_size=(1, 3))In [37]:
detections_filtered = detections[detections.class_id == 0]In [38]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections_filtered)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered
)
sv.plot_image(image=annotated_image, size=(8, 8))In [39]:
detections_filtered = detections[detections.confidence > 0.7]In [40]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = []
for class_id, confidence in zip(
detections_filtered.class_id, detections_filtered.confidence
):
labels.append(f"{model.model.names[class_id]} {confidence:.2f}")
annotated_image = box_annotator.annotate(
image.copy(),
detections=detections_filtered,
)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [41]:
detections_filtered = detections[
(detections.class_id != 0) & (detections.confidence > 0.7)
]In [42]:
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
labels = [
f"{class_id} {confidence:.2f}"
for class_id, confidence in zip(
detections_filtered.class_id, detections_filtered.confidence
)
]
annotated_image = box_annotator.annotate(
image.copy(),
detections=detections_filtered,
)
annotated_image = label_annotator.annotate(
annotated_image, detections=detections_filtered, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
!pip install -q supervisionIn [ ]:
!mkdir {HOME}/videosIn [ ]:
%cd {HOME}/videosIn [ ]:
from supervision.assets import download_assets, VideoAssets
download_assets(VideoAssets.VEHICLES)
VIDEO_PATH = VideoAssets.VEHICLES.valueIn [ ]:
sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
In [ ]:
frame_generator = sv.get_video_frames_generator(source_path=VIDEO_PATH)In [ ]:
frame = next(iter(frame_generator))
sv.plot_image(image=frame, size=(8, 8))In [ ]:
RESULT_VIDEO_PATH = f"{HOME}/videos/vehicle-counting-result.mp4"In [ ]:
video_info = sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)
with sv.VideoSink(target_path=RESULT_VIDEO_PATH, video_info=video_info) as sink:
for frame in sv.get_video_frames_generator(source_path=VIDEO_PATH, stride=2):
sink.write_frame(frame=frame)In [ ]:
sv.VideoInfo.from_video_path(video_path=RESULT_VIDEO_PATH)VideoInfo(width=3840, height=2160, fps=25, total_frames=269)
In [ ]:
!pip install -q roboflowIn [ ]:
!mkdir {HOME}/datasets
%cd {HOME}/datasets
import roboflow
from roboflow import Roboflow
roboflow.login()
rf = Roboflow()
project = rf.workspace("roboflow-jvuqo").project("fashion-assistant-segmentation")
dataset = project.version(5).download("yolov8")/content/datasets/images/datasets visit https://app.roboflow.com/auth-cli to get your authentication token. Paste the authentication token here: ·········· loading Roboflow workspace... loading Roboflow project... Dependency ultralytics==8.0.196 is required but found version=8.2.54, to fix: `pip install ultralytics==8.0.196`
Downloading Dataset Version Zip in fashion-assistant-segmentation-5 to yolov8:: 100%|██████████| 122509/122509 [00:03<00:00, 37319.95it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 15%|█▍ | 187/1254 [00:00<00:00, 1860.52it/s]
Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 30%|██▉ | 374/1254 [00:00<00:00, 1609.45it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 43%|████▎ | 538/1254 [00:00<00:00, 1529.93it/s]
In [ ]:
ds = sv.DetectionDataset.from_yolo(
images_directory_path=f"{dataset.location}/train/images",
annotations_directory_path=f"{dataset.location}/train/labels",
data_yaml_path=f"{dataset.location}/data.yaml",
)Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 79%|███████▉ | 989/1254 [00:00<00:00, 2606.19it/s] Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 100%|██████████| 1254/1254 [00:00<00:00, 2505.30it/s]
In [ ]:
len(ds)573
In [ ]:
ds.classes['baseball cap', 'hoodie', 'jacket', 'pants', 'shirt', 'shorts', 'sneaker', 'sunglasses', 'sweatshirt', 't-shirt']
In [ ]:
IMAGE_NAME = list(ds.images.keys())[0]
image = ds.images[IMAGE_NAME]
annotations = ds.annotations[IMAGE_NAME]
box_annotator = sv.BoxAnnotator()
label_annotator = sv.LabelAnnotator()
mask_annotator = sv.MaskAnnotator()
labels = [f"{ds.classes[class_id]}" for class_id in annotations.class_id]
annotated_image = mask_annotator.annotate(image.copy(), detections=annotations)
annotated_image = box_annotator.annotate(annotated_image, detections=annotations)
annotated_image = label_annotator.annotate(
annotated_image, detections=annotations, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
ds_train, ds_test = ds.split(split_ratio=0.8)In [ ]:
"ds_train", len(ds_train), "ds_test", len(ds_test)('ds_train', 458, 'ds_test', 115)In [ ]:
ds_train.as_pascal_voc(
images_directory_path=f"{HOME}/datasets/result/images",
annotations_directory_path=f"{HOME}/datasets/result/labels",
)