Spaces:
Runtime error
Runtime error
TK156
commited on
Commit
·
b82f603
1
Parent(s):
ffa72b5
fix: 最小構成でエラー修正
Browse files- OpenCV削除
- 最小限の依存関係
- シンプルなグラデーション深度マップ
- app.py +28 -94
- requirements.txt +0 -1
app.py
CHANGED
@@ -1,114 +1,48 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
from PIL import Image
|
4 |
-
import cv2
|
5 |
|
6 |
-
def
|
7 |
-
"""
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
def estimate_depth(image):
|
12 |
-
"""軽量な深度推定(グラデーションベース)"""
|
13 |
try:
|
14 |
-
|
15 |
-
|
16 |
-
# 画像の前処理
|
17 |
-
if isinstance(image, str):
|
18 |
-
image = Image.open(image)
|
19 |
-
elif isinstance(image, np.ndarray):
|
20 |
-
image = Image.fromarray(image)
|
21 |
|
22 |
-
#
|
23 |
-
|
24 |
-
|
|
|
25 |
|
26 |
-
#
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
|
31 |
-
|
32 |
-
img_array = np.array(image)
|
33 |
-
height, width = img_array.shape[:2]
|
34 |
|
35 |
-
|
36 |
-
depth_gradient = np.linspace(0, 1, height)
|
37 |
-
depth_map = np.tile(depth_gradient.reshape(-1, 1), (1, width))
|
38 |
-
|
39 |
-
# カラーマップ適用
|
40 |
-
depth_colored = cv2.applyColorMap(
|
41 |
-
(depth_map * 255).astype(np.uint8),
|
42 |
-
cv2.COLORMAP_VIRIDIS
|
43 |
-
)
|
44 |
-
depth_colored = cv2.cvtColor(depth_colored, cv2.COLOR_BGR2RGB)
|
45 |
-
|
46 |
-
return Image.fromarray(depth_colored), image
|
47 |
|
48 |
except Exception as e:
|
49 |
-
print(f"Error
|
50 |
-
# エラー時は元画像をそのまま返す
|
51 |
return image, image
|
52 |
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
depth_map, original = estimate_depth(image)
|
59 |
-
return original, depth_map
|
60 |
-
|
61 |
-
# Gradio インターフェース作成
|
62 |
-
with gr.Blocks(title="深度推定 API", theme=gr.themes.Soft()) as demo:
|
63 |
-
gr.Markdown("# 🌊 深度推定・3D可視化 API")
|
64 |
-
gr.Markdown("画像をアップロードして深度マップを生成します")
|
65 |
-
|
66 |
-
with gr.Row():
|
67 |
-
with gr.Column():
|
68 |
-
input_image = gr.Image(
|
69 |
-
label="入力画像",
|
70 |
-
type="pil",
|
71 |
-
height=400
|
72 |
-
)
|
73 |
-
submit_btn = gr.Button("深度推定実行", variant="primary", size="lg")
|
74 |
-
|
75 |
-
with gr.Column():
|
76 |
-
with gr.Tab("元画像"):
|
77 |
-
output_original = gr.Image(label="元画像", height=400)
|
78 |
-
with gr.Tab("深度マップ"):
|
79 |
-
output_depth = gr.Image(label="深度マップ", height=400)
|
80 |
|
81 |
with gr.Row():
|
82 |
-
gr.
|
83 |
-
|
84 |
-
1. 画像をアップロードまたはドラッグ&ドロップ
|
85 |
-
2. 「深度推定実行」ボタンをクリック
|
86 |
-
3. 深度マップが生成されます(紫=近い、黄=遠い)
|
87 |
-
|
88 |
-
### ⚡ 技術情報
|
89 |
-
- モデル: Intel DPT-Hybrid-MiDaS
|
90 |
-
- 処理時間: 数秒〜数十秒
|
91 |
-
- 最大解像度: 512px(メモリ効率のため)
|
92 |
-
""")
|
93 |
-
|
94 |
-
# イベントハンドラー
|
95 |
-
submit_btn.click(
|
96 |
-
fn=process_image,
|
97 |
-
inputs=[input_image],
|
98 |
-
outputs=[output_original, output_depth]
|
99 |
-
)
|
100 |
|
101 |
-
# サンプル画像も処理可能
|
102 |
input_image.change(
|
103 |
-
fn=
|
104 |
-
inputs=
|
105 |
-
outputs=[
|
106 |
)
|
107 |
|
108 |
-
# アプリケーション起動
|
109 |
if __name__ == "__main__":
|
110 |
-
demo.launch(
|
111 |
-
server_name="0.0.0.0",
|
112 |
-
server_port=7860,
|
113 |
-
share=True
|
114 |
-
)
|
|
|
1 |
import gradio as gr
|
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 |
+
# 画像サイズ取得
|
12 |
+
width, height = image.size
|
|
|
|
|
|
|
|
|
|
|
13 |
|
14 |
+
# 上から下へのグラデーション
|
15 |
+
depth_array = np.zeros((height, width), dtype=np.uint8)
|
16 |
+
for y in range(height):
|
17 |
+
depth_array[y, :] = int(255 * y / height)
|
18 |
|
19 |
+
# カラー深度マップ作成(青から赤へ)
|
20 |
+
depth_colored = np.zeros((height, width, 3), dtype=np.uint8)
|
21 |
+
depth_colored[:, :, 0] = 255 - depth_array # 赤チャンネル
|
22 |
+
depth_colored[:, :, 2] = depth_array # 青チャンネル
|
23 |
|
24 |
+
depth_image = Image.fromarray(depth_colored)
|
|
|
|
|
25 |
|
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 |
+
input_image = gr.Image(label="入力画像", type="pil")
|
39 |
+
output_depth = gr.Image(label="深度マップ")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
40 |
|
|
|
41 |
input_image.change(
|
42 |
+
fn=create_depth_map,
|
43 |
+
inputs=input_image,
|
44 |
+
outputs=[input_image, output_depth]
|
45 |
)
|
46 |
|
|
|
47 |
if __name__ == "__main__":
|
48 |
+
demo.launch()
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,4 +1,3 @@
|
|
1 |
-
opencv-python-headless
|
2 |
pillow
|
3 |
numpy
|
4 |
gradio
|
|
|
|
|
1 |
pillow
|
2 |
numpy
|
3 |
gradio
|