3.5 MiB
Vendored
3.5 MiB
Vendored
In [ ]:
!nvidia-smiTue Jun 13 13:06:22 2023
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 525.85.12 Driver Version: 525.85.12 CUDA Version: 12.0 |
|-------------------------------+----------------------+----------------------+
| 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 52C 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 [ ]:
import os
HOME = os.getcwd()
print(HOME)/content
In [ ]:
!mkdir {HOME}/imagesIn [ ]:
%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 [ ]:
!pip install -q supervision
import supervision as sv
print(sv.__version__)[?25l [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m0.0/45.4 kB[0m [31m?[0m eta [36m-:--:--[0m [2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m45.4/45.4 kB[0m [31m3.4 MB/s[0m eta [36m0:00:00[0m [?25h0.17.0
In [ ]:
import cv2
IMAGE_PATH = f"{HOME}/images/dog.jpeg"
image = cv2.imread(IMAGE_PATH)In [ ]:
!pip install -q super-gradientsIn [ ]:
from super_gradients.training import models
model = models.get("yolo_nas_l", pretrained_weights="coco")
result = model.predict(image)
detections = sv.Detections.from_yolo_nas(result)In [ ]:
"detections", len(detections)('detections', 7)In [ ]:
!pip install -q ultralytics[?25l [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m0.0/599.6 kB[0m [31m?[0m eta [36m-:--:--[0m [2K [91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m[91m╸[0m [32m593.9/599.6 kB[0m [31m19.5 MB/s[0m eta [36m0:00:01[0m [2K [90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━[0m [32m599.6/599.6 kB[0m [31m14.3 MB/s[0m eta [36m0:00:00[0m [?25h
In [ ]:
from ultralytics import YOLO
model = YOLO("yolov8s.pt")
result = model(image, verbose=False)[0]
detections = sv.Detections.from_ultralytics(result)In [ ]:
"detections", len(detections)('detections', 4)In [ ]:
box_annotator = sv.BoxAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
box_annotator = sv.BoxAnnotator()
labels = [
f"{model.model.names[class_id]} {confidence:.2f}"
for class_id, confidence in zip(detections.class_id, detections.confidence)
]
annotated_image = box_annotator.annotate(
image.copy(), detections=detections, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
mask_annotator = sv.MaskAnnotator(color_lookup=sv.ColorLookup.INDEX)
annotated_image = mask_annotator.annotate(image.copy(), detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
detections_index = detections[0]
detections_index_list = detections[[0, 1, 3]]
detections_index_slice = detections[:2]In [ ]:
box_annotator = sv.BoxAnnotator()
images = [
box_annotator.annotate(image.copy(), detections=d)
for d in [detections_index, detections_index_list, detections_index_slice]
]
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 [ ]:
detections_filtered = detections[detections.class_id == 0]In [ ]:
box_annotator = sv.BoxAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections_filtered)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
detections_filtered = detections[detections.confidence > 0.75]In [ ]:
box_annotator = sv.BoxAnnotator()
labels = [
f"{model.model.names[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, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
detections_filtered = detections[
(detections.class_id != 0) & (detections.confidence > 0.75)
]In [ ]:
box_annotator = sv.BoxAnnotator()
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, labels=labels
)
sv.plot_image(image=annotated_image, size=(8, 8))In [ ]:
!pip install -q supervision[assets]In [ ]:
!mkdir {HOME}/videosIn [ ]:
%cd {HOME}/videosIn [ ]:
from supervision.assets import download_assets, VideoAssets
download_assets(VideoAssets.VEHICLES)
VIDEO_PATH = f"{HOME}/videos/vehicle.mp4"In [ ]:
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 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.20 is required but found version=8.0.117, to fix: `pip install ultralytics<=8.0.20` Downloading Dataset Version Zip in fashion-assistant-segmentation-5 to yolov8: 100% [125448709 / 125448709] bytes
Extracting Dataset Version Zip to fashion-assistant-segmentation-5 in yolov8:: 100%|██████████| 1254/1254 [00:00<00:00, 3283.33it/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",
)In [ ]:
len(ds)573
In [ ]:
ds.classes['baseball cap', 'hoodie', 'jacket', 'pants', 'shirt', 'shorts', 'sneaker', 'sunglasses', 'sweatshirt', 'tshirt']
In [ ]:
IMAGE_NAME = list(ds.images.keys())[0]
image = ds.images[IMAGE_NAME]
annotations = ds.annotations[IMAGE_NAME]
box_annotator = sv.BoxAnnotator()
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, 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",
)