本人在生成ava数据集的时候发现yolo对目标的检测情况不大好,导致最终放到slowfast中回归效果差,因此决定将via标注好的数据放到yolo中进行训练。
我大概解释以下,在标注的文件中一个json对应了一个视频的多个图片,这里拿我编辑的视频1解释:image1是视频1里面第一张照片,后面的_1是里面有一个对象。这里就是第一个视频的第一张图片有一个检测对象。
"image1_1": {
"vid": "1",
"xy": [
2,
0.0006400000000184036,
6.999840000000006,
1245.99936,
713.00016
],
"av": {
"1": "0",
"2": "",
"3": "",
"4": "",
"5": ""
},
"flg": 0,
"z": []
},
"image2_1": {
"vid": "2",
"xy": [
2,
1,
3.26914,
444.11931,
171.65494
],
"av": {
"1": "0",
"2": "",
"3": "",
"4": "",
"5": ""
},
"flg": 0,
"z": []
},
如果在用via标注的过程中回来标注前面的图片,则会变成这样:
大概意思就是我分别在第12张照片和第13张照片中补充了一个对象的检测框
"12_HndcTSRv": {
"vid": "12",
"flg": 0,
"z": [],
"xy": [
2,
437.31,
6.247,
441.996,
82.777
],
"av": {
"1": "0",
"2": "",
"3": "",
"4": "",
"5": ""
}
},
"13_4XM1e1nr": {
"vid": "13",
"flg": 0,
"z": [],
"xy": [
2,
546.638,
4.685,
452.928,
106.204
],
"av": {
"1": "0",
"2": "",
"3": "",
"4": "",
"5": ""
}
}
介于我标注的结果有以上两种情况,我撰写以下代码解决问题
以下是转化代码:
import os
import json
from PIL import Image
import re
def extract_key_value(json_file, target_key):
with open(json_file, 'r',encoding='utf-8') as file:
data = json.load(file)
if target_key in data:
return data[target_key]
else:
return None
def main():
folder_path = 'C:/Users/yuqi zhang/Desktop/finish2'
img_path = 'C:/Users/yuqi zhang/Desktop/finish2/img/'
target_key = "metadata" # Change this to the desired key
save_path = 'C:/Users/yuqi zhang/Desktop/data'
#打开文件夹读取json文件中value的内容
for root, _, files in os.walk(folder_path):#打开文件夹
json_files = [f for f in files if f.endswith('.json')]
for json_file in json_files: #打开文件夹的文件
# with open(json_file, 'w') as file:
file_seq=json_file.split("_")[0]
# print(file_seq)
json_path = os.path.join(root, json_file)
# print(json_path)
value = extract_key_value(json_path, target_key)
# print(value)
#构建对应的txt文件
for i in range(1,17):
img= 30+i*15 +1
img_seq=str(img).rjust(6, "0")
txt_file=file_seq + "_"+img_seq+".txt"
# regex_pattern = str(i)+"_"
print("-------------------------------------------")
contentList1 = search_json(value, "image"+str(i)+"_")
contentList = search_json(value, "^"+str(i)+"_")
txt_path = os.path.join(save_path, txt_file)
with open(txt_path,"w") as txt_file:
# print(results)
# print(len(contentList))
for i in range(len(contentList1)):
x=contentList1[i][1]
y=contentList1[i][2]
w=contentList1[i][3]
h=contentList1[i][4]
print(x,y,w,h)
if x < 0 : x=0
if y < 0 : y=0
if w < 0 : w=0
if h < 0 : h=0
img_file=file_seq + "_"+img_seq+".jpg"
img_p=img_path+img_file
img = Image.open(img_p)
img_w=img.width
img_h=img.height
x1=round((x+w/2)/img_w,6)
y1=round((y+h/2)/img_h,6)
w1=round(w/img_w,6)
h1=round(h/img_h,6)
txt_file.write(f"0")
txt_file.write(f" {x1}")
txt_file.write(f" {y1}")
txt_file.write(f" {w1}")
txt_file.write(f" {h1}\n")
for i in range(len(contentList)):
x=contentList[i][1]
y=contentList[i][2]
w=contentList[i][3]
h=contentList[i][4]
print(x,y,w,h)
if x < 0 : x=0
if y < 0 : y=0
if w < 0 : w=0
if h < 0 : h=0
img_file=file_seq + "_"+img_seq+".jpg"
img_p=img_path+img_file
img = Image.open(img_p)
img_w=img.width
img_h=img.height
x1=round((x+w/2)/img_w,6)
y1=round((y+h/2)/img_h,6)
w1=round(w/img_w,6)
h1=round(h/img_h,6)
txt_file.write(f"0")
txt_file.write(f" {x1}")
txt_file.write(f" {y1}")
txt_file.write(f" {w1}")
txt_file.write(f" {h1}\n")
def search_json(data, regex):
result = {}
contentList = []
for key, value in data.items():
if re.search(regex, key):
result[key] = value['xy']
contentList.append(value['xy'])
elif isinstance(value, dict):
sub_result = search_json(value, regex)
if sub_result:
result[key] = sub_result
# print(result[key])
return contentList
if __name__ == '__main__':
main()