Files
SnapOtter/apps/docs/zh-TW/tools/image/smart-crop.md
T
SnapOtterandGitHub 4963ab3bbd feat(docs-i18n): translate all documentation into 20 languages
All 181 docs markdown files translated into 20 languages (apps/docs/<locale>/**). Companion to the i18n code PR; admin-merged because the file count exceeds GitHub's per-PR CI trigger limit. Validated by pnpm i18n:check (all surfaces, 0 stale/missing) and a clean all-locale docs build.
2026-07-11 13:52:47 +08:00

3.6 KiB
Raw Blame History

description, i18n_source_hash, i18n_provenance, i18n_output_hash
description i18n_source_hash i18n_provenance i18n_output_hash
主體、臉部與熵感知裁切,使用 Sharp 與 AI 臉部偵測智慧地取景。 acbe1439c6d8 human 84b65e84b154

智慧裁切

智慧主體感知、臉部感知或修剪式裁切。使用 Sharp 的 attention/entropy 策略以及 AI 臉部偵測進行智慧取景。

API 端點

POST /api/v1/tools/image/smart-crop

處理方式: 非同步(回傳 202,透過 SSE 輪詢 /api/v1/jobs/{jobId}/progress 以取得狀態)

模型套件: face-detection200-300 MB- 僅在 face 模式下需要

參數

參數 型別 必填 預設值 說明
file file - 影像檔案(multipart
mode string "subject" 裁切模式:subjectfacetrim。(舊值 attentioncontent 分別對應到 subjecttrim
strategy string "attention" 主體模式的策略:attentionentropy
width integer - 目標寬度(像素)
height integer - 目標高度(像素)
padding integer 0 主體周圍的內距百分比(0-50
facePreset string "head-shoulders" 臉部取景預設:closeuphead-shouldersupper-bodyhalf-body
sensitivity number 0.5 臉部偵測靈敏度(0-1
threshold integer 30 修剪模式用於背景偵測的閾值(0-255)
padToSquare boolean false 將修剪後的結果補滿成正方形
padColor string "#ffffff" 內距的背景顏色
targetSize integer - 補滿後輸出的目標大小(像素)
quality integer - 輸出品質(1-100

範例請求

curl -X POST http://localhost:1349/api/v1/tools/image/smart-crop \
  -F "file=@portrait.jpg" \
  -F 'settings={"mode":"face","width":1080,"height":1080,"facePreset":"head-shoulders"}'

回應

初始回應(202 Accepted

{
  "jobId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
  "async": true
}

進度(SSE 位於 /api/v1/jobs/{jobId}/progress

event: progress
data: {"phase":"processing","percent":50}

最終結果(透過 SSE

{
  "phase": "complete",
  "percent": 100,
  "result": {
    "jobId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890",
    "downloadUrl": "/api/v1/download/{jobId}/portrait_smartcrop.jpg",
    "originalSize": 500000,
    "processedSize": 320000
  }
}

模式

主體模式

使用 Sharp 的 attention 或 entropy 策略找出視覺上最有趣的區域,並圍繞該區域裁切。

臉部模式

使用 AI 偵測臉部,然後依指定的 facePreset 圍繞偵測到的臉部取景裁切。若未偵測到任何臉部,則回退到主體模式(attention 策略)。

修剪模式

移除影像中均勻的邊框/背景。可選擇將結果以指定的背景顏色與目標大小補滿成正方形。

注意事項

  • 此工具使用 createToolRoute 工廠並搭配 executionHint: "long",因此會回傳 202 並附帶 SSE 進度。
  • 臉部模式需要 face-detection 模型套件(200-300 MB)。
  • 主體與修剪模式無需任何 AI 模型套件即可運作。
  • facePreset 決定裁切對偵測到的臉部取景的緊密程度:closeup 最緊,half-body 最寬。
  • 若未指定寬度/高度,則預設為 1080x1080。