python實現(xiàn)json轉(zhuǎn)yolo格式
json轉(zhuǎn)yolo格式
視覺分割得一些標注文件是json格式,比如,舌頭將這個舌頭區(qū)域分割出來(用mask二值圖的形式),對舌頭的分割第一步是需要檢測出來,缺少數(shù)據(jù)集,可以使用分割出來的結(jié)果,將分割的結(jié)果轉(zhuǎn)化成可以用于目標檢測的數(shù)據(jù)集。
下面是將json文件轉(zhuǎn)化成一個yolov8的數(shù)據(jù)格式,首先看一下json的數(shù)據(jù)格式:
json的數(shù)據(jù)格式
我關注的就是"shapes"這個字段因為它是我舌頭區(qū)域的坐標點,其次關注的是 “imageHeight”: 圖片的高, “imageWidth”: 圖片的寬。這些在生成yolov8格式的檢測框的時候啥都有用。
{ "version": "5.2.1", "flags": {}, "shapes": [ { "fill_color": null, "line_color": null, "label": "tongue", "points": [ [ 700.361963190184, 510.8926380368097 ], ....... [ 573.9815950920246, 515.1871165644171 ], ], "group_id": null, "description": null, "shape_type": "polygon", "flags": {} } ], "imagePath": "0000.jpg", "imageData": "iVBORw0KGgoA.....................AAAAAElFTkSuQmCC", "imageHeight": 777, "imageWidth": 1286, "fillColor": [ 255, 0, 0, 128 ], "lineColor": [ 0, 255, 0, 128 ] }
yolo數(shù)據(jù)格式
對應的yolov8的數(shù)據(jù)格式就是yolo系列的標簽存儲形式
yolo系列對應的是[class x y w’ h’]。注意 class也就是label標簽, x y 就是(x, y)表示中心橫坐標與圖像寬度、高度的比值,w’ :檢測框box寬度與圖像寬度比值,h’:檢測框高度與圖像高度比值。
# 一個txt文件 0 0.507394403152401 0.5280297826310096 0.49941035598087944 0.33793653425555276 1 0.407394403152401 0.9280297826310096 0.19941035598087944 0.33793653425555276 2 0.37394403152401 0.5280297826310096 0.19941035598087944 0.13793653425555276
代碼
def json_to_yolov8(data): # 獲取原圖的寬和高 image_width = data['imageWidth'] image_height = data['imageHeight'] for shape in data['shapes']: if shape['label'] == 'tongue': points = shape['points'] x_min = min(point[0] for point in points) x_max = max(point[0] for point in points) y_min = min(point[1] for point in points) y_max = max(point[1] for point in points) x_center = (x_min + x_max) / 2 y_center = (y_min + y_max) / 2 w = x_max - x_min h = y_max - y_min x_center /= image_width y_center /= image_height w /= image_width h /= image_height yolov8_box = [0, x_center, y_center, w, h] return yolov8_box # Replace 'your_json_file.json' and 'your_image.jpg' with the actual paths json_folder = "path/to/json" # 輸入json文件的路徑位置 yolov8_labels = 'path/to/txt' # 輸出的目標文件存放路徑 for json_file in os.listdir(json_folder): if json_file.endswith('.json'): json_name = os.path.basename(json_file).split('.')[0] output_file = os.path.join(yolov8_labels, f'{json_name}.txt') jsonfile = os.path.join(json_folder, f'{json_name}.json') with open(jsonfile, 'r') as file: data = json.load(file) yolov8_box = json_to_yolov8(data) with open(output_file, 'w') as f: result_str = ' '.join(str(data) for data in yolov8_box) f.write(result_str) print("over!")
到此這篇關于python實現(xiàn)json轉(zhuǎn)yolo格式的文章就介紹到這了,更多相關python json轉(zhuǎn)yolo內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
python?中的np.zeros()和np.ones()函數(shù)詳解
這篇文章主要介紹了python?中的np.zeros()和np.ones()函數(shù),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04python定間隔取點(np.linspace)的實現(xiàn)
今天小編就為大家分享一篇python定間隔取點(np.linspace)的實現(xiàn),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-11-11Python的Django中django-userena組件的簡單使用教程
這篇文章主要介紹了Python的Django中django-userena組件的簡單使用教程,包括用戶登陸和注冊等簡單功能的實現(xiàn),需要的朋友可以參考下2015-05-05python中關于requests里的timeout()用法
這篇文章主要介紹了python中關于requests里的timeout()用法,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-08-08一次性徹底講透Python中pd.concat與pd.merge
本文主要介紹了一次性徹底講透Python中pd.concat與pd.merge,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-06-06