当前位置: 首页 > news >正文

音频分类标注工具

pyqt 分类标注工具:

import glob
import sys
import json
import os
from PyQt5.QtWidgets import (QApplication, QMainWindow, QTableWidget, QTableWidgetItem,QSplitter, QVBoxLayout, QWidget, QPushButton, QRadioButton,QButtonGroup, QLabel, QHBoxLayout, QMessageBox
)
from PyQt5.QtCore import Qt, QUrl
from PyQt5.QtGui import QColor
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContentclass AudioAnnotator(QMainWindow):def __init__(self,base_dir):super().__init__()self.setWindowTitle("MP3标注工具 (单选标注)")self.setGeometry(100, 100, 1000, 600)  # 增大窗口尺寸# 初始化媒体播放器self.player = QMediaPlayer()# 主布局main_widget = QWidget()self.setCentralWidget(main_widget)layout = QVBoxLayout()main_widget.setLayout(layout)# 分割左右区域splitter = QSplitter(Qt.Horizontal)# 左侧:音频文件列表(表格)self.table = QTableWidget()self.table.setColumnCount(2)self.table.setHorizontalHeaderLabels(["音频文件", "标注状态"])self.table.setEditTriggers(QTableWidget.NoEditTriggers)self.table.cellClicked.connect(self.play_audio)self.table.setColumnWidth(0, 400)  # 文件名列宽self.table.setColumnWidth(1, 150)  # 状态列宽# 右侧:单选标注区域right_panel = QWidget()right_layout = QVBoxLayout()# 单选按钮组self.radio_group = QButtonGroup()self.radio_1 = QRadioButton("small (1)")self.radio_2 = QRadioButton("normal (2)")self.radio_3 = QRadioButton("3 (3)")self.radio_group.addButton(self.radio_1, 1)self.radio_group.addButton(self.radio_2, 2)self.radio_group.addButton(self.radio_3, 3)self.radio_group.buttonClicked[int].connect(self.on_radio_selected)right_layout.addWidget(QLabel("标注选项:"))right_layout.addWidget(self.radio_1)right_layout.addWidget(self.radio_2)right_layout.addWidget(self.radio_3)right_layout.addStretch()right_panel.setLayout(right_layout)# 底部按钮self.btn_save = QPushButton("保存标注")self.btn_save.clicked.connect(self.save_annotation)# 添加到布局splitter.addWidget(self.table)splitter.addWidget(right_panel)layout.addWidget(splitter)layout.addWidget(self.btn_save)self.json_path=os.path.basename(base_dir)+'.json'files = glob.glob(base_dir + "/*.mp3")self.audio_files = files# 加载历史标注self.annotations = {}self.load_annotations()self.load_audio_files()def on_radio_selected(self, checked_id):print(f"选中了按钮 ID: {checked_id}")current_row = self.table.currentRow()if current_row >= 0:audio_file = self.audio_files[current_row]checked_id = self.radio_group.checkedId()if checked_id == -1:QMessageBox.warning(self, "警告", "请选择标注选项!")return# 更新标注字典self.annotations[audio_file] = checked_id# 保存到JSON文件with open(self.json_path, "w", encoding="utf-8") as f:json.dump(self.annotations, f, ensure_ascii=False, indent=4)# 更新表格显示self.load_audio_files()def load_audio_files(self):"""加载音频文件到表格并显示标注状态"""self.table.setRowCount(len(self.audio_files))# 标注选项映射label_map = {1: "small", 2: "normal", 3: "3"}for i, file in enumerate(self.audio_files):# 文件名列file_item = QTableWidgetItem(file)self.table.setItem(i, 0, file_item)# 状态列status_item = QTableWidgetItem()if file in self.annotations:label_id = self.annotations[file]status_item.setText(f"已标注: {label_map.get(label_id, '未知')}")file_item.setBackground(QColor(200, 255, 200))  # 浅绿色背景status_item.setBackground(QColor(200, 255, 200))else:status_item.setText("未标注")file_item.setBackground(QColor(255, 200, 200))  # 浅红色背景status_item.setBackground(QColor(255, 200, 200))self.table.setItem(i, 1, status_item)def play_audio(self, row, column):if column > 0:  # 只在点击第一列时触发returnfile_path = self.audio_files[row]print('start play',file_path)media_content = QMediaContent(QUrl.fromLocalFile(file_path))self.player.setMedia(media_content)self.player.play()# 加载该音频的历史标注if file_path in self.annotations:checked_id = self.annotations[file_path]self.radio_group.button(checked_id).setChecked(True)else:self.radio_group.setExclusive(False)for btn in self.radio_group.buttons():btn.setChecked(False)self.radio_group.setExclusive(True)def save_annotation(self):"""保存标注到JSON文件"""current_row = self.table.currentRow()if current_row >= 0:audio_file = self.audio_files[current_row]checked_id = self.radio_group.checkedId()if checked_id == -1:QMessageBox.warning(self, "警告", "请选择标注选项!")return# 更新标注字典self.annotations[audio_file] = checked_id# 保存到JSON文件with open(self.json_path, "w", encoding="utf-8") as f:json.dump(self.annotations, f, ensure_ascii=False, indent=4)# 更新表格显示self.load_audio_files()QMessageBox.information(self, "成功", f"标注已保存:{os.path.basename(audio_file)} -> {checked_id}")else:QMessageBox.warning(self, "警告", "请先选中音频文件!")def load_annotations(self):"""加载历史标注文件"""if os.path.exists(self.json_path):try:with open(self.json_path, "r", encoding="utf-8") as f:self.annotations = json.load(f)# 转换键为绝对路径(如果保存的是相对路径)base_dir = os.path.dirname(os.path.abspath(self.json_path))fixed_annotations = {}for k, v in self.annotations.items():if not os.path.isabs(k):fixed_path = os.path.join(base_dir, k)if os.path.exists(fixed_path):fixed_annotations[fixed_path] = velse:fixed_annotations[k] = velse:fixed_annotations[k] = vself.annotations = fixed_annotationsexcept Exception as e:QMessageBox.warning(self, "警告", f"加载标注文件出错: {str(e)}")self.annotations = {}if __name__ == "__main__":base_dir = r"/Users/lbg/Documents/data/audio_0817_low"app = QApplication(sys.argv)window = AudioAnnotator(base_dir)window.show()sys.exit(app.exec_())

http://www.dtcms.com/a/335436.html

相关文章:

  • 91.解码方法
  • GaussDB 数据库架构师修炼(十三)安全管理(5)-全密态数据库
  • 17.5 展示购物车缩略信息
  • JMeter(进阶篇)
  • 3D打印——给开发板做外壳
  • 蓝凌EKP产品:JSP 性能优化和 JSTL/EL要点检查列表
  • Trae 辅助下的 uni-app 跨端小程序工程化开发实践分享
  • Docker之自定义jkd镜像上传阿里云
  • Spring AI 集成阿里云百炼平台
  • vscode无法检测到typescript环境解决办法
  • SpringCloud 03 负载均衡
  • 向量数据库基础和实践 (Faiss)
  • QT 基础聊天应用项目文档
  • Flutter vs Pygame 桌面应用开发对比分析
  • Android原生(Kotlin)与Flutter混合开发 - 设备控制与状态同步解决方案
  • 安卓开发者自学鸿蒙开发2页面高级技巧
  • 第一阶段总结:你的第一个3D网页
  • 【牛客刷题】成绩统计与发短信问题详解
  • OpenMemory MCP发布!AI记忆本地共享,Claude、Cursor一键同步效率翻倍!
  • 【FreeRTOS】刨根问底6: 应该如何防止任务栈溢出?
  • JavaScript性能优化实战(四):资源加载优化
  • FreeRTOS源码分析八:timer管理(一)
  • Hunyuan-GameCraft:基于混合历史条件的高动态交互游戏视频生成
  • 健身房预约系统SSM+Mybatis实现(三、校验 +页面完善+头像上传)
  • 基于Node.js+Express的电商管理平台的设计与实现/基于vue的网上购物商城的设计与实现/基于Node.js+Express的在线销售系统
  • Visual Studio Code 基础设置指南
  • iSCSI服务配置全指南(含服务器与客户端)
  • 12.web api 3
  • Docker入门:容器化技术的第一堂课
  • Chrome插件开发实战:todoList 插件