python實(shí)現(xiàn)json轉(zhuǎn)yolo格式
json轉(zhuǎn)yolo格式
視覺分割得一些標(biāo)注文件是json格式,比如,舌頭將這個(gè)舌頭區(qū)域分割出來(用mask二值圖的形式),對(duì)舌頭的分割第一步是需要檢測(cè)出來,缺少數(shù)據(jù)集,可以使用分割出來的結(jié)果,將分割的結(jié)果轉(zhuǎn)化成可以用于目標(biāo)檢測(cè)的數(shù)據(jù)集。
下面是將json文件轉(zhuǎn)化成一個(gè)yolov8的數(shù)據(jù)格式,首先看一下json的數(shù)據(jù)格式:
json的數(shù)據(jù)格式
我關(guān)注的就是"shapes"這個(gè)字段因?yàn)樗俏疑囝^區(qū)域的坐標(biāo)點(diǎn),其次關(guān)注的是 “imageHeight”: 圖片的高, “imageWidth”: 圖片的寬。這些在生成yolov8格式的檢測(cè)框的時(shí)候啥都有用。
{
"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ù)格式
對(duì)應(yīng)的yolov8的數(shù)據(jù)格式就是yolo系列的標(biāo)簽存儲(chǔ)形式
yolo系列對(duì)應(yīng)的是[class x y w’ h’]。注意 class也就是label標(biāo)簽, x y 就是(x, y)表示中心橫坐標(biāo)與圖像寬度、高度的比值,w’ :檢測(cè)框box寬度與圖像寬度比值,h’:檢測(cè)框高度與圖像高度比值。
# 一個(gè)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' # 輸出的目標(biāo)文件存放路徑
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!")到此這篇關(guān)于python實(shí)現(xiàn)json轉(zhuǎn)yolo格式的文章就介紹到這了,更多相關(guān)python json轉(zhuǎn)yolo內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
python?中的np.zeros()和np.ones()函數(shù)詳解
這篇文章主要介紹了python?中的np.zeros()和np.ones()函數(shù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-04-04
python定間隔取點(diǎn)(np.linspace)的實(shí)現(xiàn)
今天小編就為大家分享一篇python定間隔取點(diǎn)(np.linspace)的實(shí)現(xiàn),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-11-11
python實(shí)現(xiàn)不同電腦之間視頻傳輸功能
這篇文章主要介紹了python實(shí)現(xiàn)不同電腦之間視頻傳輸,本文視頻傳輸實(shí)現(xiàn)的前提是確保發(fā)送端和接收端接在同一個(gè)局域網(wǎng)下,分為發(fā)送端和接收端,通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-06-06
Python的Django中django-userena組件的簡單使用教程
這篇文章主要介紹了Python的Django中django-userena組件的簡單使用教程,包括用戶登陸和注冊(cè)等簡單功能的實(shí)現(xiàn),需要的朋友可以參考下2015-05-05
python中關(guān)于requests里的timeout()用法
這篇文章主要介紹了python中關(guān)于requests里的timeout()用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
一次性徹底講透Python中pd.concat與pd.merge
本文主要介紹了一次性徹底講透Python中pd.concat與pd.merge,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06

