Spaces:
Runtime error
Runtime error
TK156
commited on
Commit
·
2e78bab
0
Parent(s):
feat: 深度推定Gradioアプリ
Browse files- Intel DPT-Hybrid-MiDaS
- メモリ最適化済み
- README.md +32 -0
- app.py +140 -0
- requirements.txt +7 -0
README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
title: Depth Estimation API
|
| 3 |
+
emoji: 🌊
|
| 4 |
+
colorFrom: blue
|
| 5 |
+
colorTo: purple
|
| 6 |
+
sdk: gradio
|
| 7 |
+
sdk_version: 4.44.0
|
| 8 |
+
app_file: app.py
|
| 9 |
+
pinned: false
|
| 10 |
+
license: mit
|
| 11 |
+
---
|
| 12 |
+
|
| 13 |
+
# 深度推定・3D可視化 API
|
| 14 |
+
|
| 15 |
+
Intel DPT-Hybrid-MiDaSモデルを使用した深度推定アプリケーションです。
|
| 16 |
+
|
| 17 |
+
## 機能
|
| 18 |
+
|
| 19 |
+
- 画像から深度マップを生成
|
| 20 |
+
- リアルタイム処理
|
| 21 |
+
- 直感的なWebインターフェース
|
| 22 |
+
|
| 23 |
+
## 使用モデル
|
| 24 |
+
|
| 25 |
+
- Intel/dpt-hybrid-midas (Transformers)
|
| 26 |
+
|
| 27 |
+
## 技術スタック
|
| 28 |
+
|
| 29 |
+
- Gradio (Web UI)
|
| 30 |
+
- PyTorch (Deep Learning)
|
| 31 |
+
- Transformers (Hugging Face)
|
| 32 |
+
- OpenCV (画像処理)
|
app.py
ADDED
|
@@ -0,0 +1,140 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
import numpy as np
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
from transformers import DPTImageProcessor, DPTForDepthEstimation
|
| 7 |
+
import cv2
|
| 8 |
+
|
| 9 |
+
# グローバル変数でモデルを保持
|
| 10 |
+
processor = None
|
| 11 |
+
model = None
|
| 12 |
+
|
| 13 |
+
def load_model():
|
| 14 |
+
"""モデルを一度だけ読み込む"""
|
| 15 |
+
global processor, model
|
| 16 |
+
if processor is None or model is None:
|
| 17 |
+
print("Loading depth estimation model...")
|
| 18 |
+
processor = DPTImageProcessor.from_pretrained("Intel/dpt-hybrid-midas")
|
| 19 |
+
model = DPTForDepthEstimation.from_pretrained("Intel/dpt-hybrid-midas")
|
| 20 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 21 |
+
model.to(device)
|
| 22 |
+
model.eval()
|
| 23 |
+
print(f"Model loaded on {device}")
|
| 24 |
+
|
| 25 |
+
def estimate_depth(image):
|
| 26 |
+
"""深度推定を実行"""
|
| 27 |
+
try:
|
| 28 |
+
# モデル読み込み
|
| 29 |
+
load_model()
|
| 30 |
+
|
| 31 |
+
# 画像の前処理
|
| 32 |
+
if isinstance(image, str):
|
| 33 |
+
image = Image.open(image)
|
| 34 |
+
elif isinstance(image, np.ndarray):
|
| 35 |
+
image = Image.fromarray(image)
|
| 36 |
+
|
| 37 |
+
# RGB変換
|
| 38 |
+
if image.mode != 'RGB':
|
| 39 |
+
image = image.convert('RGB')
|
| 40 |
+
|
| 41 |
+
# サイズ制限(メモリ効率のため)
|
| 42 |
+
max_size = 512
|
| 43 |
+
if max(image.size) > max_size:
|
| 44 |
+
image.thumbnail((max_size, max_size), Image.Resampling.LANCZOS)
|
| 45 |
+
|
| 46 |
+
# 推論実行
|
| 47 |
+
inputs = processor(images=image, return_tensors="pt")
|
| 48 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 49 |
+
inputs = {k: v.to(device) for k, v in inputs.items()}
|
| 50 |
+
|
| 51 |
+
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
predicted_depth = outputs.predicted_depth
|
| 54 |
+
|
| 55 |
+
# 深度マップの後処理
|
| 56 |
+
depth = predicted_depth.squeeze().cpu().numpy()
|
| 57 |
+
depth_min = depth.min()
|
| 58 |
+
depth_max = depth.max()
|
| 59 |
+
|
| 60 |
+
if depth_max - depth_min > 0:
|
| 61 |
+
depth_normalized = (depth - depth_min) / (depth_max - depth_min)
|
| 62 |
+
else:
|
| 63 |
+
depth_normalized = np.zeros_like(depth)
|
| 64 |
+
|
| 65 |
+
# カラーマップ適用
|
| 66 |
+
depth_colored = cv2.applyColorMap(
|
| 67 |
+
(depth_normalized * 255).astype(np.uint8),
|
| 68 |
+
cv2.COLORMAP_VIRIDIS
|
| 69 |
+
)
|
| 70 |
+
depth_colored = cv2.cvtColor(depth_colored, cv2.COLOR_BGR2RGB)
|
| 71 |
+
|
| 72 |
+
return Image.fromarray(depth_colored), image
|
| 73 |
+
|
| 74 |
+
except Exception as e:
|
| 75 |
+
print(f"Error in depth estimation: {e}")
|
| 76 |
+
# エラー時は元画像をそのまま返す
|
| 77 |
+
return image, image
|
| 78 |
+
|
| 79 |
+
def process_image(image):
|
| 80 |
+
"""Gradio用の処理関数"""
|
| 81 |
+
if image is None:
|
| 82 |
+
return None, None
|
| 83 |
+
|
| 84 |
+
depth_map, original = estimate_depth(image)
|
| 85 |
+
return original, depth_map
|
| 86 |
+
|
| 87 |
+
# Gradio インターフェース作成
|
| 88 |
+
with gr.Blocks(title="深度推定 API", theme=gr.themes.Soft()) as demo:
|
| 89 |
+
gr.Markdown("# 🌊 深度推定・3D可視化 API")
|
| 90 |
+
gr.Markdown("画像をアップロードして深度マップを生成します")
|
| 91 |
+
|
| 92 |
+
with gr.Row():
|
| 93 |
+
with gr.Column():
|
| 94 |
+
input_image = gr.Image(
|
| 95 |
+
label="入力画像",
|
| 96 |
+
type="pil",
|
| 97 |
+
height=400
|
| 98 |
+
)
|
| 99 |
+
submit_btn = gr.Button("深度推定実行", variant="primary", size="lg")
|
| 100 |
+
|
| 101 |
+
with gr.Column():
|
| 102 |
+
with gr.Tab("元画像"):
|
| 103 |
+
output_original = gr.Image(label="元画像", height=400)
|
| 104 |
+
with gr.Tab("深度マップ"):
|
| 105 |
+
output_depth = gr.Image(label="深度マップ", height=400)
|
| 106 |
+
|
| 107 |
+
with gr.Row():
|
| 108 |
+
gr.Markdown("""
|
| 109 |
+
### 📝 使い方
|
| 110 |
+
1. 画像をアップロードまたはドラッグ&ドロップ
|
| 111 |
+
2. 「深度推定実行」ボタンをクリック
|
| 112 |
+
3. 深度マップが生成されます(紫=近い、黄=遠い)
|
| 113 |
+
|
| 114 |
+
### ⚡ 技術情報
|
| 115 |
+
- モデル: Intel DPT-Hybrid-MiDaS
|
| 116 |
+
- 処理時間: 数秒〜数十秒
|
| 117 |
+
- 最大解像度: 512px(メモリ効率のため)
|
| 118 |
+
""")
|
| 119 |
+
|
| 120 |
+
# イベントハンドラー
|
| 121 |
+
submit_btn.click(
|
| 122 |
+
fn=process_image,
|
| 123 |
+
inputs=[input_image],
|
| 124 |
+
outputs=[output_original, output_depth]
|
| 125 |
+
)
|
| 126 |
+
|
| 127 |
+
# サンプル画像も処理可能
|
| 128 |
+
input_image.change(
|
| 129 |
+
fn=process_image,
|
| 130 |
+
inputs=[input_image],
|
| 131 |
+
outputs=[output_original, output_depth]
|
| 132 |
+
)
|
| 133 |
+
|
| 134 |
+
# アプリケーション起動
|
| 135 |
+
if __name__ == "__main__":
|
| 136 |
+
demo.launch(
|
| 137 |
+
server_name="0.0.0.0",
|
| 138 |
+
server_port=7860,
|
| 139 |
+
share=True
|
| 140 |
+
)
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
transformers
|
| 4 |
+
opencv-python
|
| 5 |
+
pillow
|
| 6 |
+
numpy
|
| 7 |
+
gradio
|