如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations)
生活随笔
收集整理的這篇文章主要介紹了
如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations)
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
原理
代碼
# -*- coding: utf-8 -*- """ @File : convert_tf-predict2yolo.py @Time : 2020/2/13 15:57 @Author : Dontla @Email : sxana@qq.com @Software: PyCharm """ # -*- 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 = 4670 # 類號(hào)(只有一類的情況) class_num = '0'def extract_content(content):return re.findall('(.*?) (.*?) (.*?) (.*?) (.*?) (.*?)\n', content)if __name__ == '__main__':# 以下三個(gè)路徑是相對當(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)建單文件寫入字符串對象obj_strs = ''# 將數(shù)據(jù)格式從絕對坐標(biāo)轉(zhuǎn)換為相對坐標(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 + 1), 'w', encoding='utf-8') as file_write:file_write.write(obj_strs)
參考文章:如何將yolo的標(biāo)注(annotations).txt 坐標(biāo)轉(zhuǎn)換成tensorflow-yolov3(YunYang1994)的.txt 標(biāo)注坐標(biāo)?
總結(jié)
以上是生活随笔為你收集整理的如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 图像识别 标注(annotation)的
- 下一篇: 修改labelImg软件的yolo标注写