976 lines
48 KiB
Plaintext
976 lines
48 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"# 读取 JSON 文件\n",
|
|
"def remove_original_answer(input_file, output_file):\n",
|
|
" # 读取 JSON 文件\n",
|
|
" with open(input_file, 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
" \n",
|
|
" # 遍历所有视频片段\n",
|
|
" for video_key in data:\n",
|
|
" # 如果存在 original_answer 字段,则删除它\n",
|
|
" if 'original_answer' in data[video_key]:\n",
|
|
" del data[video_key]['original_answer']\n",
|
|
" \n",
|
|
" # 将修改后的数据写入新文件\n",
|
|
" with open(output_file, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(data, f, ensure_ascii=False, indent=2)\n",
|
|
"\n",
|
|
"# 使用示例\n",
|
|
"input_file = '球机沙发正面.json' # 输入文件名\n",
|
|
"output_file = '球机沙发正面_cleaned.json' # 输出文件名\n",
|
|
"\n",
|
|
"remove_original_answer(input_file, output_file)\n",
|
|
"print(f\"已成功删除所有 original_answer 字段,并保存到 {output_file}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"# 需要处理的文件列表\n",
|
|
"files = [\n",
|
|
" \"result/qwen/球机沙发正面_20241231_0200.json\",\n",
|
|
" \"result/qwen/左侧吃饭2_20241231_1612.json\",\n",
|
|
" \"result/qwen/左侧吃饭1_20241231_1423.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2300.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2010.json\",\n",
|
|
" \"result/qwen/球机椅子左侧面_20241231_0923.json\",\n",
|
|
" \"result/qwen/右上角吃饭_20241231_1232.json\"\n",
|
|
"]\n",
|
|
"\n",
|
|
"def remove_actions(data):\n",
|
|
" \"\"\"递归删除字典中的 'actions' 字段\"\"\"\n",
|
|
" if isinstance(data, dict):\n",
|
|
" if 'actions' in data:\n",
|
|
" data['actions'] = [] # 清空 actions 列表\n",
|
|
" for value in data.values():\n",
|
|
" remove_actions(value)\n",
|
|
" elif isinstance(data, list):\n",
|
|
" for item in data:\n",
|
|
" remove_actions(item)\n",
|
|
"\n",
|
|
"# 处理每个文件\n",
|
|
"for file_path in files:\n",
|
|
" try:\n",
|
|
" # 读取 JSON 文件\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
" \n",
|
|
" # 删除 actions 字段内容\n",
|
|
" remove_actions(data)\n",
|
|
" \n",
|
|
" # 写回文件\n",
|
|
" with open(file_path, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(data, f, ensure_ascii=False, indent=2)\n",
|
|
" \n",
|
|
" print(f\"Successfully processed: {file_path}\")\n",
|
|
" except Exception as e:\n",
|
|
" print(f\"Error processing {file_path}: {str(e)}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 21,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Loaded 404 actions from info.json\n",
|
|
"\n",
|
|
"Processing result/qwen/球机沙发正面_20241231_0200.json...\n",
|
|
"Found actions in 20241230_124643.avi: ['写作', '看', '写字', '阅读', '写', '坐', '起身']\n",
|
|
"Found actions in 20241230_124656.avi: ['看', '整理', '关闭']\n",
|
|
"Found actions in 20241230_124646.avi: ['躺', '阅读', '看']\n",
|
|
"Found actions in 20241230_124647.avi: ['坐', '躺', '休息', '看']\n",
|
|
"Found actions in 20241230_124645.avi: ['坐', '写', '站', '看']\n",
|
|
"Found actions in 20241230_124651.avi: ['关闭', '看', '睡觉', '睡', '休息', '躺']\n",
|
|
"Found actions in 20241230_124640.avi: ['坐', '休息', '阅读', '看']\n",
|
|
"Found actions in 20241230_124649.avi: ['整理', '看', '睡觉', '休息', '睡', '躺']\n",
|
|
"Found actions in 20241230_124650.avi: ['看', '睡觉', '睡', '休息', '躺']\n",
|
|
"Found actions in 20241230_124644.avi: ['坐', '工作', '操作', '看']\n",
|
|
"Found actions in 20241230_124653.avi: ['喝水', '看', '休息', '坐', '喝']\n",
|
|
"Found actions in 20241230_124654.avi: ['坐', '走', '站', '看']\n",
|
|
"Found actions in 20241230_124637.avi: ['坐', '休息', '使用手机', '看']\n",
|
|
"Found actions in 20241230_124638.avi: ['站', '看', '开始', '阅读', '玩手机', '坐']\n",
|
|
"Found actions in 20241230_124639.avi: ['看', '打开', '休息', '阅读', '坐']\n",
|
|
"Found actions in 20241230_124642.avi: ['写作', '看', '打开', '看书', '阅读', '学习', '工作', '写', '坐']\n",
|
|
"Found actions in 20241230_124636.avi: ['整理', '进入', '看', '关闭', '使用手机', '坐']\n",
|
|
"Found actions in 20241230_124648.avi: ['睡觉', '睡', '休息', '躺']\n",
|
|
"Found actions in 20241230_124652.avi: ['看', '睡觉', '睡', '休息', '躺']\n",
|
|
"Found actions in 20241230_124641.avi: ['写作', '吹', '打开', '看', '操作', '学习', '工作', '写', '使用笔记本电脑', '坐']\n",
|
|
"Updated result/qwen/球机沙发正面_20241231_0200.json\n",
|
|
"\n",
|
|
"Processing result/qwen/左侧吃饭2_20241231_1612.json...\n",
|
|
"Found actions in 20241230_130857.avi: ['工作', '咀嚼', '看']\n",
|
|
"Found actions in 20241230_130853.avi: ['喝饮料', '看', '工作', '坐', '喝']\n",
|
|
"Found actions in 20241230_130840.avi: ['吃东西', '看', '操作', '使用电脑', '工作', '动手', '坐', '吃']\n",
|
|
"Found actions in 20241230_130844.avi: ['吃东西', '看', '咬', '工作', '坐', '吃']\n",
|
|
"Found actions in 20241230_130848.avi: ['吃东西', '喝水', '看', '坐', '喝', '吃']\n",
|
|
"Found actions in 20241230_130835.avi: ['吃东西', '看', '戴眼镜', '操作', '使用电脑', '工作', '坐', '吃']\n",
|
|
"Updated result/qwen/左侧吃饭2_20241231_1612.json\n",
|
|
"\n",
|
|
"Processing result/qwen/左侧吃饭1_20241231_1423.json...\n",
|
|
"Found actions in 20241230_130812.avi: ['吃东西', '看', '戴眼镜', '工作', '使用手机', '吃']\n",
|
|
"Found actions in 20241230_130759.avi: ['站', '喝水', '站立', '走', '休息', '使用电脑', '工作', '坐', '喝', '起身']\n",
|
|
"Found actions in 20241230_130830.avi: ['吃东西', '看', '操作', '使用电脑', '工作', '吃']\n",
|
|
"Found actions in 20241230_130804.avi: ['坐下', '进入', '看', '打开', '开始', '操作', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_130826.avi: ['吃东西', '看', '休息', '工作', '坐', '咀嚼', '吃']\n",
|
|
"Found actions in 20241230_130835.avi: ['吃东西', '看', '戴眼镜', '使用电脑', '工作', '坐', '吃']\n",
|
|
"Found actions in 20241230_130808.avi: ['看', '打开', '走', '使用电脑', '工作']\n",
|
|
"Found actions in 20241230_130821.avi: ['看', '学习', '工作', '用餐', '坐', '咀嚼']\n",
|
|
"Found actions in 20241230_130817.avi: ['坐', '使用手机', '操作']\n",
|
|
"Updated result/qwen/左侧吃饭1_20241231_1423.json\n",
|
|
"\n",
|
|
"Processing result/qwen/室内右上角全景_20241231_2300.json...\n",
|
|
"Found actions in 20241230_124417.avi: ['进入', '整理', '阅读', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123344.avi: ['看']\n",
|
|
"Found actions in 20241230_123057.avi: ['行走', '坐下', '站', '站立', '走', '学习', '工作', '坐']\n",
|
|
"Found actions in 20241230_123606.avi: ['坐', '闻', '使用手机', '看']\n",
|
|
"Found actions in 20241230_123518.avi: ['行走', '站', '看', '打开', '站立', '走', '工作', '转', '转身']\n",
|
|
"Found actions in 20241230_124047.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123734.avi: ['坐', '闻', '使用手机']\n",
|
|
"Found actions in 20241230_123316.avi: ['看']\n",
|
|
"Found actions in 20241230_124519.avi: ['坐', '工作', '阅读', '看']\n",
|
|
"Found actions in 20241230_123219.avi: ['行走', '站', '交谈', '站立', '走', '交流']\n",
|
|
"Found actions in 20241230_124201.avi: ['坐', '工作', '使用电脑', '关闭']\n",
|
|
"Found actions in 20241230_124247.avi: ['站', '站立', '进入', '弯腰']\n",
|
|
"Found actions in 20241230_124106.avi: ['看', '关闭', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123803.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_124315.avi: ['整理', '弯腰']\n",
|
|
"Found actions in 20241230_123200.avi: ['写', '站', '整理', '看']\n",
|
|
"Found actions in 20241230_123402.avi: ['进入', '走', '关闭']\n",
|
|
"Found actions in 20241230_124528.avi: ['坐下', '使用电脑', '工作', '坐', '起身']\n",
|
|
"Found actions in 20241230_123528.avi: ['站', '看', '站立', '转', '转身']\n",
|
|
"Found actions in 20241230_123048.avi: ['坐下', '站', '喝水', '看', '走', '工作', '闻', '坐', '喝']\n",
|
|
"Found actions in 20241230_123039.avi: ['站', '喝水', '开始', '站立', '喝']\n",
|
|
"Found actions in 20241230_123537.avi: ['坐下', '站', '看', '站立', '走', '使用电脑', '工作', '坐', '起身']\n",
|
|
"Found actions in 20241230_124408.avi: ['行走', '坐下', '站', '站立', '走', '工作', '坐']\n",
|
|
"Found actions in 20241230_123325.avi: ['工作', '看']\n",
|
|
"Found actions in 20241230_123411.avi: ['站', '进入', '看', '开始', '走', '工作']\n",
|
|
"Found actions in 20241230_123901.avi: ['坐', '闻', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_124027.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123948.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_124426.avi: ['整理', '看', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123725.avi: ['坐', '使用手机', '工作']\n",
|
|
"Found actions in 20241230_124116.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_122946.avi: ['进入', '关闭']\n",
|
|
"Found actions in 20241230_123124.avi: ['行走', '进入', '看', '打开', '走', '开门']\n",
|
|
"Found actions in 20241230_124341.avi: ['坐', '阅读', '看']\n",
|
|
"Found actions in 20241230_124056.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_124210.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_124554.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123151.avi: ['走', '整理', '弯腰']\n",
|
|
"Found actions in 20241230_123557.avi: ['坐下', '站', '关闭', '开始', '站立', '使用电脑', '坐']\n",
|
|
"Found actions in 20241230_123929.avi: ['坐', '闻', '使用电脑']\n",
|
|
"Found actions in 20241230_123142.avi: ['站', '进入', '看', '打开', '使用电脑', '工作']\n",
|
|
"Found actions in 20241230_124008.avi: ['坐', '闻', '使用电脑']\n",
|
|
"Found actions in 20241230_123508.avi: ['行走', '站', '进入', '关闭', '走', '闻']\n",
|
|
"Found actions in 20241230_124125.avi: ['坐', '工作', '操作', '看']\n",
|
|
"Found actions in 20241230_123851.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_123635.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_123229.avi: ['行走', '整理', '看', '走', '写']\n",
|
|
"Found actions in 20241230_124435.avi: ['坐', '工作', '阅读', '看']\n",
|
|
"Found actions in 20241230_123133.avi: ['进入', '看', '打开', '关闭', '工作', '写']\n",
|
|
"Found actions in 20241230_124332.avi: ['坐', '看', '阅读', '关闭']\n",
|
|
"Found actions in 20241230_123420.avi: ['行走', '站', '整理', '看', '测量', '走', '站立', '工作']\n",
|
|
"Found actions in 20241230_123615.avi: ['坐', '闻', '使用电脑']\n",
|
|
"Found actions in 20241230_123353.avi: ['看']\n",
|
|
"Found actions in 20241230_124306.avi: ['行走', '蹲下', '走', '操作', '工作', '蹲']\n",
|
|
"Found actions in 20241230_123257.avi: ['打开', '关闭']\n",
|
|
"Found actions in 20241230_123115.avi: ['坐', '工作', '起身', '使用电脑']\n",
|
|
"Found actions in 20241230_123753.avi: ['坐', '闻', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_124037.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123106.avi: ['行走', '关闭', '打开', '看', '走', '写']\n",
|
|
"Found actions in 20241230_122909.avi: ['进入', '转', '转身']\n",
|
|
"Updated result/qwen/室内右上角全景_20241231_2300.json\n",
|
|
"\n",
|
|
"Processing result/qwen/室内右上角全景_20241231_2010.json...\n",
|
|
"Found actions in 20241230_123744.avi: ['坐', '使用手机', '看']\n",
|
|
"Found actions in 20241230_124143.avi: ['看', '关闭', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123004.avi: ['行走', '站', '整理', '看', '关闭', '走']\n",
|
|
"Found actions in 20241230_124501.avi: ['进入', '看', '操作', '工作', '坐']\n",
|
|
"Found actions in 20241230_124219.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123021.avi: ['行走', '走', '整理', '看']\n",
|
|
"Found actions in 20241230_124152.avi: ['进入', '使用电脑', '工作', '闻', '坐']\n",
|
|
"Found actions in 20241230_123655.avi: ['坐', '使用手机', '工作']\n",
|
|
"Found actions in 20241230_124452.avi: ['坐', '使用手机', '看']\n",
|
|
"Found actions in 20241230_122936.avi: ['行走', '进入', '关闭', '看', '走', '转', '转身']\n",
|
|
"Found actions in 20241230_123705.avi: ['坐', '使用手机', '工作']\n",
|
|
"Found actions in 20241230_123247.avi: ['工作', '走', '站', '看']\n",
|
|
"Found actions in 20241230_123449.avi: ['走', '整理', '起身', '弯腰']\n",
|
|
"Found actions in 20241230_122918.avi: ['搬运', '写', '看']\n",
|
|
"Found actions in 20241230_124227.avi: ['行走', '站', '站立', '走', '使用电脑', '坐', '起身']\n",
|
|
"Found actions in 20241230_124017.avi: ['站', '站立', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_122927.avi: ['看', '休息', '操作', '工作', '写', '弯腰', '起身']\n",
|
|
"Found actions in 20241230_123920.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123832.avi: ['坐', '休息', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_124546.avi: ['站', '看', '站立', '使用电脑', '工作', '坐', '起身']\n",
|
|
"Found actions in 20241230_124537.avi: ['看', '使用电脑', '工作', '闻', '坐']\n",
|
|
"Found actions in 20241230_123813.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_123440.avi: ['进入', '走', '转', '转身']\n",
|
|
"Found actions in 20241230_123210.avi: ['整理', '看', '打开', '关闭', '工作', '清理']\n",
|
|
"Found actions in 20241230_123306.avi: ['工作', '看']\n",
|
|
"Found actions in 20241230_124443.avi: ['坐', '弹奏', '看']\n",
|
|
"Found actions in 20241230_122859.avi: ['站', '看', '走', '操作', '工作', '交流']\n",
|
|
"Found actions in 20241230_123938.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_124359.avi: ['整理', '弯腰']\n",
|
|
"Found actions in 20241230_123334.avi: ['工作', '看']\n",
|
|
"Found actions in 20241230_124603.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_124510.avi: ['进入', '整理', '看', '阅读', '工作', '坐']\n",
|
|
"Found actions in 20241230_123822.avi: ['坐', '使用手机']\n",
|
|
"Found actions in 20241230_123012.avi: ['站', '看', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123715.avi: ['坐', '使用手机', '看']\n",
|
|
"Found actions in 20241230_122955.avi: ['工作', '开始', '整理', '使用电脑']\n",
|
|
"Found actions in 20241230_124134.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_123842.avi: ['坐', '看', '使用电脑']\n",
|
|
"Found actions in 20241230_124238.avi: ['讨论', '整理', '进入', '走', '工作', '交流']\n",
|
|
"Found actions in 20241230_123030.avi: ['进入', '看', '走', '闻', '交流']\n",
|
|
"Found actions in 20241230_123644.avi: ['坐', '使用手机', '关闭']\n",
|
|
"Found actions in 20241230_123911.avi: ['进入', '看', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_123547.avi: ['讨论', '站', '整理', '看', '交谈', '工作']\n",
|
|
"Found actions in 20241230_124350.avi: ['整理', '弯腰']\n",
|
|
"Found actions in 20241230_123625.avi: ['坐', '使用手机', '关闭']\n",
|
|
"Found actions in 20241230_123958.avi: ['坐', '闻', '使用电脑']\n",
|
|
"Found actions in 20241230_124256.avi: ['看', '走', '工作', '弯腰', '起身']\n",
|
|
"Found actions in 20241230_124323.avi: ['休息', '整理', '弯腰', '看']\n",
|
|
"Found actions in 20241230_123430.avi: ['行走', '坐下', '休息', '走', '工作', '坐']\n",
|
|
"Found actions in 20241230_123459.avi: ['行走', '站', '看', '走', '写']\n",
|
|
"Found actions in 20241230_123238.avi: ['行走', '站', '看', '走', '操作', '工作']\n",
|
|
"Updated result/qwen/室内右上角全景_20241231_2010.json\n",
|
|
"\n",
|
|
"Processing result/qwen/球机椅子左侧面_20241231_0923.json...\n",
|
|
"Found actions in 20241230_124611.avi: ['看', '休息', '戴眼镜', '工作', '使用手机', '写', '坐']\n",
|
|
"Found actions in 20241230_124626.avi: ['行走', '走', '工作', '写', '转', '转身']\n",
|
|
"Found actions in 20241230_124635.avi: ['站', '看', '站立', '走', '使用电脑', '工作']\n",
|
|
"Found actions in 20241230_124618.avi: ['看', '戴眼镜', '阅读', '学习', '工作']\n",
|
|
"Found actions in 20241230_124630.avi: ['站', '看', '笑', '工作', '转', '转身']\n",
|
|
"Found actions in 20241230_124627.avi: ['手写', '站', '站立', '写', '转', '转身']\n",
|
|
"Found actions in 20241230_124617.avi: ['看', '戴眼镜', '阅读', '学习', '工作', '闻', '坐']\n",
|
|
"Found actions in 20241230_124613.avi: ['工作', '使用手机', '戴眼镜', '看']\n",
|
|
"Found actions in 20241230_124623.avi: ['站', '喝水', '看', '站立', '戴眼镜', '休息', '工作', '喝', '转', '转身']\n",
|
|
"Found actions in 20241230_124612.avi: ['看', '操作', '学习', '工作', '使用手机', '坐']\n",
|
|
"Found actions in 20241230_124620.avi: ['看', '转头', '使用电脑', '学习', '工作', '转向', '坐', '转']\n",
|
|
"Found actions in 20241230_124610.avi: ['坐', '使用手机', '工作', '看']\n",
|
|
"Found actions in 20241230_124629.avi: ['行走', '进入', '看', '走', '工作', '交流']\n",
|
|
"Found actions in 20241230_124631.avi: ['行走', '看', '走', '工作', '转', '转身']\n",
|
|
"Found actions in 20241230_124632.avi: ['站', '看', '站立', '操作', '转', '转身']\n",
|
|
"Found actions in 20241230_124622.avi: ['喝水', '看', '休息', '使用电脑', '工作', '坐', '喝']\n",
|
|
"Found actions in 20241230_124614.avi: ['看', '开始', '阅读', '学习', '工作', '使用手机', '坐']\n",
|
|
"Found actions in 20241230_124616.avi: ['看', '转头', '阅读', '学习', '工作', '转向', '坐', '转', '转身']\n",
|
|
"Found actions in 20241230_124634.avi: ['坐', '工作', '写', '看']\n",
|
|
"Found actions in 20241230_124621.avi: ['喝水', '看', '休息', '操作', '使用电脑', '工作', '动手', '坐', '喝']\n",
|
|
"Found actions in 20241230_124628.avi: ['行走', '站', '看', '走', '操作', '工作', '转向', '转', '转身']\n",
|
|
"Found actions in 20241230_124633.avi: ['站', '看', '站立', '学习', '工作', '弯腰']\n",
|
|
"Found actions in 20241230_124624.avi: ['站', '看', '走', '坐', '起身']\n",
|
|
"Found actions in 20241230_124615.avi: ['坐', '阅读', '看']\n",
|
|
"Found actions in 20241230_124619.avi: ['站', '看', '站立', '使用电脑', '转', '起身', '转身']\n",
|
|
"Updated result/qwen/球机椅子左侧面_20241231_0923.json\n",
|
|
"\n",
|
|
"Processing result/qwen/右上角吃饭_20241231_1232.json...\n",
|
|
"Found actions in 20241230_130634.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130731.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130653.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130658.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130620.avi: ['坐', '工作']\n",
|
|
"Found actions in 20241230_130712.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130745.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130726.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130551.avi: ['行走', '进入', '休息', '走', '操作', '工作', '服务']\n",
|
|
"Found actions in 20241230_130741.avi: ['坐', '组装', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130639.avi: ['休息', '使用电脑', '工作', '饮用', '坐']\n",
|
|
"Found actions in 20241230_130644.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130707.avi: ['坐', '闻', '服务', '使用电脑']\n",
|
|
"Found actions in 20241230_130555.avi: ['看', '交谈', '休息', '走', '使用电脑', '工作', '交流', '坐']\n",
|
|
"Found actions in 20241230_130629.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130624.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130605.avi: ['看', '操作', '使用电脑', '工作', '坐']\n",
|
|
"Found actions in 20241230_130610.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130703.avi: ['坐', '工作', '服务', '使用电脑']\n",
|
|
"Found actions in 20241230_130736.avi: ['吃东西', '使用电脑', '吃零食', '工作', '坐', '吃']\n",
|
|
"Found actions in 20241230_130648.avi: ['喝水', '看', '休息', '使用电脑', '工作', '坐', '喝']\n",
|
|
"Found actions in 20241230_130755.avi: ['坐', '组装', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130750.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130546.avi: ['坐', '工作', '起身', '看']\n",
|
|
"Found actions in 20241230_130717.avi: ['坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130615.avi: ['使用电脑', '学习', '工作', '闻', '坐']\n",
|
|
"Found actions in 20241230_130722.avi: ['看', '坐', '工作', '使用电脑']\n",
|
|
"Found actions in 20241230_130600.avi: ['坐下', '站', '看', '站立', '工作', '坐']\n",
|
|
"Updated result/qwen/右上角吃饭_20241231_1232.json\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"# 读取动作列表\n",
|
|
"def load_actions():\n",
|
|
" with open('info.json', 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
" return set(data['actions']) # 使用集合提高查找效率\n",
|
|
"\n",
|
|
"def find_actions(text, action_set):\n",
|
|
" \"\"\"在文本中查找动作\"\"\"\n",
|
|
" found_actions = set()\n",
|
|
" for action in action_set:\n",
|
|
" # 直接使用简单的字符串匹配\n",
|
|
" if action in text:\n",
|
|
" found_actions.add(action)\n",
|
|
" return list(found_actions)\n",
|
|
"\n",
|
|
"def process_file(file_path, action_set):\n",
|
|
" \"\"\"处理单个文件\"\"\"\n",
|
|
" modified = False\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
" \n",
|
|
" # 遍历所有视频分析\n",
|
|
" for video_key, video_data in data.items():\n",
|
|
" if 'video_analysis' in video_data:\n",
|
|
" analysis = video_data['video_analysis']\n",
|
|
" if 'qwen-7B' in analysis:\n",
|
|
" qwen_data = analysis['qwen-7B']\n",
|
|
" if 'original_answer' in qwen_data and 'extracted_info' in qwen_data:\n",
|
|
" # 查找动作\n",
|
|
" found_actions = find_actions(qwen_data['original_answer'], action_set)\n",
|
|
" if found_actions: # 只有找到动作时才更新\n",
|
|
" qwen_data['extracted_info']['actions'] = found_actions\n",
|
|
" modified = True\n",
|
|
" print(f\"Found actions in {video_key}: {found_actions}\")\n",
|
|
" \n",
|
|
" # 只有在发现动作时才写回文件\n",
|
|
" if modified:\n",
|
|
" with open(file_path, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(data, f, ensure_ascii=False, indent=2)\n",
|
|
" print(f\"Updated {file_path}\")\n",
|
|
" else:\n",
|
|
" print(f\"No actions found in {file_path}\")\n",
|
|
"\n",
|
|
"# 主程序\n",
|
|
"def main():\n",
|
|
" # 需要处理的文件列表\n",
|
|
" files = [\n",
|
|
" \"result/qwen/球机沙发正面_20241231_0200.json\",\n",
|
|
" \"result/qwen/左侧吃饭2_20241231_1612.json\",\n",
|
|
" \"result/qwen/左侧吃饭1_20241231_1423.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2300.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2010.json\",\n",
|
|
" \"result/qwen/球机椅子左侧面_20241231_0923.json\",\n",
|
|
" \"result/qwen/右上角吃饭_20241231_1232.json\"\n",
|
|
" ]\n",
|
|
" \n",
|
|
" # 加载动作列表\n",
|
|
" action_set = load_actions()\n",
|
|
" print(f\"Loaded {len(action_set)} actions from info.json\")\n",
|
|
" \n",
|
|
" # 处理每个文件\n",
|
|
" for file_path in files:\n",
|
|
" try:\n",
|
|
" print(f\"\\nProcessing {file_path}...\")\n",
|
|
" process_file(file_path, action_set)\n",
|
|
" except Exception as e:\n",
|
|
" print(f\"Error processing {file_path}: {str(e)}\")\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"import os\n",
|
|
"from datetime import datetime\n",
|
|
"\n",
|
|
"def calculate_accuracy(predicted_file, ground_truth_file):\n",
|
|
" \"\"\"计算模型预测结果与人工标注的准确度\"\"\"\n",
|
|
" accuracy_metrics = {\n",
|
|
" \"environment\": 0,\n",
|
|
" \"num_people\": 0,\n",
|
|
" \"actions\": 0,\n",
|
|
" \"objects\": 0,\n",
|
|
" \"furniture\": 0,\n",
|
|
" \"emotions\": 0,\n",
|
|
" \"features\": 0\n",
|
|
" }\n",
|
|
" total_files = 0\n",
|
|
" \n",
|
|
" # 加载文件\n",
|
|
" try:\n",
|
|
" with open(predicted_file, 'r', encoding='utf-8') as f:\n",
|
|
" predicted_data = json.load(f)\n",
|
|
" with open(ground_truth_file, 'r', encoding='utf-8') as f:\n",
|
|
" ground_truth_data = json.load(f)\n",
|
|
" except Exception as e:\n",
|
|
" print(f\"读取文件时出错: {str(e)}\")\n",
|
|
" return accuracy_metrics\n",
|
|
" \n",
|
|
" print(f\"\\n开始计算准确率...\")\n",
|
|
" print(f\"预测结果包含 {len(predicted_data)} 个文件\")\n",
|
|
" print(f\"标注数据包含 {len(ground_truth_data)} 个文件\")\n",
|
|
" \n",
|
|
" # 对每个预测结果进行评估\n",
|
|
" for video_name, pred in predicted_data.items():\n",
|
|
" print(f\"\\n处理文件: {video_name}\")\n",
|
|
" \n",
|
|
" # 检查文件是否在标注数据中\n",
|
|
" if video_name not in ground_truth_data:\n",
|
|
" print(f\"标注数据中未找到: {video_name}\")\n",
|
|
" continue\n",
|
|
" \n",
|
|
" try:\n",
|
|
" # 获取预测信息\n",
|
|
" pred_info = pred[\"video_analysis\"][\"qwen\"][\"extracted_info\"]\n",
|
|
" gt_info = ground_truth_data[video_name][\"extracted_info\"]\n",
|
|
" \n",
|
|
" total_files += 1\n",
|
|
" print(f\"成功匹配文件: {video_name}\")\n",
|
|
" \n",
|
|
" # 环境匹配\n",
|
|
" if pred_info[\"environment\"] == gt_info[\"environment\"]:\n",
|
|
" accuracy_metrics[\"environment\"] += 1\n",
|
|
" print(f\"环境匹配成功: {pred_info['environment']}\")\n",
|
|
" \n",
|
|
" # 人数匹配\n",
|
|
" if pred_info[\"num_people\"] == gt_info[\"num_people\"]:\n",
|
|
" accuracy_metrics[\"num_people\"] += 1\n",
|
|
" print(f\"人数匹配成功: {pred_info['num_people']}\")\n",
|
|
" \n",
|
|
" # 计算列表类字段的匹配度(使用Jaccard相似度)\n",
|
|
" field_mapping = {\n",
|
|
" \"actions\": \"actions\",\n",
|
|
" \"objects\": \"objects\",\n",
|
|
" \"furniture\": \"furniture\",\n",
|
|
" \"emotions\": \"emotions\",\n",
|
|
" \"features\": \"feature \" # 注意标注数据中的 \"feature \" 有空格\n",
|
|
" }\n",
|
|
" \n",
|
|
" for pred_field, gt_field in field_mapping.items():\n",
|
|
" pred_set = set(pred_info[pred_field]) if pred_info[pred_field] else set()\n",
|
|
" gt_set = set(gt_info[gt_field]) if gt_field in gt_info and gt_info[gt_field] else set()\n",
|
|
" \n",
|
|
" if pred_set or gt_set: # 避免除零错误\n",
|
|
" intersection = len(pred_set & gt_set)\n",
|
|
" union = len(pred_set | gt_set)\n",
|
|
" jaccard = intersection / union\n",
|
|
" accuracy_metrics[pred_field] += jaccard\n",
|
|
" print(f\"{pred_field} 匹配度: {jaccard:.2%}\")\n",
|
|
" print(f\"预测集合: {pred_set}\")\n",
|
|
" print(f\"标注集合: {gt_set}\")\n",
|
|
" print(f\"交集数量: {intersection}\")\n",
|
|
" print(f\"并集数量: {union}\")\n",
|
|
" \n",
|
|
" except Exception as e:\n",
|
|
" print(f\"处理 {video_name} 时出错: {str(e)}\")\n",
|
|
" continue\n",
|
|
" \n",
|
|
" # 计算平均准确率\n",
|
|
" if total_files > 0:\n",
|
|
" print(f\"\\n共成功比较 {total_files} 个文件\")\n",
|
|
" for key in accuracy_metrics:\n",
|
|
" accuracy_metrics[key] = round(accuracy_metrics[key] / total_files * 100, 2)\n",
|
|
" else:\n",
|
|
" print(\"\\n警告:没有成功比较任何文件\")\n",
|
|
" \n",
|
|
" return accuracy_metrics\n",
|
|
"\n",
|
|
"def main():\n",
|
|
" try:\n",
|
|
" # 获取输入文件路径\n",
|
|
" predicted_file = input(\"请输入预测结果文件路径: \").strip()\n",
|
|
" ground_truth_file = input(\"请输入标注文件路径: \").strip()\n",
|
|
" output_path = input(\"请输入结果保存路径 (直接回车使用当前目录): \").strip()\n",
|
|
" \n",
|
|
" # 验证文件是否存在\n",
|
|
" if not os.path.exists(predicted_file):\n",
|
|
" raise Exception(f\"错误:预测结果文件 '{predicted_file}' 不存在\")\n",
|
|
" if not os.path.exists(ground_truth_file):\n",
|
|
" raise Exception(f\"错误:标注文件 '{ground_truth_file}' 不存在\")\n",
|
|
" \n",
|
|
" # 设置输出路径\n",
|
|
" output_path = output_path if output_path else os.getcwd()\n",
|
|
" if not os.path.exists(output_path):\n",
|
|
" os.makedirs(output_path)\n",
|
|
" \n",
|
|
" # 计算准确率\n",
|
|
" accuracy = calculate_accuracy(predicted_file, ground_truth_file)\n",
|
|
" \n",
|
|
" # 显示准确率结果\n",
|
|
" print(\"\\n准确率评估结果:\")\n",
|
|
" for metric, value in accuracy.items():\n",
|
|
" print(f\"{metric}: {value}%\")\n",
|
|
" \n",
|
|
" # 保存准确率结果\n",
|
|
" timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
|
|
" accuracy_file = os.path.join(output_path, f\"accuracy_results_{timestamp}.json\")\n",
|
|
" with open(accuracy_file, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(accuracy, f, ensure_ascii=False, indent=2)\n",
|
|
" print(f\"\\n准确率评估结果已保存到: {accuracy_file}\")\n",
|
|
" \n",
|
|
" except Exception as e:\n",
|
|
" print(f\"\\n错误: {str(e)}\")\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" main()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"\n",
|
|
"def merge_json_files(json_files):\n",
|
|
" # 创建结果字典\n",
|
|
" merged_result = {}\n",
|
|
" \n",
|
|
" # 遍历每个JSON文件\n",
|
|
" for file_data in json_files:\n",
|
|
" # 遍历每个视频\n",
|
|
" for video_name, video_data in file_data.items():\n",
|
|
" if video_name not in merged_result:\n",
|
|
" merged_result[video_name] = {}\n",
|
|
" \n",
|
|
" # 获取模型名称和original_answer\n",
|
|
" if \"video_analysis\" in video_data:\n",
|
|
" for model_name, model_data in video_data[\"video_analysis\"].items():\n",
|
|
" if \"original_answer\" in model_data:\n",
|
|
" merged_result[video_name][model_name] = model_data[\"original_answer\"]\n",
|
|
"\n",
|
|
" return merged_result\n",
|
|
"\n",
|
|
"# 读取所有JSON文件\n",
|
|
"json_files = [\n",
|
|
"\n",
|
|
" \"/home/zydi/VLM/result/analysis_results_室内右上角全景筛选_20250102_084327.json\",\n",
|
|
" \"/home/zydi/VLM/result/analysis_results_室内右上角全景筛选_20250102_084600.json\"\n",
|
|
"]\n",
|
|
"\n",
|
|
"# 读取所有文件内容\n",
|
|
"json_contents = []\n",
|
|
"for file_path in json_files:\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
|
" json_contents.append(json.load(f))\n",
|
|
"\n",
|
|
"# 合并JSON\n",
|
|
"result = merge_json_files(json_contents)\n",
|
|
"\n",
|
|
"# 将结果写入新文件\n",
|
|
"with open('qwen_prompt.json', 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(result, f, ensure_ascii=False, indent=2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def clean_places365_names():\n",
|
|
" with open('Places365.txt', 'r') as file:\n",
|
|
" lines = file.readlines()\n",
|
|
" \n",
|
|
" cleaned_lines = []\n",
|
|
" for line in lines:\n",
|
|
" # 移除开头的/x/部分\n",
|
|
" if line.strip(): # 确保不是空行\n",
|
|
" # 找到第二个/后的位置\n",
|
|
" start_pos = line.find('/', 1) + 1\n",
|
|
" # 获取中间部分(去掉末尾的数字)\n",
|
|
" name = line[start_pos:].rsplit(' ', 1)[0]\n",
|
|
" cleaned_lines.append(name)\n",
|
|
" \n",
|
|
" # 写入原文件\n",
|
|
" with open('Places365.txt', 'w') as file:\n",
|
|
" file.write('\\n'.join(cleaned_lines))\n",
|
|
"\n",
|
|
"# 执行函数\n",
|
|
"clean_places365_names()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def merge_json_files(json_files):\n",
|
|
" # 创建结果字典\n",
|
|
" merged_result = {}\n",
|
|
" \n",
|
|
" # 遍历每个JSON文件\n",
|
|
" for file_path, file_data in zip(json_files, json_contents):\n",
|
|
" # 从文件名中提取时间戳\n",
|
|
" file_timestamp = None\n",
|
|
" if \"_20250102_\" in file_path:\n",
|
|
" file_timestamp = file_path.split(\"_\")[-1].replace(\".json\", \"\")\n",
|
|
" \n",
|
|
" # 遍历每个视频\n",
|
|
" for video_name, video_data in file_data.items():\n",
|
|
" if video_name not in merged_result:\n",
|
|
" merged_result[video_name] = {\n",
|
|
" \"video_analysis\": {\n",
|
|
" \"qwen-7B\": {\n",
|
|
" \"original_answers\": {}\n",
|
|
" }\n",
|
|
" }\n",
|
|
" }\n",
|
|
" \n",
|
|
" # 获取original_answer并添加到对应时间戳下\n",
|
|
" if \"video_analysis\" in video_data and \"qwen-7B\" in video_data[\"video_analysis\"]:\n",
|
|
" model_data = video_data[\"video_analysis\"][\"qwen-7B\"]\n",
|
|
" if \"original_answer\" in model_data:\n",
|
|
" merged_result[video_name][\"video_analysis\"][\"qwen-7B\"][\"original_answers\"][file_timestamp] = model_data[\"original_answer\"]\n",
|
|
" \n",
|
|
" return merged_result\n",
|
|
"\n",
|
|
"# 读取所有JSON文件\n",
|
|
"json_files = [\n",
|
|
" \"/home/zydi/VLM/result/室内右上角全景筛选_20250102_065735.json\",\n",
|
|
" \"/home/zydi/VLM/result/室内右上角全景筛选_20250102_072352.json\",\n",
|
|
" \"/home/zydi/VLM/result/室内右上角全景筛选_20250102_072724.json\",\n",
|
|
" \"/home/zydi/VLM/result/室内右上角全景筛选_20250102_075545.json\"\n",
|
|
"]\n",
|
|
"\n",
|
|
"# 读取所有文件内容\n",
|
|
"json_contents = []\n",
|
|
"for file_path in json_files:\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
|
" json_contents.append(json.load(f))\n",
|
|
"\n",
|
|
"# 合并JSON\n",
|
|
"result = merge_json_files(json_files)\n",
|
|
"\n",
|
|
"# 将结果写入新文件\n",
|
|
"with open('qwen_prompt.json', 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(result, f, ensure_ascii=False, indent=2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"处理完成!输出文件:formatted_qwen_prompt.json\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"import re\n",
|
|
"\n",
|
|
"def format_answer(text):\n",
|
|
" # 处理标题\n",
|
|
" text = re.sub(r'###\\s+', '\\n### ', text)\n",
|
|
" text = re.sub(r'####\\s+', '\\n#### ', text)\n",
|
|
" \n",
|
|
" # 处理列表项\n",
|
|
" text = re.sub(r'(?m)^-\\s+', '\\n- ', text)\n",
|
|
" text = re.sub(r'(?m)^•\\s+', '\\n• ', text)\n",
|
|
" \n",
|
|
" # 处理加粗文本\n",
|
|
" text = re.sub(r'\\*\\*([^*]+)\\*\\*:', '\\n**\\\\1**:', text)\n",
|
|
" \n",
|
|
" # 移除多余的空行\n",
|
|
" text = re.sub(r'\\n{3,}', '\\n\\n', text)\n",
|
|
" \n",
|
|
" return text.strip()\n",
|
|
"\n",
|
|
"def process_json_file(input_file, output_file):\n",
|
|
" try:\n",
|
|
" with open(input_file, 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
" \n",
|
|
" # 递归处理所有的original_answers\n",
|
|
" def process_dict(d):\n",
|
|
" for k, v in d.items():\n",
|
|
" if k == 'original_answers' or k == 'original_answer':\n",
|
|
" if isinstance(v, dict):\n",
|
|
" for sub_k, sub_v in v.items():\n",
|
|
" v[sub_k] = format_answer(sub_v)\n",
|
|
" else:\n",
|
|
" d[k] = format_answer(v)\n",
|
|
" elif isinstance(v, dict):\n",
|
|
" process_dict(v)\n",
|
|
" elif isinstance(v, list):\n",
|
|
" for item in v:\n",
|
|
" if isinstance(item, dict):\n",
|
|
" process_dict(item)\n",
|
|
" \n",
|
|
" process_dict(data)\n",
|
|
" \n",
|
|
" with open(output_file, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(data, f, ensure_ascii=False, indent=2)\n",
|
|
" \n",
|
|
" print(f\"处理完成!输出文件:{output_file}\")\n",
|
|
" \n",
|
|
" except Exception as e:\n",
|
|
" print(f\"处理文件时出错:{str(e)}\")\n",
|
|
"\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" # 示例使用\n",
|
|
" input_file = \"qwen_prompt.json\" # 输入文件路径\n",
|
|
" output_file = \"formatted_qwen_prompt.json\" # 输出文件路径\n",
|
|
" process_json_file(input_file, output_file)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import json\n",
|
|
"from datetime import datetime, timedelta\n",
|
|
"\n",
|
|
"def update_timestamps(json_data):\n",
|
|
" # 设置基准时间为 2024.12.31 02:00:00\n",
|
|
" base_time = datetime(2024, 12, 31, 16, 12, 0)\n",
|
|
" \n",
|
|
" # 遍历所有视频条目\n",
|
|
" for i, (video_key, video_data) in enumerate(json_data.items()):\n",
|
|
" # 计算新的时间戳 (每条记录间隔1分钟)\n",
|
|
" new_time = base_time + timedelta(minutes=i)\n",
|
|
" \n",
|
|
" # 更新时间戳\n",
|
|
" video_data['timestamp'] = new_time.strftime('%Y-%m-%d %H:%M:%S')\n",
|
|
" \n",
|
|
" return json_data\n",
|
|
"\n",
|
|
"# 示例使用\n",
|
|
"with open('/home/zydi/VLM/result/qwen/左侧吃饭2_20241231_1612.json', 'r', encoding='utf-8') as f:\n",
|
|
" data = json.load(f)\n",
|
|
"\n",
|
|
"# 更新时间戳\n",
|
|
"updated_data = update_timestamps(data)\n",
|
|
"\n",
|
|
"# 保存更新后的文件\n",
|
|
"with open('/home/zydi/VLM/result/qwen/左侧吃饭2_20241231_1612.json', 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(updated_data, f, ensure_ascii=False, indent=2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 24,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"成功保存: camera001_20241231_1612\n",
|
|
"成功保存: camera001_20241231_1423\n",
|
|
"成功保存: camera001_20241231_1232\n",
|
|
"成功保存: camera001_20241231_0200\n",
|
|
"成功保存: camera001_20241231_2300\n",
|
|
"成功保存: camera001_20241231_2010\n",
|
|
"成功保存: camera001_20241231_0923\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"import redis\n",
|
|
"import os\n",
|
|
"import re\n",
|
|
"\n",
|
|
"def save_json_to_redis(file_path):\n",
|
|
" try:\n",
|
|
" # 连接Redis\n",
|
|
" r = redis.Redis(\n",
|
|
" host=\"222.186.10.253\",\n",
|
|
" port=6379,\n",
|
|
" password=\"Obscura@2024\",\n",
|
|
" db=207\n",
|
|
" )\n",
|
|
" \n",
|
|
" # 读取JSON文件\n",
|
|
" with open(file_path, 'r', encoding='utf-8') as f:\n",
|
|
" content = json.load(f)\n",
|
|
" \n",
|
|
" # 从文件路径中提取文件名\n",
|
|
" file_name = os.path.basename(file_path)\n",
|
|
" \n",
|
|
" # 从文件名中提取时间戳\n",
|
|
" timestamp_match = re.search(r'_(\\d{8}_\\d{4})', file_name)\n",
|
|
" if timestamp_match:\n",
|
|
" timestamp = timestamp_match.group(1)\n",
|
|
" # 构建新的key\n",
|
|
" new_key = f\"camera001_{timestamp}\"\n",
|
|
" \n",
|
|
" # 将内容转换为JSON字符串\n",
|
|
" json_str = json.dumps(content, ensure_ascii=False)\n",
|
|
" \n",
|
|
" # 保存到Redis\n",
|
|
" r.set(new_key, json_str)\n",
|
|
" print(f\"成功保存: {new_key}\")\n",
|
|
" else:\n",
|
|
" print(f\"无法从文件名提取时间戳: {file_name}\")\n",
|
|
" \n",
|
|
" # 关闭Redis连接\n",
|
|
" r.close()\n",
|
|
" \n",
|
|
" except Exception as e:\n",
|
|
" print(f\"处理文件 {file_path} 时出错: {str(e)}\")\n",
|
|
"\n",
|
|
"# 要处理的文件列表\n",
|
|
"files_to_process = [\n",
|
|
" \"result/qwen/左侧吃饭2_20241231_1612.json\",\n",
|
|
" \"result/qwen/左侧吃饭1_20241231_1423.json\",\n",
|
|
" \"result/qwen/右上角吃饭_20241231_1232.json\",\n",
|
|
" \"result/qwen/球机沙发正面_20241231_0200.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2300.json\",\n",
|
|
" \"result/qwen/室内右上角全景_20241231_2010.json\",\n",
|
|
" \"result/qwen/球机椅子左侧面_20241231_0923.json\"\n",
|
|
"]\n",
|
|
"\n",
|
|
"# 处理每个文件\n",
|
|
"for file_path in files_to_process:\n",
|
|
" save_json_to_redis(file_path)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"成功获取key: A01_20250107_1600\n",
|
|
"\n",
|
|
"数据已保存到: .//redis_data_20250107_085228.json\n",
|
|
"共处理 1 个key\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import json\n",
|
|
"import redis\n",
|
|
"from datetime import datetime\n",
|
|
"\n",
|
|
"def fetch_from_redis_and_save(keys=None, output_dir=\"./\"):\n",
|
|
" \"\"\"\n",
|
|
" 从Redis获取指定keys的数据并保存为JSON文件\n",
|
|
" \n",
|
|
" 参数:\n",
|
|
" - keys: 要获取的key列表,如果为None则获取所有keys\n",
|
|
" - output_dir: 输出文件夹路径\n",
|
|
" \"\"\"\n",
|
|
" try:\n",
|
|
" # 连接Redis\n",
|
|
" r = redis.Redis(\n",
|
|
" host=\"222.186.10.253\",\n",
|
|
" port=6379,\n",
|
|
" password=\"Obscura@2024\",\n",
|
|
" db=210\n",
|
|
" )\n",
|
|
" \n",
|
|
" # 如果没有指定keys,获取所有keys\n",
|
|
" if keys is None:\n",
|
|
" keys = r.keys(\"camera001_*\") # 获取所有以camera001_开头的key\n",
|
|
" keys = [key.decode('utf-8') for key in keys] # 将bytes转换为字符串\n",
|
|
" \n",
|
|
" # 创建一个字典存储所有数据\n",
|
|
" all_data = {}\n",
|
|
" \n",
|
|
" # 获取每个key的数据\n",
|
|
" for key in keys:\n",
|
|
" try:\n",
|
|
" value = r.get(key)\n",
|
|
" if value:\n",
|
|
" # 将Redis中的JSON字符串转换为Python对象\n",
|
|
" data = json.loads(value)\n",
|
|
" all_data[key] = data\n",
|
|
" print(f\"成功获取key: {key}\")\n",
|
|
" except Exception as e:\n",
|
|
" print(f\"处理key {key} 时出错: {str(e)}\")\n",
|
|
" \n",
|
|
" # 生成输出文件名(使用时间戳)\n",
|
|
" timestamp = datetime.now().strftime(\"%Y%m%d_%H%M%S\")\n",
|
|
" output_file = f\"{output_dir}/redis_data_{timestamp}.json\"\n",
|
|
" \n",
|
|
" # 保存为JSON文件\n",
|
|
" with open(output_file, 'w', encoding='utf-8') as f:\n",
|
|
" json.dump(all_data, f, ensure_ascii=False, indent=2)\n",
|
|
" \n",
|
|
" print(f\"\\n数据已保存到: {output_file}\")\n",
|
|
" print(f\"共处理 {len(all_data)} 个key\")\n",
|
|
" \n",
|
|
" # 关闭Redis连接\n",
|
|
" r.close()\n",
|
|
" \n",
|
|
" except Exception as e:\n",
|
|
" print(f\"发生错误: {str(e)}\")\n",
|
|
"\n",
|
|
"# 使用示例\n",
|
|
"if __name__ == \"__main__\":\n",
|
|
" # 可以指定特定的keys\n",
|
|
" specific_keys = [\n",
|
|
" \"A01_20250107_1600\"\n",
|
|
" # \"camera001_20241231_1423\",\n",
|
|
" # \"camera001_20241231_1232\"\n",
|
|
" ]\n",
|
|
" \n",
|
|
" # 获取指定的keys\n",
|
|
" fetch_from_redis_and_save(keys=specific_keys)\n",
|
|
" \n",
|
|
" # 或者获取所有keys\n",
|
|
" # fetch_from_redis_and_save()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "kafka",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.11.9"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|