Spaces:
Runtime error
Runtime error
| import gradio as gr | |
| import numpy as np | |
| from PIL import Image | |
| def create_depth_map(image): | |
| """シンプルな深度マップ生成""" | |
| if image is None: | |
| return None | |
| try: | |
| # 画像をnumpy配列に変換 | |
| img_array = np.array(image) | |
| height, width = img_array.shape[:2] | |
| # 上から下へのグラデーション(上=遠い/青、下=近い/赤) | |
| depth_map = np.zeros((height, width, 3), dtype=np.uint8) | |
| for y in range(height): | |
| ratio = y / height | |
| # 青から赤へのグラデーション | |
| depth_map[y, :, 0] = int(255 * ratio) # 赤 | |
| depth_map[y, :, 2] = int(255 * (1 - ratio)) # 青 | |
| return Image.fromarray(depth_map) | |
| except Exception as e: | |
| print(f"Error: {e}") | |
| return image | |
| # Gradioインターフェース | |
| with gr.Blocks(title="深度推定API") as demo: | |
| gr.Markdown("# 🌊 深度推定・3D可視化 API") | |
| gr.Markdown("画像をアップロードして深度マップを生成します") | |
| with gr.Row(): | |
| with gr.Column(): | |
| input_image = gr.Image( | |
| label="入力画像", | |
| type="pil", | |
| height=300 | |
| ) | |
| with gr.Column(): | |
| output_depth = gr.Image( | |
| label="深度マップ(上=遠い/青、下=近い/赤)", | |
| height=300 | |
| ) | |
| # 画像変更時に自動処理 | |
| input_image.change( | |
| fn=create_depth_map, | |
| inputs=input_image, | |
| outputs=output_depth | |
| ) | |
| gr.Markdown(""" | |
| ### 📝 使い方 | |
| 1. 画像をアップロードまたはドラッグ&ドロップ | |
| 2. 深度マップが自動生成されます | |
| 3. 青=遠い部分、赤=近い部分 | |
| """) | |
| if __name__ == "__main__": | |
| demo.launch() |