summaryrefslogtreecommitdiff
path: root/src/mcp_sd15/preprocessors/depth.py
diff options
context:
space:
mode:
authortanaka takashi <isazaka@gmail.com>2026-09-12 18:04:56 +0900
committertanaka takashi <isazaka@gmail.com>2026-09-12 18:04:56 +0900
commitcba15a62829a0fe57a03c8031f981e8f790e2efc (patch)
tree3bc0ef1a243f356f55c2fa97f63c3bfd5c77e8fc /src/mcp_sd15/preprocessors/depth.py
feat: Stable Diffusion 1.x MCPサーバを実装
仕様書・設計書に基づき、ローカルCUDA GPU上のStable Diffusion 1.x互換モデルをMCP経由で利用するサーバを新規実装。 実装内容: - generate_image、generate_guided_image、inpaint_imageの3ツールをMCP SDK v2で公開 - TOMLからcheckpoint、VAE、複数LoRAと重み、ControlNet、生成設定を読み込み - モデル種別ごとの固定ディレクトリ解決と起動時の設定・ファイル存在検証を追加 - Depth/MiDaS、OpenPose body/full、ControlNet Inpaintの前処理とパイプライン切替を実装 - FP16/CUDA、1枚固定生成、ControlNetオンデマンドロード、単一GPU直列実行を実装 - Base64 PNG/JPEG入力の形式・サイズ検証と任意のdata URI入力に対応 - seed、サイズ、モード、時刻、処理時間をPNG metadataへ記録 - 任意のローカルPNG保存、生成エラー分類、CUDA OOM後のキャッシュ回復を追加 - --configと--versionを備えたstdio CLIエントリポイントを追加 開発・運用: - Python 3.11およびuvによる依存・lock管理を構成 - config.example.tomlとセットアップ・運用手順をREADMEへ追加 - Ruff、Pyright、pytestの設定を追加 - 設定、パス解決、schema、画像検証、サービスルーティング、排他制御、MCP画像返却をテスト 検証結果: - pytest: 30 passed - Ruff lint/format: passed - Pyright: 0 errors, 0 warnings - CLI versionおよびMCPクライアント経由のPNG返却を確認 - 実GPU smoke testはCUDA GPUとモデルファイルがないため対象外
Diffstat (limited to 'src/mcp_sd15/preprocessors/depth.py')
-rw-r--r--src/mcp_sd15/preprocessors/depth.py24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/mcp_sd15/preprocessors/depth.py b/src/mcp_sd15/preprocessors/depth.py
new file mode 100644
index 0000000..861b889
--- /dev/null
+++ b/src/mcp_sd15/preprocessors/depth.py
@@ -0,0 +1,24 @@
+"""MiDaS depth-map preprocessing."""
+
+from __future__ import annotations
+
+from typing import cast
+
+from PIL import Image
+
+
+class DepthPreprocessor:
+ """Lazily loads the fixed MiDaS annotator from the local cache."""
+
+ def __init__(self) -> None:
+ self._detector: object | None = None
+
+ def __call__(self, image: Image.Image) -> Image.Image:
+ if self._detector is None:
+ from controlnet_aux import MidasDetector
+
+ self._detector = MidasDetector.from_pretrained(
+ "lllyasviel/Annotators", local_files_only=True
+ )
+ result = cast(Image.Image, self._detector(image)) # type: ignore[operator]
+ return result.convert("RGB")