Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,87 +6,67 @@
|
|
6 |
from PIL import Image
|
7 |
import cv2
|
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 |
-
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 |
-
|
54 |
cv2.COLORMAP_VIRIDIS)
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
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()
|
|
|
6 |
from PIL import Image
|
7 |
import cv2
|
8 |
|
9 |
+
# デバイス設定
|
10 |
+
device = "cpu"
|
11 |
+
print(f"Using device: {device}")
|
12 |
+
|
13 |
+
# モデル読み込み
|
14 |
+
model_name = "depth-anything/Depth-Anything-V2-Small-hf"
|
15 |
+
processor = AutoImageProcessor.from_pretrained(model_name)
|
16 |
+
model = AutoModelForDepthEstimation.from_pretrained(model_name)
|
17 |
+
model.to(device)
|
18 |
+
model.eval()
|
19 |
+
print("Model loaded successfully")
|
20 |
+
|
21 |
+
def predict_depth(image):
|
22 |
+
"""深度推定関数"""
|
23 |
+
if image is None:
|
24 |
+
return None, None
|
25 |
+
|
26 |
+
try:
|
27 |
+
# 画像処理
|
28 |
+
if hasattr(image, 'convert'):
|
29 |
+
image = image.convert('RGB')
|
30 |
+
|
31 |
+
# サイズ調整
|
32 |
+
max_size = 256
|
33 |
+
if max(image.size) > max_size:
|
34 |
+
ratio = max_size / max(image.size)
|
35 |
+
new_size = tuple(int(dim * ratio) for dim in image.size)
|
36 |
+
image = image.resize(new_size, Image.Resampling.LANCZOS)
|
37 |
+
|
38 |
+
# 深度推定
|
39 |
+
inputs = processor(images=image, return_tensors="pt")
|
40 |
+
|
41 |
+
with torch.no_grad():
|
42 |
+
outputs = model(**inputs)
|
43 |
+
depth = outputs.predicted_depth.squeeze().cpu().numpy()
|
44 |
+
|
45 |
+
# 可視化
|
46 |
+
depth_norm = ((depth - depth.min()) / (depth.max() -
|
|
|
|
|
|
|
|
|
|
|
47 |
depth.min()) * 255).astype(np.uint8)
|
48 |
+
depth_colored = cv2.applyColorMap(depth_norm,
|
49 |
cv2.COLORMAP_VIRIDIS)
|
50 |
+
depth_colored = cv2.cvtColor(depth_colored, cv2.COLOR_BGR2RGB)
|
51 |
+
depth_image = Image.fromarray(depth_colored)
|
52 |
+
|
53 |
+
return image, depth_image
|
54 |
+
|
55 |
+
except Exception as e:
|
56 |
+
print(f"Error: {e}")
|
57 |
+
return image, None
|
58 |
+
|
59 |
+
# Gradioインターフェース
|
60 |
+
demo = gr.Interface(
|
61 |
+
fn=predict_depth,
|
62 |
+
inputs=gr.Image(type="pil"),
|
63 |
+
outputs=[
|
64 |
+
gr.Image(type="pil", label="Original"),
|
65 |
+
gr.Image(type="pil", label="Depth Map")
|
66 |
+
],
|
67 |
+
title="Depth Estimation API",
|
68 |
+
description="DepthAnything V2による深度推定"
|
69 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
70 |
|
|
|
71 |
if __name__ == "__main__":
|
72 |
demo.launch()
|