Cursor Madrid
Hackathon 3

Hạng mục LibreYOLO

Cursor
×
LibreYOLO
LibreYOLO

Chào mừng các hacker. Hãy tìm hiểu LibreYOLO là gì, làm theo hướng dẫn thiết lập, sao chép ví dụ và bắt đầu xây dựng.

LibreYOLO là gì

LibreYOLO là một engine hiện đại, 100% giấy phép MIT để huấn luyện và triển khai khả năng phát hiện đối tượng tiên tiến nhất. LibreYOLO đưa YOLO trở lại dễ tiếp cận, đúng như những người tạo ra nó luôn mong muốn.

parkour_result.jpg
Kết quả phát hiện của LibreYOLO
✓ Đã phát hiện 1 đối tượng (người)0.023s

Hướng dẫn thiết lập

Dán prompt bên dưới vào Cursor. Prompt sẽ hướng dẫn agent cài đặt LibreYOLO từ mã nguồn và xác minh quá trình cài đặt. Hoạt động trên Windows, macOS và Linux.

Prompt thiết lập
# Install LibreYOLO (Cursor Hackathon, LibreYOLO Track)
#
# Your job: get LibreYOLO installed from source on the user's machine and
# verify it imports. The user is on Windows, macOS, or Linux. Adapt the
# venv activation line to match. Everything else is identical across OSes.
#
# Prereqs the user must already have:
#   - Python 3.10 or newer  (check: python --version  or  python3 --version)
#   - git
# If either is missing, stop and tell the user. Do not silently install.
#
# If LibreYOLO is already installed and importable in the current environment,
# skip the install steps and jump straight to VERIFY.

# 1. Clone the dev branch (we will move to main later)
git clone -b dev https://github.com/LibreYOLO/libreyolo.git
cd libreyolo

# 2. Create a virtual environment (stdlib only, no extra tools required)
python -m venv .venv

# 3. Activate it
# macOS / Linux:
source .venv/bin/activate
# Windows PowerShell (run instead of the line above):
# .venv\Scripts\Activate.ps1
# Windows cmd.exe:
# .venv\Scripts\activate.bat

# 4. Upgrade pip (avoids resolver bugs on older Python installs)
python -m pip install --upgrade pip

# 5. Install LibreYOLO in editable mode
pip install -e .

# 6. VERIFY (should print: LibreYOLO ready)
python -c "from libreyolo import LibreYOLO; print('LibreYOLO ready')"

# OPTIONAL: smoke test on a real image (downloads small weights on first run)
python -c "from libreyolo import LibreYOLO; m = LibreYOLO('LibreYOLO9t.pt'); r = m('https://raw.githubusercontent.com/LibreYOLO/libreyolo/release/libreyolo/assets/parkour.jpg'); print(len(r), 'detections')"

# === FAST PATH (only if the user already has `uv` installed) ===============
# Replaces steps 2-5 with a much faster resolver. Skip if uv is not present.
# Do NOT install uv just for this; pip is already fine.
#   uv venv
#   source .venv/bin/activate           # or .venv\Scripts\Activate.ps1 on Windows
#   uv pip install -e .

# === GPU NOTES =============================================================
# Step 5 installs the default PyTorch wheel from PyPI:
#   - Linux:   CUDA 12.x build (works with NVIDIA GPU out of the box)
#   - macOS:   CPU + MPS (Apple Silicon GPU works automatically)
#   - Windows: CPU only by default
# Windows + NVIDIA GPU: install a CUDA torch wheel BEFORE step 5:
#   pip install --index-url https://download.pytorch.org/whl/cu121 torch torchvision
# Hackathon-safe default: the CPU build is fine for small images and YOLO9t.

# === OPTIONAL EXTRAS (install only if your project needs them) =============
# pip install -e ".[onnx]"        # ONNX export + ONNX Runtime inference
# pip install -e ".[rfdetr]"      # RF-DETR transformer flagship
# pip install -e ".[tensorrt]"    # NVIDIA TensorRT (Linux/Windows + CUDA)
# pip install -e ".[openvino]"    # Intel CPU/GPU/VPU acceleration
# pip install -e ".[ncnn]"        # Lightweight CPU/Vulkan deployment

# === IF THINGS BREAK =======================================================
# Windows: "running scripts is disabled on this system" when activating venv:
#   Set-ExecutionPolicy -Scope CurrentUser RemoteSigned   (run once, then retry)
# macOS / Linux: "python: command not found":
#   use  python3 -m venv .venv  instead, and  python3 -m pip ... for step 4
# torch wheel mismatch / ImportError:
#   find the right wheel at https://pytorch.org/get-started/locally/
# Anything else: ask the user to paste the full error, then debug.

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

Ví dụ 1: Phát hiện đối tượng

Một ví dụ phát hiện hoàn chỉnh tối giản với mô hình chủ lực YOLO9t gọn nhẹ. Sau khi cài đặt LibreYOLO, hãy dán đoạn này vào tệp Python hoặc notebook để xác nhận mọi thứ hoạt động và xem cấu trúc của đối tượng kết quả.

detect.py
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# One factory, any architecture. Auto-detects family, size, and classes.
model = LibreYOLO("LibreYOLO9t.pt")

# Accepts file paths, URLs, PIL, NumPy, tensors, or raw bytes.
result = model(SAMPLE_IMAGE, save=True)

print(result.boxes.xyxy)        # (N, 4) tensor of bounding boxes
print(result.boxes.conf)        # (N,) confidence scores
print(result.names[int(result.boxes.cls[0].item())])  # first class name
print(result.saved_path)        # where the annotated image was saved

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

Ví dụ 2: Phân đoạn bằng RF-DETR

Phân đoạn thực thể với mô hình chủ lực RF-DETR dựa trên transformer. Hậu tố -seg yêu cầu factory tải head phân đoạn, nhờ đó cùng một lời gọi trả về cả bounding box lẫn mặt nạ nhị phân cho từng thực thể.

Cài đặt extra RF-DETR
# Required once for RF-DETR examples
pip install -e ".[rfdetr]"

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

segment.py
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# RF-DETR is the transformer flagship. The "-seg" suffix tells the factory
# to load the segmentation head. Same call shape as detection.
model = LibreYOLO("LibreRFDETRs-seg.pt")

# save=True draws boxes plus translucent mask overlays on top of the image.
result = model(SAMPLE_IMAGE, save=True)

# Boxes still work the same as in detection
print(result.boxes.xyxy)            # (N, 4) bounding boxes
print(result.boxes.cls)             # (N,) class IDs

# Masks are the new bit
print(result.masks.data.shape)      # (N, H, W) binary masks at image resolution
print(result.masks.xy[0].shape)     # polygon contour for the first instance
print(result.saved_path)            # annotated output path

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

Ví dụ 3: Keypoint cơ thể người

Ước lượng tư thế người bằng YOLO-NAS pose. Hậu tố -pose tải head keypoint, trả về bounding box của người cùng 17 keypoint COCO cho mỗi người được phát hiện.

keypoints.py
from libreyolo import LibreYOLO, SAMPLE_IMAGE

# YOLO-NAS pose predicts one person box plus 17 COCO keypoints per person.
model = LibreYOLO("LibreYOLONASs-pose.pt")
result = model(SAMPLE_IMAGE, save=True)

print(result.boxes.xyxy)             # (N, 4) person boxes
print(result.keypoints.xy.shape)     # (N, 17, 2) pixel coordinates
print(result.keypoints.conf.shape)   # (N, 17) keypoint confidence
if len(result):
    print(result.keypoints.xy[0, 0])  # first person's nose keypoint
print(result.saved_path)             # annotated output path

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

Ví dụ 4: Suy luận trên video

Chạy cùng một lời gọi LibreYOLO trên video. Chỉ cần đổi một dòng mô hình để phát hiện, phân đoạn hoặc tìm keypoint; phân đoạn RF-DETR vẫn cần extra rfdetr từ Ví dụ 2.

video.py
from libreyolo import LibreYOLO

# Pick one model:
model = LibreYOLO("LibreYOLO9t.pt")          # detection
# model = LibreYOLO("LibreRFDETRs-seg.pt")   # segmentation
# model = LibreYOLO("LibreYOLONASs-pose.pt") # keypoints

for frame in model("clip.mp4", stream=True, save=True):
    print(frame.frame_idx, len(frame))

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

Ví dụ 5: Theo dõi đối tượng

ByteTrack thêm ID ổn định xuyên suốt các khung hình video. Đây là cách nhanh nhất để đi từ phát hiện đến đếm người, xử lý clip thể thao, phân tích giao thông hoặc bất kỳ tác vụ nào cần biết cùng một đối tượng có còn trên màn hình hay không.

Cài đặt extra theo dõi
pip install libreyolo[tracking]

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.

track.py
from libreyolo import LibreYOLO

model = LibreYOLO("LibreYOLO9t.pt")

for result in model.track(
    "clip.mp4",
    track_conf=0.25,
    iou=0.45,
    save=True,      # writes runs/track/<video_stem>.mp4 by default
    vid_stride=1,
):
    print(result.frame_idx, result.track_id)

Nhấp vào vị trí bất kỳ trong hộp để chọn tất cả hoặc dùng nút Sao chép.