击剑动作识别程序全局变量设置 - 调整距离阈值

该代码展示了如何使用Tkinter库创建窗口来调整击剑动作识别程序中的距离阈值 distance_threshold

# 一些全局变量
label = [' '] # 识别输出标签
posess = '' # 视频输出标签
i = 0
t = 30 # 后处理帧数
posess_a = ''
posess_b = ''
a = 1.2 # 辅助拟合精度
distance_threshold = 121 # 设置KNN距离阈值
neighbors = 2 # 邻居
input_path = ''

# 初始化MediaPipe的人体姿势模型
mp_drawing = mp.solutions.drawing_utils
mp_pose = mp.solutions.pose

# 修改distance_threshold
def set_distance_threshold(value):
    global distance_threshold
    distance_threshold = value

# 创建窗口
root = tk.Tk()
root.withdraw()

# 显示使用说明信息
info = '欢迎使用击剑动作识别程序V6.2(作者:冯楠20201888)\n请选择输入视频文件\n支持格式:*.mp4, *.avi\n点击确认选择文件'
tk.messagebox.showinfo('使用说明', info)

# 选择输入视频文件
input_path = filedialog.askopenfilename(title='选择输入视频文件', filetypes=[('视频文件', '*.mp4;*.avi'), ('所有文件', '*.*')])
if not input_path:
    tk.messagebox.showerror('错误', '未选择输入视频文件!')
    exit()

# 关闭窗口
root.destroy()

# 创建调整distance_threshold的窗口
root2 = tk.Tk()
root2.title('调整距离阈值')
root2.geometry('200x100')

# 创建滑动条
scale = tk.Scale(root2, from_=0, to=200, orient=tk.HORIZONTAL, length=150, label='距离阈值', command=set_distance_threshold)
scale.set(distance_threshold)
scale.pack()

# 运行窗口
root2.mainloop()

代码说明:

  1. 全局变量定义: 代码开头定义了程序中使用的全局变量,包括 distance_threshold,用于控制KNN算法的距离阈值。
  2. set_distance_threshold 函数: 该函数用于更新全局变量 distance_threshold 的值。它接受一个参数 value,代表滑动条当前的值。
  3. 创建窗口: 使用 tk.Tk() 创建主窗口,并使用 root.withdraw() 隐藏它。
  4. 显示使用说明: 使用 tk.messagebox.showinfo() 显示程序的使用说明信息。
  5. 选择视频文件: 使用 filedialog.askopenfilename() 允许用户选择输入视频文件。
  6. 创建阈值调整窗口: 使用 tk.Tk() 创建一个新的窗口,并使用 root2.title() 设置标题,使用 root2.geometry() 设置大小。
  7. 创建滑动条: 使用 tk.Scale() 创建一个水平滑动条,用于调整 distance_threshold 的值。
  8. 绑定滑动条事件: 将滑动条的 command 属性绑定到 set_distance_threshold 函数,以便在滑动条值改变时更新 distance_threshold
  9. 运行窗口: 使用 root2.mainloop() 运行阈值调整窗口。

使用方法:

  1. 运行代码。
  2. 点击“确认”按钮选择输入视频文件。
  3. 将弹出的阈值调整窗口中滑动条拖动到想要的值,即可改变 distance_threshold 的值。

注意:

  • 该代码示例使用了 tkinter 库。如果你的系统没有安装该库,请使用 pip install tkinter 命令安装。
  • 该代码仅展示了如何使用Tkinter库来创建一个简单的阈值调整窗口。在实际应用中,你需要将代码整合到你的击剑动作识别程序中。
  • 你需要根据实际需要调整 distance_threshold 的取值范围和滑动条的长度。

原文地址: https://www.cveoy.top/t/topic/gQVO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录