本部分目前仅提供英文版本。

Cursor Madrid
Hackathon 3

LibreYOLO 赛道

Cursor
×
LibreYOLO
LibreYOLO

欢迎,各位参赛者。了解 LibreYOLO,按照设置教程操作,复制示例,然后开始构建。

什么是 LibreYOLO

LibreYOLO 是一个现代化、100% 采用 MIT 许可的引擎,用于训练和部署先进的目标检测模型。它旨在让 YOLO 重新变得人人可用,一如其创作者最初的设想。

parkour_result.jpg
LibreYOLO 检测结果
✓ 检测到 1 个目标(人)0.023s

设置教程

将下面的提示词粘贴到 Cursor 中。它会指导 agent 从源码安装 LibreYOLO 并验证安装。适用于 Windows、macOS 和 Linux。

设置提示词
# 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.

点击框内任意位置即可全选,也可以使用复制按钮。

示例 1:目标检测

一个使用轻量级旗舰 YOLO9t 的最小端到端检测示例。安装 LibreYOLO 后,将其粘贴到 Python 文件或 notebook 中,确认一切正常并查看 Results 对象的结构。

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

点击框内任意位置即可全选,也可以使用复制按钮。

示例 2:使用 RF-DETR 进行分割

使用旗舰 RF-DETR transformer 进行实例分割。-seg 后缀会让工厂加载分割 head,因此一次调用即可获得检测框和每个实例的二值掩码。

安装 RF-DETR extra
# Required once for RF-DETR examples
pip install -e ".[rfdetr]"

点击框内任意位置即可全选,也可以使用复制按钮。

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

点击框内任意位置即可全选,也可以使用复制按钮。

示例 3:人体关键点

使用 YOLO-NAS pose 进行人体姿态估计。-pose 后缀会加载关键点 head,为每个检测到的人返回检测框和 17 个 COCO 关键点。

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

点击框内任意位置即可全选,也可以使用复制按钮。

示例 4:视频推理

在视频上运行同一个 LibreYOLO 调用。只需替换一行模型代码,即可进行检测、分割或关键点预测;RF-DETR 分割仍需安装示例 2 中的 rfdetr extra。

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))

点击框内任意位置即可全选,也可以使用复制按钮。

示例 5:目标跟踪

ByteTrack 会在视频帧之间添加稳定的 ID。这是从检测快速扩展到人数统计、体育视频分析、交通分析,或任何需要判断屏幕上是否仍为同一目标的任务的最短路径。

安装跟踪 extra
pip install libreyolo[tracking]

点击框内任意位置即可全选,也可以使用复制按钮。

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)

点击框内任意位置即可全选,也可以使用复制按钮。