标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注
生活随笔
收集整理的這篇文章主要介紹了
标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 原
- 20200708 當起始圖片序號不是1時,可改成以下代碼
原
執行evaluate.py后,會在predicted文件夾生成預測信息
如圖,將Yunyang tensorflow-yolov3 predicted轉換為正常yolo標注
代碼:
結果:
20200708 當起始圖片序號不是1時,可改成以下代碼
# -*- coding: utf-8 -*- """ @File : convert_tf-predict2yolo.py @Time : 2020/2/13 15:57 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ import re# 流程:按文件名依次讀取內容(像讀取yolo標注那樣讀取)--> 提取坐標--> 將坐標轉換成yolo格式--> 寫入目標文件# 轉換參數 # 圖像分辨率(如果圖片分辨率不一樣的話就需要重寫了aha) img_width, img_height = 1280, 720 # 文件數量 file_num = 800 # 起始圖片序號 start = 10201 # 類號(只有一類的情況) class_num = '0'def extract_content(content):return re.findall('(.*?) (.*?) (.*?) (.*?) (.*?) (.*?)\n', content)if __name__ == '__main__':# 以下三個路徑是相對當前文件的source_txt_path = './source_txt_path/'target_txt_path = './target_txt_path/'# 逐個打開文件處理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# ...# 讀取文件內容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# ...# 提取數據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')] # ...# 創建單文件寫入字符串對象obj_strs = ''# 將數據格式從絕對坐標轉換為相對坐標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]# ...# 去掉前倆個(類名和置信度)obj_str = obj_str[2:]# print(obj_str)# ('237', '116', '402', '256')# ('655', '21', '807', '152')# ('513', '317', '658', '447')# ...# 將元組字符串轉換成列表數字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中:# 用“+”連接字符串不建議使用,占用內存較大;建議使用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)# 將即將寫入的內容去除首位的無效字符(如空格,換行符,制表符,回車符)obj_strs = obj_strs.strip()# 將內容寫入文件with open('{}{}.txt'.format(target_txt_path, i + start), 'w', encoding='utf-8') as file_write:file_write.write(obj_strs)運行結果:
參考文章1:圖像識別 標注(annotation)的反向優化策略
參考文章2:標注反向優化 生成全體測試集空標注(無需坐標、只要送給權重evaluate即可)predicted
總結
以上是生活随笔為你收集整理的标注(annotation)的反向优化策略 将Yunyang tensorflow-yolov3 predicted转换为正常yolo标注的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: windows如何清理无效注册表?如何重
- 下一篇: LabelImg 批量生成标注图片文件夹