3.5 MiB
Vendored
3.5 MiB
Vendored
In [1]:
!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 [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/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.9.0
In [6]:
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)[0]
detections = sv.Detections.from_yolo_nas(result)In [20]:
"detections", len(detections)Out [20]:
('detections', 7)In [10]:
!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 [32]:
from ultralytics import YOLO
model = YOLO('yolov8l-seg.pt')
result = model(image, verbose=False)[0]
detections = sv.Detections.from_yolov8(result)In [22]:
"detections", len(detections)Out [22]:
('detections', 4)In [31]:
box_annotator = sv.BoxAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [30]:
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 [29]:
mask_annotator = sv.MaskAnnotator()
annotated_image = mask_annotator.annotate(image.copy(), detections=detections)
sv.plot_image(image=annotated_image, size=(8, 8))In [44]:
detections_index = detections[0]
detections_index_list = detections[[0, 1, 3]]
detections_index_slice = detections[:2]In [50]:
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 [58]:
detections_filtered = detections[detections.class_id == 0]In [59]:
box_annotator = sv.BoxAnnotator()
annotated_image = box_annotator.annotate(image.copy(), detections=detections_filtered)
sv.plot_image(image=annotated_image, size=(8, 8))In [51]:
detections_filtered = detections[detections.confidence > 0.75]In [53]:
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 [61]:
detections_filtered = detections[(detections.class_id != 0) & (detections.confidence > 0.75)]In [62]:
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 [63]:
!mkdir {HOME}/videosIn [ ]:
%cd {HOME}/videos
!wget --load-cookies /tmp/cookies.txt "https://docs.google.com/uc?export=download&confirm=$(wget --quiet --save-cookies /tmp/cookies.txt --keep-session-cookies --no-check-certificate 'https://docs.google.com/uc?export=download&id=1pz68D1Gsx80MoPg-_q-IbEdESEmyVLm-' -O- | sed -rn 's/.*confirm=([0-9A-Za-z_]+).*/\1\n/p')&id=1pz68D1Gsx80MoPg-_q-IbEdESEmyVLm-" -O vehicle-counting.mp4 && rm -rf /tmp/cookies.txtIn [66]:
VIDEO_PATH = f"{HOME}/videos/vehicle-counting.mp4"In [67]:
sv.VideoInfo.from_video_path(video_path=VIDEO_PATH)Out [67]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=538)
In [68]:
frame_generator = sv.get_video_frames_generator(source_path=VIDEO_PATH)In [69]:
frame = next(iter(frame_generator))
sv.plot_image(image=frame, size=(8, 8))In [70]:
RESULT_VIDEO_PATH = f"{HOME}/videos/vehicle-counting-result.mp4"In [71]:
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 [72]:
sv.VideoInfo.from_video_path(video_path=RESULT_VIDEO_PATH)Out [72]:
VideoInfo(width=3840, height=2160, fps=25, total_frames=269)
In [ ]:
!pip install -q roboflowIn [74]:
!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 [75]:
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 [76]:
len(ds)Out [76]:
573
In [77]:
ds.classesOut [77]:
['baseball cap', 'hoodie', 'jacket', 'pants', 'shirt', 'shorts', 'sneaker', 'sunglasses', 'sweatshirt', 'tshirt']
In [86]:
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 [87]:
ds_train, ds_test = ds.split(split_ratio=0.8)In [88]:
"ds_train", len(ds_train), "ds_test", len(ds_test)Out [88]:
('ds_train', 458, 'ds_test', 115)In [89]:
ds_train.as_pascal_voc(
images_directory_path=f"{HOME}/datasets/result/images",
annotations_directory_path=f"{HOME}/datasets/result/labels"
)