标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注
生活随笔
收集整理的這篇文章主要介紹了
标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 原
- 20200708 當(dāng)起始圖片序號(hào)不是1時(shí),可改成以下代碼
原
執(zhí)行evaluate.py后,會(huì)在predicted文件夾生成預(yù)測(cè)信息
如圖,將Yunyang tensorflow-yolov3 predicted轉(zhuǎn)換為正常yolo標(biāo)注
代碼:
結(jié)果:
20200708 當(dāng)起始圖片序號(hào)不是1時(shí),可改成以下代碼
# -*- coding: utf-8 -*- """ @File : convert_tf-predict2yolo.py @Time : 2020/2/13 15:57 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import re# 流程:按文件名依次讀取內(nèi)容(像讀取yolo標(biāo)注那樣讀取)--> 提取坐標(biāo)--> 將坐標(biāo)轉(zhuǎn)換成yolo格式--> 寫入目標(biāo)文件# 轉(zhuǎn)換參數(shù) # 圖像分辨率(如果圖片分辨率不一樣的話就需要重寫了aha) img_width, img_height = 1280, 720 # 文件數(shù)量 file_num = 800 # 起始圖片序號(hào) start = 10201 # 類號(hào)(只有一類的情況) class_num = '0'def extract_content(content):return re.findall('(.*?) (.*?) (.*?) (.*?) (.*?) (.*?)\n', content)if __name__ == '__main__':# 以下三個(gè)路徑是相對(duì)當(dāng)前文件的source_txt_path = './source_txt_path/'target_txt_path = './target_txt_path/'# 逐個(gè)打開文件處理for i in range(file_num):with open('{}{}.txt'.format(source_txt_path, i), 'r', encoding='utf-8') as file_read:# print(f.read())# object 0.6160 237 116 402 256# object 0.5585 655 21 807 152# object 0.3669 513 317 658 447# object 0.3459 418 484 590 629# ...# 讀取文件內(nèi)容content = file_read.read()# print(content)# object 0.6160 237 116 402 256# object 0.5585 655 21 807 152# object 0.3669 513 317 658 447# object 0.3459 418 484 590 629# ...# 提取數(shù)據(jù)content_extract = extract_content(content)# print(content_extract)# [('object', '0.6160', '237', '116', '402', '256'), ('object', '0.5585', '655', '21', '807', '152'),# ('object', '0.3669', '513', '317', '658', '447'), ('object', '0.3459', '418', '484', '590', '629')] # ...# 創(chuàng)建單文件寫入字符串對(duì)象obj_strs = ''# 將數(shù)據(jù)格式從絕對(duì)坐標(biāo)轉(zhuǎn)換為相對(duì)坐標(biāo)for obj_str in content_extract:# print(obj_str)# ('object', '0.6160', '237', '116', '402', '256')# ('object', '0.5585', '655', '21', '807', '152')# ('object', '0.3669', '513', '317', '658', '447')# ('object', '0.3459', '418', '484', '590', '629')# ('object', '0.5679', '452', '221', '621', '371')# ...# object_evar = list(map(eval, obj_str))# print(object_evar)# [<class 'object'>, 0.616, 237, 116, 402, 256]# [<class 'object'>, 0.5585, 655, 21, 807, 152]# [<class 'object'>, 0.3669, 513, 317, 658, 447]# ...# 去掉前倆個(gè)(類名和置信度)obj_str = obj_str[2:]# print(obj_str)# ('237', '116', '402', '256')# ('655', '21', '807', '152')# ('513', '317', '658', '447')# ...# 將元組字符串轉(zhuǎn)換成列表數(shù)字object_evar = list(map(eval, obj_str))# print(object_evar)# [237, 116, 402, 256]# [655, 21, 807, 152]# [513, 317, 658, 447]# ...# 映射變量a1, b1, a2, b2 = object_evar[0], object_evar[1], object_evar[2], object_evar[3]c1, c2, d1, d2 = (a1 + a2) / (2 * img_width), (b1 + b2) / (2 * img_height), (a2 - a1) / img_width, (b2 - b1) / img_height# print(c1, c2, d1, d2)# 將映射變量格式化后加入到obj_strs中:# 用“+”連接字符串不建議使用,占用內(nèi)存較大;建議使用join()方法,用''將可迭代字符串連接起來# obj_strs = obj_strs + class_num + (' {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(c1, c2, d1, d2))obj_strs = ''.join([obj_strs, class_num, ' {:.6f} {:.6f} {:.6f} {:.6f}\n'.format(c1, c2, d1, d2)])print(obj_strs)# 將即將寫入的內(nèi)容去除首位的無效字符(如空格,換行符,制表符,回車符)obj_strs = obj_strs.strip()# 將內(nèi)容寫入文件with open('{}{}.txt'.format(target_txt_path, i + start), 'w', encoding='utf-8') as file_write:file_write.write(obj_strs)運(yùn)行結(jié)果:
參考文章1:圖像識(shí)別 標(biāo)注(annotation)的反向優(yōu)化策略
參考文章2:標(biāo)注反向優(yōu)化 生成全體測(cè)試集空標(biāo)注(無需坐標(biāo)、只要送給權(quán)重evaluate即可)predicted
總結(jié)
以上是生活随笔為你收集整理的标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows如何清理无效注册表?如何重
- 下一篇: LabelImg 批量生成标注图片文件夹