─░├žeri─če ge├ž

Basit Yard─▒mc─▒ Programlar

perspektifli kod

Bu ultralytics paketi, i┼č ak─▒┼člar─▒n─▒z─▒ destekleyebilecek, geli┼čtirebilecek ve h─▒zland─▒rabilecek say─▒s─▒z yard─▒mc─▒ programla birlikte gelir. ├çok daha fazlas─▒ mevcut, ancak burada ├žo─ču geli┼čtirici i├žin yararl─▒ olacak baz─▒lar─▒ var. Bunlar ayr─▒ca programlamay─▒ ├Â─črenirken kullanmak i├žin harika bir referans noktas─▒d─▒r.



─░zle: Ultralytics Yard─▒mc─▒ Programlar | Otomatik Ek A├ž─▒klama, Explorer API ve Veri K├╝mesi D├Ân├╝┼čt├╝rme

Veri

YOLO Veri Gezgini

YOLO Ka┼čif eklenmi┼čtir. 8.1.0 y─▒ld├Ân├╝m├╝ g├╝ncellemesi ve veri setinizi daha iyi anlamak i├žin kullanabilece─činiz g├╝├žl├╝ bir ara├žt─▒r. YOLO Explorer'─▒n sa─člad─▒─č─▒ temel i┼člevlerden biri, veri k├╝menizdeki nesne ├Ârneklerini bulmak i├žin metin sorgular─▒n─▒ kullanma becerisidir.

Otomatik Etiketleme / Ek A├ž─▒klamalar

Veri k├╝mesine a├ž─▒klama eklemek ├žok yo─čun kaynak gerektiren ve zaman alan bir s├╝re├žtir. Makul miktarda veri ├╝zerinde e─čitilmi┼č bir YOLO nesne alg─▒lama modeliniz varsa, bunu kullanabilir ve SAM ek verileri (segmentasyon format─▒) otomatik olarak tan─▒mlamak i├žin.

from ultralytics.data.annotator import auto_annotate

auto_annotate(  # (1)!
    data="path/to/new/data",
    det_model="yolov8n.pt",
    sam_model="mobile_sam.pt",
    device="cuda",
    output_dir="path/to/save_labels",
)
  1. Bu fonksiyondan hi├žbir ┼čey d├Ânmez

  2. Referans b├Âl├╝m├╝ne bak─▒n─▒z annotator.auto_annotate i┼člevin nas─▒l ├žal─▒┼čt─▒─č─▒ hakk─▒nda daha fazla bilgi i├žin.

  3. ile birlikte kullan─▒n. fonksiyon segments2boxes nesne alg─▒lama s─▒n─▒rlay─▒c─▒ kutular─▒n─▒ da olu┼čturmak i├žin

COCO YOLO Format─▒na D├Ân├╝┼čt├╝r

COCO JSON ek a├ž─▒klamalar─▒n─▒ uygun YOLO bi├žimine d├Ân├╝┼čt├╝rmek i├žin kullan─▒n. Nesne alg─▒lama (s─▒n─▒rlay─▒c─▒ kutu) veri k├╝meleri i├žin, use_segments ve use_keypoints her ikisi de False

from ultralytics.data.converter import convert_coco

convert_coco(  # (1)!
    "../datasets/coco/annotations/",
    use_segments=False,
    use_keypoints=False,
    cls91to80=True,
)
  1. Bu fonksiyondan hi├žbir ┼čey d├Ânmez

Hakk─▒nda ek bilgi i├žin convert_coco fonksiyon, referans sayfas─▒n─▒ ziyaret edin

S─▒n─▒rlay─▒c─▒ Kutu Boyutlar─▒n─▒ Al

from ultralytics.utils.plotting import Annotator
from ultralytics import YOLO
import cv2

model = YOLO('yolov8n.pt')  # Load pretrain or fine-tune model

# Process the image
source = cv2.imread('path/to/image.jpg')
results = model(source)

# Extract results
annotator = Annotator(source, example=model.names)

for box in results[0].boxes.xyxy.cpu():
    width, height, area = annotator.get_bbox_dimension(box)
    print("Bounding Box Width {}, Height {}, Area {}".format(
        width.item(), height.item(), area.item()))

S─▒n─▒rlay─▒c─▒ Kutular─▒ Segmentlere D├Ân├╝┼čt├╝rme

Mevcut ile x y w h s─▒n─▒rlay─▒c─▒ kutu verilerini kullanarak segmentlere d├Ân├╝┼čt├╝r├╝n yolo_bbox2segment i┼člev. Resimler ve ek a├ž─▒klamalar i├žin dosyalar─▒n bu ┼čekilde d├╝zenlenmesi gerekir:

data
|__ images
    ÔöťÔöÇ 001.jpg
    ÔöťÔöÇ 002.jpg
    ÔöťÔöÇ ..
    ÔööÔöÇ NNN.jpg
|__ labels
    ÔöťÔöÇ 001.txt
    ÔöťÔöÇ 002.txt
    ÔöťÔöÇ ..
    ÔööÔöÇ NNN.txt
from ultralytics.data.converter import yolo_bbox2segment

yolo_bbox2segment(  # (1)!
    im_dir="path/to/images",
    save_dir=None,  # saved to "labels-segment" in images directory
    sam_model="sam_b.pt",
)
  1. Bu fonksiyondan hi├žbir ┼čey d├Ânmez

Ziyaret edin yolo_bbox2segment referans sayfas─▒ i┼člev hakk─▒nda daha fazla bilgi i├žin.

Segmentleri S─▒n─▒rlay─▒c─▒ Kutulara D├Ân├╝┼čt├╝rme

kullanan bir veri k├╝meniz varsa segmentasyon veri k├╝mesi format─▒ bunlar─▒ kolayca yukar─▒-sa─č (veya yatay) s─▒n─▒rlay─▒c─▒ kutulara d├Ân├╝┼čt├╝rebilirsiniz (x y w h bi├žimi) bu i┼člev ile.

import numpy as np

from ultralytics.utils.ops import segments2boxes

segments = np.array(
    [
        [805, 392, 797, 400, ..., 808, 714, 808, 392],
        [115, 398, 113, 400, ..., 150, 400, 149, 298],
        [267, 412, 265, 413, ..., 300, 413, 299, 412],
    ]
)

segments2boxes([s.reshape(-1, 2) for s in segments])
# >>> array([[ 741.66, 631.12, 133.31, 479.25],
#           [ 146.81, 649.69, 185.62, 502.88],
#           [ 281.81, 636.19, 118.12, 448.88]],
#           dtype=float32) # xywh bounding boxes

Bu i┼člevin nas─▒l ├žal─▒┼čt─▒─č─▒n─▒ anlamak i├žin referans sayfas─▒n─▒ ziyaret edin

Yard─▒mc─▒ Programlar

G├Âr├╝nt├╝ S─▒k─▒┼čt─▒rma

En boy oran─▒n─▒ ve kalitesini koruyarak tek bir g├Âr├╝nt├╝ dosyas─▒n─▒ k├╝├ž├╝lt├╝lm├╝┼č boyuta s─▒k─▒┼čt─▒r─▒r. Giri┼č g├Âr├╝nt├╝s├╝ maksimum boyuttan daha k├╝├ž├╝kse, yeniden boyutland─▒r─▒lmaz.

from pathlib import Path

from ultralytics.data.utils import compress_one_image

for f in Path("path/to/dataset").rglob("*.jpg"):
    compress_one_image(f)  # (1)!
  1. Bu fonksiyondan hi├žbir ┼čey d├Ânmez

Otomatik B├Âl├╝nm├╝┼č Veri K├╝mesi

Bir veri k├╝mesini otomatik olarak train/val/test b├Âler ve elde edilen b├Âlmeleri autosplit_*.txt dosyalar. Bu i┼člev rastgele ├Ârnekleme kullanacakt─▒r, bu da fraction e─čitim i├žin arg├╝man.

from ultralytics.data.utils import autosplit

autosplit(  # (1)!
    path="path/to/images",
    weights=(0.9, 0.1, 0.0),  # (train, validation, test) fractional splits
    annotated_only=False,  # split only images with annotation file when True
)
  1. Bu fonksiyondan hi├žbir ┼čey d├Ânmez

Bu i┼člev hakk─▒nda daha fazla bilgi i├žin Referans sayfas─▒na bak─▒n.

Segment-poligondan ─░kili Maskeye

Tek bir ├žokgeni (liste olarak) belirtilen g├Âr├╝nt├╝ boyutunda bir ikili maskeye d├Ân├╝┼čt├╝r├╝r. ├çokgen bi├žiminde [N, 2] ile N say─▒s─▒ olarak (x, y) poligon konturunu tan─▒mlayan noktalar.

Uyar─▒

N her zaman e┼čit olsun.

import numpy as np

from ultralytics.data.utils import polygon2mask

imgsz = (1080, 810)
polygon = np.array([805, 392, 797, 400, ..., 808, 714, 808, 392])  # (238, 2)

mask = polygon2mask(
    imgsz,  # tuple
    [polygon],  # input as list
    color=255,  # 8-bit binary
    downsample_ratio=1,
)

S─▒n─▒rlay─▒c─▒ Kutular

S─▒n─▒rlay─▒c─▒ Kutu (yatay) ├ľrnekleri

S─▒n─▒rlay─▒c─▒ kutu verilerini y├Ânetmek i├žin Bboxes s─▒n─▒f─▒, kutu koordinat bi├žimlendirmesi aras─▒nda d├Ân├╝┼čt├╝rme yapmaya, kutu boyutlar─▒n─▒ ├Âl├žeklendirmeye, alanlar─▒ hesaplamaya, ofsetleri dahil etmeye ve daha fazlas─▒na yard─▒mc─▒ olacakt─▒r!

import numpy as np

from ultralytics.utils.instance import Bboxes

boxes = Bboxes(
    bboxes=np.array(
        [
            [22.878, 231.27, 804.98, 756.83],
            [48.552, 398.56, 245.35, 902.71],
            [669.47, 392.19, 809.72, 877.04],
            [221.52, 405.8, 344.98, 857.54],
            [0, 550.53, 63.01, 873.44],
            [0.0584, 254.46, 32.561, 324.87],
        ]
    ),
    format="xyxy",
)

boxes.areas()
# >>> array([ 4.1104e+05,       99216,       68000,       55772,       20347,      2288.5])

boxes.convert("xywh")
print(boxes.bboxes)
# >>> array(
#     [[ 413.93, 494.05,  782.1, 525.56],
#      [ 146.95, 650.63,  196.8, 504.15],
#      [  739.6, 634.62, 140.25, 484.85],
#      [ 283.25, 631.67, 123.46, 451.74],
#      [ 31.505, 711.99,  63.01, 322.91],
#      [  16.31, 289.67, 32.503,  70.41]]
# )

Bkz. Bboxes referans b├Âl├╝m├╝ daha fazla nitelik ve y├Ântem i├žin kullan─▒labilir.

─░pucu

A┼ča─č─▒daki i┼člevlerin ├žo─čuna (ve daha fazlas─▒na) Bboxes s─▒n─▒f ancak fonksiyonlarla do─črudan ├žal─▒┼čmay─▒ tercih ederseniz, bunlar─▒ ba─č─▒ms─▒z olarak nas─▒l i├že aktaraca─č─▒n─▒za ili┼čkin sonraki alt b├Âl├╝mlere bak─▒n.

├ľl├žeklendirme Kutular─▒

G├Âr├╝nt├╝y├╝ yukar─▒ veya a┼ča─č─▒ ├Âl├žeklendirirken, kar┼č─▒l─▒k gelen s─▒n─▒rlay─▒c─▒ kutu koordinatlar─▒ uygun ┼čekilde ├Âl├žeklendirilebilir. ultralytics.utils.ops.scale_boxes.

import cv2 as cv
import numpy as np

from ultralytics.utils.ops import scale_boxes

image = cv.imread("ultralytics/assets/bus.jpg")
h, w, c = image.shape
resized = cv.resize(image, None, (), fx=1.2, fy=1.2)
new_h, new_w, _ = resized.shape

xyxy_boxes = np.array(
    [
        [22.878, 231.27, 804.98, 756.83],
        [48.552, 398.56, 245.35, 902.71],
        [669.47, 392.19, 809.72, 877.04],
        [221.52, 405.8, 344.98, 857.54],
        [0, 550.53, 63.01, 873.44],
        [0.0584, 254.46, 32.561, 324.87],
    ]
)

new_boxes = scale_boxes(
    img1_shape=(h, w),  # original image dimensions
    boxes=xyxy_boxes,  # boxes from original image
    img0_shape=(new_h, new_w),  # resized image dimensions (scale to)
    ratio_pad=None,
    padding=False,
    xywh=False,
)

print(new_boxes)  # (1)!
# >>> array(
#     [[  27.454,  277.52,  965.98,   908.2],
#     [   58.262,  478.27,  294.42,  1083.3],
#     [   803.36,  470.63,  971.66,  1052.4],
#     [   265.82,  486.96,  413.98,    1029],
#     [        0,  660.64,  75.612,  1048.1],
#     [   0.0701,  305.35,  39.073,  389.84]]
# )
  1. Yeni g├Âr├╝nt├╝ boyutu i├žin ├Âl├žeklendirilmi┼č s─▒n─▒rlay─▒c─▒ kutular

S─▒n─▒rlay─▒c─▒ Kutu Bi├žim D├Ân├╝┼č├╝mleri

XYXY Ôćĺ XYWH

S─▒n─▒rlay─▒c─▒ kutu koordinatlar─▒n─▒ (x1, y1, x2, y2) bi├žiminden (x, y, geni┼člik, y├╝kseklik) bi├žimine d├Ân├╝┼čt├╝r├╝n; burada (x1, y1) sol ├╝st k├Â┼če ve (x2, y2) sa─č alt k├Â┼čedir.

import numpy as np

from ultralytics.utils.ops import xyxy2xywh

xyxy_boxes = np.array(
    [
        [22.878, 231.27, 804.98, 756.83],
        [48.552, 398.56, 245.35, 902.71],
        [669.47, 392.19, 809.72, 877.04],
        [221.52, 405.8, 344.98, 857.54],
        [0, 550.53, 63.01, 873.44],
        [0.0584, 254.46, 32.561, 324.87],
    ]
)
xywh = xyxy2xywh(xyxy_boxes)

print(xywh)
# >>> array(
#     [[ 413.93,  494.05,   782.1, 525.56],
#     [  146.95,  650.63,   196.8, 504.15],
#     [   739.6,  634.62,  140.25, 484.85],
#     [  283.25,  631.67,  123.46, 451.74],
#     [  31.505,  711.99,   63.01, 322.91],
#     [   16.31,  289.67,  32.503,  70.41]]
# )

T├╝m S─▒n─▒rlay─▒c─▒ Kutu D├Ân├╝┼č├╝mleri

from ultralytics.utils.ops import (
    ltwh2xywh,
    ltwh2xyxy,
    xywh2ltwh,  # xywh Ôćĺ top-left corner, w, h
    xywh2xyxy,
    xywhn2xyxy,  # normalized Ôćĺ pixel
    xyxy2ltwh,  # xyxy Ôćĺ top-left corner, w, h
    xyxy2xywhn,  # pixel Ôćĺ normalized
)

for func in (ltwh2xywh, ltwh2xyxy, xywh2ltwh, xywh2xyxy, xywhn2xyxy, xyxy2ltwh, xyxy2xywhn):
    print(help(func))  # print function docstrings

Her i┼člev i├žin dok├╝man dizinine bak─▒n veya ultralytics.utils.ops referans sayfas─▒ her bir i┼člev hakk─▒nda daha fazla bilgi edinmek i├žin.

Çizim

Ek A├ž─▒klamalar ├çizme

Ultralytics her t├╝rl├╝ veriye a├ž─▒klama eklemek i├žin kullan─▒labilen bir Annotator s─▒n─▒f─▒ i├žerir. Nesne alg─▒lama s─▒n─▒rlama kutular─▒, poz anahtar noktalar─▒ ve y├Ânlendirilmi┼č s─▒n─▒rlama kutular─▒ ile kullan─▒m─▒ en kolay olan─▒d─▒r.

Yatay S─▒n─▒rlay─▒c─▒ Kutular

import cv2 as cv
import numpy as np

from ultralytics.utils.plotting import Annotator, colors

names = {  # (1)!
    0: "person",
    5: "bus",
    11: "stop sign",
}

image = cv.imread("ultralytics/assets/bus.jpg")
ann = Annotator(
    image,
    line_width=None,  # default auto-size
    font_size=None,  # default auto-size
    font="Arial.ttf",  # must be ImageFont compatible
    pil=False,  # use PIL, otherwise uses OpenCV
)

xyxy_boxes = np.array(
    [
        [5, 22.878, 231.27, 804.98, 756.83],  # class-idx x1 y1 x2 y2
        [0, 48.552, 398.56, 245.35, 902.71],
        [0, 669.47, 392.19, 809.72, 877.04],
        [0, 221.52, 405.8, 344.98, 857.54],
        [0, 0, 550.53, 63.01, 873.44],
        [11, 0.0584, 254.46, 32.561, 324.87],
    ]
)

for nb, box in enumerate(xyxy_boxes):
    c_idx, *box = box
    label = f"{str(nb).zfill(2)}:{names.get(int(c_idx))}"
    ann.box_label(box, label, color=colors(c_idx, bgr=True))

image_with_bboxes = ann.result()
  1. ─░simler ┼čuradan kullan─▒labilir model.names ne zaman tespit sonu├žlar─▒ ile ├žal─▒┼čma

Y├Ânlendirilmi┼č S─▒n─▒rlay─▒c─▒ Kutular (OBB)

import cv2 as cv
import numpy as np

from ultralytics.utils.plotting import Annotator, colors

obb_names = {10: "small vehicle"}
obb_image = cv.imread("datasets/dota8/images/train/P1142__1024__0___824.jpg")
obb_boxes = np.array(
    [
        [0, 635, 560, 919, 719, 1087, 420, 803, 261],  # class-idx x1 y1 x2 y2 x3 y2 x4 y4
        [0, 331, 19, 493, 260, 776, 70, 613, -171],
        [9, 869, 161, 886, 147, 851, 101, 833, 115],
    ]
)
ann = Annotator(
    obb_image,
    line_width=None,  # default auto-size
    font_size=None,  # default auto-size
    font="Arial.ttf",  # must be ImageFont compatible
    pil=False,  # use PIL, otherwise uses OpenCV
)
for obb in obb_boxes:
    c_idx, *obb = obb
    obb = np.array(obb).reshape(-1, 4, 2).squeeze()
    label = f"{obb_names.get(int(c_idx))}"
    ann.box_label(
        obb,
        label,
        color=colors(c_idx, True),
        rotated=True,
    )

image_with_obb = ann.result()

Bounding Boxes Circle Annotation (Circle Label)

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")

w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
writer = cv2.VideoWriter("Ultralytics circle annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        break

    annotator = Annotator(im0, line_width=2)

    results = model.predict(im0)
    boxes = results[0].boxes.xyxy.cpu()
    clss = results[0].boxes.cls.cpu().tolist()

    for box, cls in zip(boxes, clss):
        x1, y1 = int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)
        annotator.circle_label(box, label=model.names[int(cls)], color=colors(int(cls), True))

    writer.write(im0)
    cv2.imshow("Ultralytics circle annotation", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

writer.release()
cap.release()
cv2.destroyAllWindows()

Bounding Boxes Text Annotation (Text Label)

import cv2

from ultralytics import YOLO
from ultralytics.utils.plotting import Annotator, colors

model = YOLO("yolov8s.pt")
cap = cv2.VideoCapture("path/to/video/file.mp4")

w, h, fps = (int(cap.get(x)) for x in (cv2.CAP_PROP_FRAME_WIDTH, cv2.CAP_PROP_FRAME_HEIGHT, cv2.CAP_PROP_FPS))
writer = cv2.VideoWriter("Ultralytics text annotation.avi", cv2.VideoWriter_fourcc(*"MJPG"), fps, (w, h))

while True:
    ret, im0 = cap.read()
    if not ret:
        break

    annotator = Annotator(im0, line_width=2)

    results = model.predict(im0)
    boxes = results[0].boxes.xyxy.cpu()
    clss = results[0].boxes.cls.cpu().tolist()

    for box, cls in zip(boxes, clss):
        x1, y1 = int((box[0] + box[2]) // 2), int((box[1] + box[3]) // 2)
        annotator.text_label(box, label=model.names[int(cls)], color=colors(int(cls), True))

    writer.write(im0)
    cv2.imshow("Ultralytics text annotation", im0)

    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

writer.release()
cap.release()
cv2.destroyAllWindows()

Bkz. Annotator Referans Sayfas─▒ daha fazla bilgi i├žin.

├çe┼čitli

Kod Profili Olu┼čturma

Kodun ├žal─▒┼čma/i┼členme s├╝resini kontrol etmek i├žin with ya da dekorat├Âr olarak.

from ultralytics.utils.ops import Profile

with Profile(device="cuda:0") as dt:
    pass  # operation to measure

print(dt)
# >>> "Elapsed time is 9.5367431640625e-07 s"

Ultralytics Desteklenen Formatlar

Ultralytics taraf─▒ndan desteklenen resim veya video t├╝rlerinin bi├žimlerini programl─▒ olarak kullanmak istiyor veya buna ihtiya├ž duyuyor musunuz? ─░htiyac─▒n─▒z varsa bu sabitleri kullan─▒n.

from ultralytics.data.utils import IMG_FORMATS, VID_FORMATS

print(IMG_FORMATS)
# {'tiff', 'pfm', 'bmp', 'mpo', 'dng', 'jpeg', 'png', 'webp', 'tif', 'jpg'}

print(VID_FORMATS)
# {'avi', 'mpg', 'wmv', 'mpeg', 'm4v', 'mov', 'mp4', 'asf', 'mkv', 'ts', 'gif', 'webm'}

B├Âl├╝nebilir Yap

En yak─▒n tam say─▒y─▒ hesaplar x ile b├Âl├╝nd├╝─č├╝nde e┼čit olarak b├Âl├╝nebilir hale getirmek i├žin y.

from ultralytics.utils.ops import make_divisible

make_divisible(7, 3)
# >>> 9
make_divisible(7, 2)
# >>> 8


Created 2024-02-20, Updated 2024-06-20
Authors: glenn-jocher (8), ambitious-octopus (1), IvorZhu331 (1), RizwanMunawar (1), Burhan-Q (2)

Yorumlar