Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,91 +1,92 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
import torch
|
| 3 |
-
from transformers import AutoImageProcessor,
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import
|
| 7 |
-
import
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import torch
|
| 3 |
+
from transformers import AutoImageProcessor,
|
| 4 |
+
AutoModelForDepthEstimation
|
| 5 |
+
import numpy as np
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import cv2
|
| 8 |
+
|
| 9 |
+
class DepthEstimationAPI:
|
| 10 |
+
def __init__(self):
|
| 11 |
+
self.device = "cpu" # Hugging Face Spacesは無料版でCPUのみ
|
| 12 |
+
print(f"Using device: {self.device}")
|
| 13 |
+
|
| 14 |
+
model_name = "depth-anything/Depth-Anything-V2-Small-hf"
|
| 15 |
+
self.processor = AutoImageProcessor.from_pretrained(model_name)
|
| 16 |
+
self.model =
|
| 17 |
+
AutoModelForDepthEstimation.from_pretrained(model_name)
|
| 18 |
+
self.model.to(self.device)
|
| 19 |
+
self.model.eval()
|
| 20 |
+
print("Model loaded successfully")
|
| 21 |
+
|
| 22 |
+
def predict(self, image_input):
|
| 23 |
+
"""Process image and return depth map"""
|
| 24 |
+
if image_input is None:
|
| 25 |
+
return None, None
|
| 26 |
+
|
| 27 |
+
try:
|
| 28 |
+
# PILイメージに変換
|
| 29 |
+
if hasattr(image_input, 'convert'):
|
| 30 |
+
image = image_input.convert('RGB')
|
| 31 |
+
else:
|
| 32 |
+
image = Image.open(image_input).convert('RGB')
|
| 33 |
+
|
| 34 |
+
# サイズ調整(メモリ節約)
|
| 35 |
+
max_size = 256
|
| 36 |
+
if max(image.size) > max_size:
|
| 37 |
+
ratio = max_size / max(image.size)
|
| 38 |
+
new_size = tuple(int(dim * ratio) for dim in
|
| 39 |
+
image.size)
|
| 40 |
+
image = image.resize(new_size,
|
| 41 |
+
Image.Resampling.LANCZOS)
|
| 42 |
+
|
| 43 |
+
# 深度推定
|
| 44 |
+
inputs = self.processor(images=image, return_tensors="pt")
|
| 45 |
+
|
| 46 |
+
with torch.no_grad():
|
| 47 |
+
outputs = self.model(**inputs)
|
| 48 |
+
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
| 49 |
+
|
| 50 |
+
# 深度マップ可視化
|
| 51 |
+
depth_normalized = ((depth - depth.min()) / (depth.max() -
|
| 52 |
+
depth.min()) * 255).astype(np.uint8)
|
| 53 |
+
depth_colored = cv2.applyColorMap(depth_normalized,
|
| 54 |
+
cv2.COLORMAP_VIRIDIS)
|
| 55 |
+
depth_colored = cv2.cvtColor(depth_colored,
|
| 56 |
+
cv2.COLOR_BGR2RGB)
|
| 57 |
+
depth_image = Image.fromarray(depth_colored)
|
| 58 |
+
|
| 59 |
+
return image, depth_image
|
| 60 |
+
|
| 61 |
+
except Exception as e:
|
| 62 |
+
print(f"Error in prediction: {e}")
|
| 63 |
+
return image_input, None
|
| 64 |
+
|
| 65 |
+
# APIインスタンス初期化
|
| 66 |
+
api = DepthEstimationAPI()
|
| 67 |
+
|
| 68 |
+
# Gradioインターフェース作成
|
| 69 |
+
with gr.Blocks(title="Depth Estimation API") as demo:
|
| 70 |
+
gr.Markdown("# 深度推定 API")
|
| 71 |
+
gr.Markdown("DepthAnything V2を使用したAI深度推定")
|
| 72 |
+
|
| 73 |
+
with gr.Row():
|
| 74 |
+
with gr.Column():
|
| 75 |
+
input_image = gr.Image(type="pil",
|
| 76 |
+
label="画像をアップロード")
|
| 77 |
+
submit_btn = gr.Button("深度マップ生成", variant="primary")
|
| 78 |
+
|
| 79 |
+
with gr.Column():
|
| 80 |
+
output_original = gr.Image(type="pil", label="元画像")
|
| 81 |
+
output_depth = gr.Image(type="pil", label="深度マップ")
|
| 82 |
+
|
| 83 |
+
# ボタンクリックで処理実行
|
| 84 |
+
submit_btn.click(
|
| 85 |
+
fn=api.predict,
|
| 86 |
+
inputs=[input_image],
|
| 87 |
+
outputs=[output_original, output_depth]
|
| 88 |
+
)
|
| 89 |
+
|
| 90 |
+
# Hugging Face Spaces用起動設定
|
| 91 |
+
if __name__ == "__main__":
|
| 92 |
+
demo.launch()
|