Gangao's picture
Update app.py
80817a3 verified
import gradio as gr
from ai2thor.controller import Controller
from ai2thor.platform import CloudRendering
# 初始化全局模拟器对象,启用 headless 模式
controller = Controller(platform=CloudRendering)
# 用于记录所有动作的列表
action_history = []
def initialize_simulation():
"""
初始化 ai2thor 模拟器,并返回初始场景的截图。
"""
controller.reset('FloorPlan28') # 指定场景
event = controller.step(action='Pass') # 不执行任何动作,只获取初始图像
image = event.frame
action_history.clear() # 清空历史记录
action_history.append((image, "初始化"))
return image, "初始化", action_history
def move_forward():
"""
在 ai2thor 模拟器中执行"向前移动"操作,并返回当前环境的截图。
"""
action = "向前移动"
event = controller.step(action='MoveAhead')
image = event.frame
action_history.append((image, action))
return image, action, action_history
def move_backward():
"""
在 ai2thor 模拟器中执行"向后移动"操作,并返回当前环境的截图。
"""
action = "向后移动"
event = controller.step(action='MoveBack')
image = event.frame
action_history.append((image, action))
return image, action, action_history
def move_left():
"""
在 ai2thor 模拟器中执行"向左旋转"操作,并返回当前环境的截图。
"""
action = "向左旋转"
event = controller.step(action='RotateLeft')
image = event.frame
action_history.append((image, action))
return image, action, action_history
def move_right():
"""
在 ai2thor 模拟器中执行"向右旋转"操作,并返回当前环境的截图。
"""
action = "向右旋转"
event = controller.step(action='RotateRight')
image = event.frame
action_history.append((image, action))
return image, action, action_history
# 使用 Gradio 构建交互界面
with gr.Blocks() as iface:
gr.Markdown("## 具身模型演示")
init_button = gr.Button("初始化")
forward_button = gr.Button("向前移动")
backward_button = gr.Button("向后移动")
left_button = gr.Button("向左旋转")
right_button = gr.Button("向右旋转")
image_output = gr.Image()
current_action_output = gr.Textbox(label="当前动作")
action_history_output = gr.Gallery(label="动作历史")
init_button.click(fn=initialize_simulation, outputs=[image_output, current_action_output, action_history_output])
forward_button.click(fn=move_forward, outputs=[image_output, current_action_output, action_history_output])
backward_button.click(fn=move_backward, outputs=[image_output, current_action_output, action_history_output])
left_button.click(fn=move_left, outputs=[image_output, current_action_output, action_history_output])
right_button.click(fn=move_right, outputs=[image_output, current_action_output, action_history_output])
iface.launch()