一、问题导入
首先我们先进入到开放平台中,选择物体检测
最近在做一个项目,需要使用到海康威视AI开放平台来训练数据集,但是刚开始遇到了一个问题就是导入自己的数据集(txt格式转成了json格式)为啥没有用,后面查看相关文档,解决了导入自己数据集的问题,就不用在平台里标注了。
二、探索步骤
首先,这里需要下载客户端来使用,在数据服务里创建数据集(具体使用方法可以参考官方文档)
接着创建并导入选择本地数据
选择带标签输入
接着点击标注图片会出现小黑框
就可以进行标注了,最后标注完三四张后我就关闭了小黑框,在图像目录下会自动生成一个json文件,里面就包含标注文件。
三、txt转json格式
上面讲的内容主要是我一步一步探索的步骤,接下来,我拿到上面小黑框标注好的json文件进行解析,写了一个python脚本。将自己的yolo格式txt格式转换成json格式的文件。下面是所提供的代码
import os
import json
from PIL import Image
def load_classes(classes_file):
with open(classes_file, 'r', encoding='utf-8') as f:
return [line.strip() for line in f.readlines()]
def convert_yolo_to_json(labels_dir, images_dir, classes_file, output_json):
class_names = load_classes(classes_file)
json_data = {
"calibInfo": {
"cereal_class_version": 22,
"FormatVersion": "1.0",
"VideoChannels": []
}
}
video_channel = {
"cereal_class_version": 22,
"VideoChannelID": 0,
"VideoChanInfo": {
"cereal_class_version": 22,
"Description": ""
},
"MediaInfo": {
"cereal_class_version": 22,
"MediaType": 3,
"FilePath": images_dir,
"FileTime": 0,
"FrameNum": 0,
"FrameWidth": -1,
"FrameHeight": -1,
"breakFrameNum": ""
},
"BasicConfig": {
"cereal_class_version": 22,
"AlgorithmValue": 1,
"EventValue": 1,
"FrameStepLength": 10,
"MinObjWidth": 10,
"MinObjHeight": 10
},
"GeneralTag": {
"cereal_class_version": 22,
"SceneType": -1,
"Weather": ""
},
"VideoInfo": {
"cereal_class_version": 22,
"MainTagPage": {
"cereal_class_version": 22,
"PropertyPageName": "",
"PropertyPageDescript": "",
"TagGroups": [],
"IsDisplay": ""
},
"mapFrameInfos": []
},
"VideoChanEvents": []
}
for label_file in os.listdir(labels_dir):
if label_file.endswith('.txt'):
image_file = label_file.replace('.txt', '.jpg')
image_path = os.path.join(images_dir, image_file)
label_path = os.path.join(labels_dir, label_file)
if os.path.exists(image_path):
with Image.open(image_path) as img:
width, height = img.size
video_channel["MediaInfo"]["FrameWidth"] = width
video_channel["MediaInfo"]["FrameHeight"] = height
video_channel["MediaInfo"]["breakFrameNum"] = image_file
# Initialize mapFrameInfo
map_frame_info = {
"key": {
"cereal_class_version": 22,
"FrameNum": image_file,
"MediaType": 3
},
"value": {
"cereal_class_version": 22,
"FrameNum": image_file,
"TimeStamp": 0,
"OsdTime": 0,
"mapRules": [],
"mapTargets": [],
"mapFrameEvents": [],
"PropertyPages": []
}
}
with open(label_path, 'r') as f:
for line in f.readlines():
class_id, x_center, y_center, width_ratio, height_ratio = map(float, line.strip().split())
class_name = class_names[int(class_id)]
x1 = (x_center - width_ratio / 2) * width
y1 = (y_center - height_ratio / 2) * height
x2 = (x_center + width_ratio / 2) * width
y2 = (y_center + height_ratio / 2) * height
target_info = {
"key": len(map_frame_info["value"]["mapTargets"]) + 1,
"value": {
"TargetID": len(map_frame_info["value"]["mapTargets"]) + 1,
"TargetType": 1,
"Vertex": [
{"cereal_class_version": 22, "fX": x1 / width, "fY": y1 / height},
{"cereal_class_version": 22, "fX": x2 / width, "fY": y1 / height},
{"cereal_class_version": 22, "fX": x2 / width, "fY": y2 / height},
{"cereal_class_version": 22, "fX": x1 / width, "fY": y2 / height}
],
"PropertyPages": [
{
"PropertyPageName": str(class_id + 1),
"PropertyPageDescript": class_name,
"TagGroups": [],
"IsDisplay": "1"
}
]
}
}
map_frame_info["value"]["mapTargets"].append(target_info)
video_channel["VideoInfo"]["mapFrameInfos"].append(map_frame_info)
json_data["calibInfo"]["VideoChannels"].append(video_channel)
with open(output_json, 'w', encoding='utf-8') as json_file:
json.dump(json_data, json_file, ensure_ascii=False, indent=4)
print(f"转换完成,JSON文件已保存到 {output_json}")
labels_dir = r"E:\Users\Administrator\Desktop\test\train\labels"
images_dir = r"E:\Users\Administrator\Desktop\test\train\images"
classes_file = r"E:\Users\Administrator\Desktop\test\train\classes.txt"
output_json = r"E:\Users\Administrator\Desktop\test\train\merged_output.json"
convert_yolo_to_json(labels_dir, images_dir, classes_file, output_json)
只需要替换上面四个文件的目录,第一个目录我labels目录(txt文件),第二个目录是图像目录(训练图像文件),第三个是标签类别文件,第四个是输出的json文件。最后在训练图像目录下创建一个Result文件夹,将json文件移动到此文件下,接着在海康威视训练平台上回到下面这个界面,点击导入带标签文件,把json文件传入即可导入自己的数据集了。
标签:cereal,22,威视,version,---,json,导入,file,class From: https://blog.csdn.net/weixin_51971381/article/details/143201538