import json
import os.path as osp
import logging
#业务需求:result.txt为两列信息n_sample中的request_images和p_sample中的register_images
#删除request_images对应的n_sample中的sample
#增加register_images对应的与register_images对应的n_sample中的request_images
#很多request_images对应一个sample
#感谢天宇哥全程指导# 构建一个request_image和sample的映射关系
def analysis_n_sample(n_path):jsonlist = json.load(open(n_path, 'r'))  # 读取json文件jsonlist[0]为字典request_img_to_sample = {}for sample in jsonlist['images']:request_images_list = sample['request_images']for request_image_path in request_images_list:request_image = osp.basename(request_image_path)#print(request_image)request_img_to_sample[request_image] = samplereturn request_img_to_sample# 解析p_samples.json,找到每一个register_images[0]和sample的映射关系
def analysis_p_sample(p_path):jsondata = json.load(open(p_path, 'r'))  # 读取json文件jsonlist为列表jsonlist = jsondata['images']register_images_to_sample = {}  # 构建register_images_to_sample字典for sample in jsonlist:# 先遍历jsonlist列表register_imageslist = sample['register_images']# register_imageslist[0]表明register_images第一张图片register_imagedata = register_imageslist[0]#得到图片名字而不是,路径名register_image = osp.basename(register_imagedata)#print(register_image)register_images_to_sample[register_image] = samplereturn  register_images_to_sampledef register2index(p_path):"""从p_json中获得register和index的关系,方便后续用index直接修改p_data"""jsondata = json.load(open(p_path, 'r'))  # 读取json文件jsonlist为列表jsonlist = jsondata['images']p_register_to_index = {}for index, sample in enumerate(jsonlist):registerimg = osp.basename(sample['register_images'][0])p_register_to_index[registerimg] = indexreturn p_register_to_indexdef readydelete(result_path):deletelist = []with open(result_path, 'r') as f:contents = f.readlines()  # 读取全部行for img in contents:delete = img.split(',')[0]request_img = delete.lstrip('00-')deletelist.append(request_img)return deletelistdef remove_request(request_img_to_sample,deletelist):"""将request 的 sample 从 n_samples.json 中移除"""n_jsondict = json.load(open(n_path, 'r')) n_jsonlist = n_jsondict['images']  remove_counter = 0for key in deletelist:sample = request_img_to_sample[key]try:   n_jsonlist.remove(sample)remove_counter += 1except ValueError as error:print(f"{key} has been removed")new_n_sample = {"images" : n_jsonlist}print(f'There are {remove_counter} samples has been removed')with open('new_n_samples.json', 'w') as f:json.dump(new_n_sample, f, indent=4)# - 解析annotation_resultdef analysis_result(result_path,request_img_to_sample,p_register_to_index,p_path,n_path):p_jsondata = json.load(open(p_path, 'r'))  # 读取json文件jsonlist为列表p_jsonlist = p_jsondata['images']with open(result_path, 'r') as txt:data_list = txt.readlines()  # 读取全部行n_samples_idslist = []  #n_samples_idslist = []定义之前遍历n_sample的ids列表new_p_sample = []for line in data_list:linedata = line.strip()request_img_with00 = linedata.split(',')[0]request_img = request_img_with00.lstrip('00-')# 返回截掉字符串左边的空格或指定字符后生成的新字符串register_img = linedata.split(',')[1]#print(register_img)p_index = p_register_to_index[register_img]#得到p_jsonlist要处理的元素编号n_sample = request_img_to_sample[request_img]if n_sample['ids'] not in n_samples_idslist:n_samples_idslist.append(n_sample['ids']) p_jsonlist[p_index]['request_images'] += n_sample['request_images']new_p_sample = {'images':p_jsonlist}with open('new_p_samples.json', 'w') as f:json.dump(new_p_sample, f, indent=4)# - 从n_sample.json中删除 挪过的sampledef check_init():from tykit import NPsamplesp = NPsamples('./p_samples.json', 'p')n = NPsamples('./n_samples.json', 'n')p.show_info()n.show_info()def check_result():from tykit import NPsamplesp = NPsamples('./new_p_samples.json', 'p')n = NPsamples('./new_n_samples.json', 'n')p.show_info()n.show_info()if __name__ == '__main__':# initoutfile_path = './'n_path = './n_samples.json'p_path = './p_samples.json'result_path = './annotation_result.txt'outfile_path = './'# check init statuscheck_init()# parse np_samples.jsonrequest_img_to_sample = analysis_n_sample(n_path)register_images_to_sample = analysis_p_sample(p_path)deletelist = readydelete(result_path)remove_request(request_img_to_sample,deletelist)p_register_to_index = register2index(p_path)analysis_result(result_path,request_img_to_sample,p_register_to_index,p_path,n_path)check_result()

删除request_images对应的n_sample中的sample 增加register_images对应的与register_images对应的n_sample中的request_images相关推荐

  1. HarmonyOS之在工程中导入Sample工程和添加Module

    一.导入 Sample 工程 ① Sample 说明 DevEco Studio 支持 HarmonyOS Sample 工程的导入功能,通过对接 Gitee 开源社区中的 Sample 资源,可一键 ...

  2. python中sample是什么意思_基于Python中random.sample()的替代方案

    python中random.sample()方法可以随机地从指定列表中提取出N个不同的元素,但在实践中发现,当N的值比较大的时候,该方法执行速度很慢,如: numpy random模块中的choice ...

  3. C语言试题六十三之请编写函数fun:将s所指字符串中ascii值为偶数的字符删除,串中剩余字符形成一个新串放在t所指的数组中。

    1. 题目 请编写函数fun,其功能是:将s所指字符串中ascii值为偶数的字符删除,串中剩余字符形成一个新串放在t所指的数组中. 2 .温馨提示 C语言试题汇总里可用于计算机二级C语言笔试.机试.研 ...

  4. 样本修改 sample_如何在R中使用sample()获取样本?

    样本修改 sample Let's understand one of the frequently used functions, sample() in R. In data analysis, ...

  5. Office Word中横线的增加和删除方法

    这里的横线指的是横在中间的线,不是用绘图画出来的,术语叫边框线. 一.增加方法: 前提是在自动更正选项中的键入时自动套用格式中勾选了"连字符(--)替换为长划线(-)". 1.连续 ...

  6. python random sample_python中的sample什么意思

    sample是random模块中的一个函数 表达式为:random.sample(sequence, k) 它的作用是从指定序列中随机获取指定长度的片断并随机排列,结果以列表的形式返回.注意:samp ...

  7. SQL Server数据表中数据的增加(插入)、查询、修改、删除

    目录 零.码仙励志 一.数据表中数据的增加(插入) 二.数据表中数据的查询 三.数据表中数据的修改 四.数据表中数据的删除 零.码仙励志 伟人所达到并保持着的高处,并不是一飞就到的,而是他们在同伴们都 ...

  8. JAVA Web项目中所出现错误及解决方式合集(不断更新中)

    JAVA Web项目中所出现错误及解决方式合集 前言 一.几个或许会用到的软件下载官网 二.Eclipse的[preferences]下没有[sever]选项 三.Tomcat的安装路径找不到 四.T ...

  9. 三位数自动递增编号函数_EXCEL中序号自动增加(如何避免隐藏行)?excel自动填充序号...

    excel表格中如何自动顺序填充序号 首先打开Excel 2007,创建如图所内容.当家也可以根据自己的实际情选择练习的内容.在此为了上的需要,特制作两列,第一列就"序号",第二列 ...

最新文章

  1. python中类似对象吗_在Python中,两个对象什么时候相同? - python
  2. 从view 得到图片
  3. 我是发起人Sumtec
  4. C++ 类里面,函数占用存储空间问题
  5. 课程设计---图书登记管理系统
  6. php验证卡号,PHP验证信用卡卡号是否正确函数
  7. 七度空间338多少钱一包_2020黄果树香烟一包多少钱 黄果树香烟价格表图排行榜...
  8. 图论 —— 最大团问题
  9. 计算机专业3d游戏设计,史塔福郡大学3D计算机游戏设计理学硕士研究生申请要求及申请材料要求清单...
  10. Golang程序性能分析(二)在Echo和Gin框架中使用pprof
  11. vue全局引入openlayers_vue项目中openlayers绘制行政区划
  12. 零基础怎么学好3D建模?只需要了解下面几点
  13. linux桌面lxde 安装_Ubuntu下安装LXDE桌面+noVNC直接WEB管理
  14. ESP32基于arduino和风天气获取代码
  15. uni-app的video禁止快进及seek()上的小坑
  16. Oracle的 wm_concat 的排序问题,Oracle的 listagg 函数[转]
  17. update select 语句和merge into语句
  18. 【改进模糊神经网络】基于粒子群-万有引力算法PSOGSA 改进的前馈神经网络研究(Matlab代码实现)
  19. 卡方检验的基本原理详解
  20. angular4子路由辅助路由

热门文章

  1. html - 好友列表 - 头像为名字最后一个字
  2. vs2019C语言fflush,vs2019清空输入缓冲区
  3. 码出高效:java开发手册_Java 11手册:Java专家分享他们在Java 11方面的最佳和最差的经验
  4. Ajax是什么?及实现步骤和原理
  5. android通知栏自定义高度,Android 自定义通知栏适配
  6. SystemUI 下拉通知栏快捷键加载流程
  7. c# hid usb 卡死_C# 访问USB(HID)设备
  8. 在Photoshop中绘制蜘蛛侠徽标
  9. Vue中使用高德地图(vue-amap)的采坑记录小白入门
  10. SCRM如何打通阿里天猫品牌电商会员管理?实现”会员通”、”营销通”、”服务通”...