"""
EZAI-Go2 视频流获取示例
演示如何初始化Go2机器狗并获取实时视频流
"""

import os
import time
import cv2
import ezai.go2 as go2
import ezai.ui as ui









def get_video_stream():
    """获取并显示Go2视频流"""
    while window.running:
        # 获取Go2机器狗视频流
        image = go2_robot.read_image()
        
        if image is not None:
            # 显示视频流
            image_label.set_image(image)
            status_label.set_text(f"状态: 视频流正常 | 分辨率: {image.shape[1]}x{image.shape[0]}")
        else:
            status_label.set_text("状态: 无法获取视频流")
        
        # 控制帧率，避免过度占用CPU
        time.sleep(0.03)  # 约30fps
        
        
        



def main():
    """主函数"""
    # 设置显示环境
    os.environ["DISPLAY"] = ":0"
    
    # 创建UI窗口
    window = ui.OpenCVUI("Go2 Video Stream", 640, 520)
    
    # 显示视频流的标签
    image_label = ui.Label(10, 10, 620, 400, align="center")
    window.add_widget(image_label)
    
    # 显示状态信息的标签
    status_label = ui.Label(10, 420, 620, 40, text="状态: 初始化中...", align="center", font_size=16)
    window.add_widget(status_label)
    
    # 退出按钮
    quit_button = ui.Button(270, 470, 100, 30, "退出", font_size=14, command=window.destroy)
    window.add_widget(quit_button)
    
    # 初始化Go2机器狗
    print("正在初始化Go2机器狗...")
    go2_robot = go2.Go2()
    
    # 显示网络接口信息
    if go2_robot.interface:
        print(f"找到网络接口: {go2_robot.interface}")
        status_label.set_text(f"状态: 已连接到 {go2_robot.interface}")
    else:
        print("未找到Go2网络接口")
        status_label.set_text("状态: 未找到Go2网络接口")
    
    # 初始化连接
    try:
        go2_robot.init()
        print("Go2初始化成功")
        status_label.set_text(f"状态: Go2已连接 ({go2_robot.interface})")
    except Exception as e:
        print(f"Go2初始化失败: {e}")
        status_label.set_text(f"状态: 连接失败 - {e}")
    
    # 启动视频流线程
    from threading import Thread
    thread = Thread(target=get_video_stream)
    thread.daemon = True
    thread.start()
    
    # 启动主循环
    print("视频流已启动，按退出按钮关闭")
    window.mainloop()

if __name__ == "__main__":
    main()





