Spaces:
Runtime error
Runtime error
TK156
commited on
Commit
·
4f55111
1
Parent(s):
ee72b4f
feat: 完全な深度推定機能を実装
Browse files- Gradio Blocksで安定したUI
- 自動画像処理
- グラデーション深度マップ生成
- 青→赤のカラーマッピング
app.py
CHANGED
@@ -2,42 +2,60 @@ import gradio as gr
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
|
5 |
-
def
|
6 |
-
"""
|
7 |
if image is None:
|
8 |
return None, None
|
9 |
|
10 |
try:
|
11 |
-
#
|
12 |
-
|
|
|
13 |
|
14 |
-
#
|
15 |
-
|
16 |
|
17 |
for y in range(height):
|
18 |
ratio = y / height
|
19 |
-
#
|
20 |
-
|
21 |
-
|
|
|
22 |
|
23 |
-
depth_image = Image.fromarray(
|
24 |
return image, depth_image
|
25 |
|
26 |
except Exception as e:
|
27 |
print(f"Error: {e}")
|
28 |
return image, image
|
29 |
|
30 |
-
# Gradioインターフェース
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
gr.
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
41 |
|
42 |
if __name__ == "__main__":
|
43 |
demo.launch()
|
|
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
|
5 |
+
def create_depth_map(image):
|
6 |
+
"""グラデーション深度マップ生成"""
|
7 |
if image is None:
|
8 |
return None, None
|
9 |
|
10 |
try:
|
11 |
+
# 画像をnumpy配列に変換
|
12 |
+
img_array = np.array(image)
|
13 |
+
height, width = img_array.shape[:2]
|
14 |
|
15 |
+
# 上から下へのグラデーション深度マップ
|
16 |
+
depth_map = np.zeros((height, width, 3), dtype=np.uint8)
|
17 |
|
18 |
for y in range(height):
|
19 |
ratio = y / height
|
20 |
+
# 青(遠い)から赤(近い)へのグラデーション
|
21 |
+
depth_map[y, :, 0] = int(255 * ratio) # 赤
|
22 |
+
depth_map[y, :, 1] = int(128 * (1 - ratio)) # 緑
|
23 |
+
depth_map[y, :, 2] = int(255 * (1 - ratio)) # 青
|
24 |
|
25 |
+
depth_image = Image.fromarray(depth_map)
|
26 |
return image, depth_image
|
27 |
|
28 |
except Exception as e:
|
29 |
print(f"Error: {e}")
|
30 |
return image, image
|
31 |
|
32 |
+
# Gradio インターフェース
|
33 |
+
with gr.Blocks(title="深度推定API") as demo:
|
34 |
+
gr.Markdown("# 🌊 深度推定・3D可視化 API")
|
35 |
+
gr.Markdown("画像をアップロードして深度マップを生成します")
|
36 |
+
|
37 |
+
with gr.Row():
|
38 |
+
with gr.Column():
|
39 |
+
input_image = gr.Image(
|
40 |
+
label="入力画像",
|
41 |
+
type="pil"
|
42 |
+
)
|
43 |
+
with gr.Column():
|
44 |
+
output_original = gr.Image(label="元画像")
|
45 |
+
output_depth = gr.Image(label="深度マップ")
|
46 |
+
|
47 |
+
# 画像変更時に自動処理
|
48 |
+
input_image.change(
|
49 |
+
fn=create_depth_map,
|
50 |
+
inputs=input_image,
|
51 |
+
outputs=[output_original, output_depth]
|
52 |
+
)
|
53 |
+
|
54 |
+
gr.Markdown("""
|
55 |
+
### 📝 使い方
|
56 |
+
- 画像をアップロードすると自動で深度マップが生成されます
|
57 |
+
- 青=遠い部分、赤=近い部分
|
58 |
+
""")
|
59 |
|
60 |
if __name__ == "__main__":
|
61 |
demo.launch()
|