日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations)

發布時間:2025/3/20 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

原理


代碼

# -*- 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# 流程:按文件名依次讀取內容(像讀取yolo標注那樣讀取)--> 提取坐標--> 將坐標轉換成yolo格式--> 寫入目標文件# 轉換參數 # 圖像分辨率(如果圖片分辨率不一樣的話就需要重寫了aha) img_width, img_height = 1280, 720 # 文件數量 file_num = 4670 # 類號(只有一類的情況) 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 + 1), 'w', encoding='utf-8') as file_write:file_write.write(obj_strs)





參考文章:如何將yolo的標注(annotations).txt 坐標轉換成tensorflow-yolov3(YunYang1994)的.txt 標注坐標?

總結

以上是生活随笔為你收集整理的如何将tensorflow-yolov3(YunYang1994).txt 坐标转换成yolo的标注(annotations)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。