import json
import os
def normalize_coordinates(x, y, img_width, img_height):
normalized_x = x / img_width
normalized_y = y / img_height
return normalized_x, normalized_y
def convert_to_yolo(json_file, output_file):
labels = ["unlabeled", "ego vehicle", "rectification border", "out of roi", "static", "dynamic", "ground", "road", "sidewalk", "parking", "rail track", "building", "wall", "fence", "guard rail", "bridge", "tunnel", "pole", "polegroup", "traffic light", "traffic sign", "vegetation", "terrain", "sky", "person", "rider", "car", "truck", "bus", "caravan", "trailer", "train", "motorcycle", "bicycle", "license plate","cargroup","bicyclegroup",'persongroup']
with open(json_file, 'r') as f:
data = json.load(f)
base_name = os.path.splitext(os.path.basename(json_file))[0]
output_file = f"{base_name}.txt"
with open(output_file, 'w') as f:
for obj in data['objects']:
label = obj['label']
if label in labels:
label_index = labels.index(label)
f.write(f"{label_index} ")
for point in obj['polygon']:
x, y = normalize_coordinates(point[0], point[1], data['imgWidth'], data['imgHeight'])
f.write(f"{x} {y} ")
f.write("\n")
# Example usage
json_folder = r"E:\jsonlabels\jsonlabels\test"
json_files = [f for f in os.listdir(json_folder) if f.endswith('.json')]
for json_file in json_files:
json_path = os.path.join(json_folder, json_file)
convert_to_yolo(json_path, json_folder)
onecocotoyolo文件一对一10.26
最新推荐文章于 2024-02-07 22:09:49 发布