首页 > 其他分享 >海康威视AI开放平台训练数据集导入问题---解决导入自己数据集的问题(txt转json格式)

海康威视AI开放平台训练数据集导入问题---解决导入自己数据集的问题(txt转json格式)

时间:2024-10-24 11:45:44浏览次数:3  
标签:cereal 22 威视 version --- json 导入 file class

一、问题导入

首先我们先进入到开放平台中,选择物体检测

最近在做一个项目,需要使用到海康威视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

相关文章

  • 在Windows中,可以使用PowerShell来迁移打印机设置,以下是用于导出和导入打印机配置的Pow
    在Windows中,可以使用PowerShell来迁移打印机设置,以下是用于导出和导入打印机配置的PowerShell代码示例。导出打印机设置使用以下命令导出当前打印机设置到一个文件:powershellCopyCodeGet-Printer|Export-Clixml-Path"C:\path\to\exported_printers.xml"导入打印机设置......
  • IDEA 2024.2.2 最新安装教程(附激活-2099年~)
    访问IDEA官网下载IDEA2024.2.2版本的安装包。下载补丁https://pan.quark.cn/s/fcc23ab8cadf检查进入IDEA中后,点击菜单Help|Register,即可查看IDEA的激活到期时间:免责声明:本文中的资源均来自互联网,仅供个人学习和交流使用,严禁用于商业行为,下载后请在24小......
  • 【2024-10-23】生活主干道
    20:00梧叶新黄柿叶红,更兼乌桕与丹枫。只言山色秋萧索,绣出西湖三四峰。                                                 ——《秋山秋雨蚤作有叹》 宋·杨万里昨天下......
  • CSP-S 考前做题
    P11218首先还是博弈论题要么打表找规律要么dp。然后注意到\(2\timesn\)网格这个东西非常典,状态数很少,可以状压。首先我们做一点观察:不难发现\(11\)一定全选,\(00\)可以选一个,然后\(01\)可以选两个然后没贡献,这样他就没法换,跑一遍最大子段和。然后注意到他换只能换......
  • docker以及docker-compose 离线安装
    一、离线安装docker1.下载离线包去官网下载离线包https://download.docker.com/linux/static/stable/ 我这里下载的是X86_64的包, 2.安装dockersudotarzxvfdocker-20.10.13.tgz  将docker目录下面的文件全部拷贝到/usr/bin/sudocp-pdocker/*/usr/bin将d......
  • object references an unsaved transient instance - save the transient instance be
    org.hibernate.TransientPropertyValueException:objectreferencesanunsavedtransientinstance-savethetransientinstancebeforeflushing:com.jms.cfc.product.domain.ProductMaintainHistory.product->com.One;nestedexceptionisjava.lang.IllegalSt......
  • 川土微一级代理商|内部集成隔离电源的隔离485芯片CA-IS2092W、CA-IS2092VW
    CA-IS2092W、CA-IS2092VW支持多节点数据通信,总线引脚具有±20kVHBMESD保护,可耐受高级别的ESD事件,保护内部电路不受损害。接收器输入阻抗为1/8单位负载,允许同一总线上最多挂接256个收发器。CA-IS2092W、CA-IS2092VW提供DC-DC转换器和RS-485收发器供电独立的版本(CA-IS2092VW),便于逻......
  • 2FA-双因素认证
    双因素认证(2FA,Two-FactorAuthentication)是一种提高安全性的方法,要求用户在登录或进行某些敏感操作时提供两种不同类型的身份验证信息。这种方法通过引入第二层验证,增加了账户被未经授权访问的难度。项目结构spring-boot-2fa-demo├──src│├──main││......
  • InternVL-1.0: Scaling up Vision Foundation Models and Aligningfor Generic Visual
    论文:https://arxiv.org/abs/2312.14238代码:https://github.com/OpenGVLab/InternVL背景在LLM时代,视觉基础模型通常通过一些轻量级的“粘合”层(如QFormer或线性投影)与LLMs连接。然而,这些模型主要源自ImageNet或JFT等纯视觉数据集,或使用图像文本对与BERT系列对齐,缺乏与L......
  • 学习高校课程-软件设计模式-工厂模式(lec3)
    FactoryMethod:ProblemExample:alogisticsmanagementapplication示例:物流管理应用程序–Firstversion:handlingtransportationbytrucks,withaTruckclass–Later:newrequeststoincorporatesealogistics,andmore–第一个版本:处理卡车运输,卡车类别–......