基本思想:最近需要使用labelme标注文件来做区域配置文件,作为c++检测+euler追踪代码的区域文件使用,省去了添加不太直观的坐标点, 所以随手用rapjson写了个构建和解析的json文件代码,可以更好的照顾懒人
一、构建
39、使用NCNN+YOLOFast/YOLOV5,实现视频流/图片的自动化的labelme标注数据_sxj731533730-CSDN博客比如我使用
labelme --nodata
标注了一个文件
labelme文件内容
{
"version": "3.16.7",
"flags": {},
"shapes": [
{
"label": "left_restrict_region",
"line_color": null,
"fill_color": null,
"points": [
[
21.64102564102564,
421.56410256410254
],
[
530.6153846153846,
610.025641025641
]
],
"shape_type": "rectangle",
"flags": {}
},
{
"label": "right_restrict_region",
"line_color": null,
"fill_color": null,
"points": [
[
1210.102564102564,
424.1282051282052
],
[
1861.3846153846152,
616.4358974358975
]
],
"shape_type": "rectangle",
"flags": {}
},
{
"label": "public_region",
"line_color": null,
"fill_color": null,
"points": [
[
533.1794871794873,
422.8461538461537
],
[
1199.8461538461538,
613.8717948717948
]
],
"shape_type": "rectangle",
"flags": {}
}
],
"lineColor": [
0,
255,
0,
128
],
"fillColor": [
255,
0,
0,
128
],
"imagePath": "cap_human_00:00:03_01.jpg",
"imageData": null,
"imageHeight": 1080,
"imageWidth": 1920
}
代码构建和解析一下,下载rapidjson头文件,导入Clion即可
cmakelist.txt
cmake_minimum_required(VERSION 3.16)
project(untitled10)
include_directories(${CMAKE_SOURCE_DIR}/include)
set(CMAKE_CXX_STANDARD 14)
add_executable(untitled10 main.cpp)
测试代码
#include <iostream>
#include<string>
#include <fstream>
#include <vector>
#include <libgen.h>
#include "rapidjson/document.h"
#include "rapidjson/writer.h"
#include "rapidjson/stringbuffer.h"
typedef struct RectPoint {
double leftx;
double lefty;
double rightx;
double righty;
std::string label;
} RectPoint;
using namespace std;
using namespace rapidjson;
void constructJson(string imagePath,string frame_name, int imageHeight, int imageWidth,std::vector<RectPoint> objects) {
rapidjson::Document doc;
doc.SetObject();
rapidjson::Document::AllocatorType &allocator = doc.GetAllocator();
//成员1
rapidjson::Value str_version(rapidjson::kStringType);
str_version.SetString("3.16.7");
rapidjson::Value str_flags(rapidjson::kStringType);
str_flags.SetObject();
rapidjson::Value str_imageData(rapidjson::kStringType);
str_imageData.SetNull();
rapidjson::Value str_imageWidth(rapidjson::kStringType);
str_imageWidth.SetInt(imageWidth);
rapidjson::Value str_imageHeight(rapidjson::kStringType);
str_imageHeight.SetInt(imageHeight);
rapidjson::Value str_imagePath(rapidjson::kStringType);
std::string image_frame_name = frame_name + ".jpg";
str_imagePath.SetString(image_frame_name.c_str(), image_frame_name.length(), allocator);
rapidjson::Value ary(rapidjson::kArrayType);
rapidjson::Value sub_line_color_array0(rapidjson::kArrayType);
rapidjson::Value sub_full_color_array1(rapidjson::kArrayType);
for (int i = 0; i < objects.size(); i++) {
// 嵌套成员2对象
rapidjson::Document sub_doc;
sub_doc.SetObject();
rapidjson::Document::AllocatorType &sub_allocator = sub_doc.GetAllocator();
rapidjson::Value sub_str_shape_type(rapidjson::kStringType);
sub_str_shape_type.SetString("rectangle");
rapidjson::Value sub_str_flags(rapidjson::kStringType);
sub_str_flags.SetObject();
rapidjson::Value sub_str_group_id(rapidjson::kStringType);
sub_str_group_id.SetNull();
rapidjson::Value sub_str_line_color_id(rapidjson::kStringType);
sub_str_line_color_id.SetNull();
rapidjson::Value sub_str_full_color_id(rapidjson::kStringType);
sub_str_full_color_id.SetNull();
rapidjson::Value sub_str_label(rapidjson::kStringType);
std::string labelname =objects[i].label;
sub_str_label.SetString(labelname.c_str(), labelname.length(), allocator);
// 嵌套坐标点
rapidjson::Value sub_array0(rapidjson::kArrayType);
rapidjson::Value sub_array1(rapidjson::kArrayType);
rapidjson::Value sub_point(rapidjson::kArrayType);
float x_min = objects[i].leftx;
float y_min = objects[i].lefty;
float x_max = objects[i].rightx;
float y_max = objects[i].righty;
sub_array0.PushBack(x_min, allocator).PushBack(y_min, allocator);
sub_array1.PushBack(x_max, allocator).PushBack(y_max, allocator);
sub_point.PushBack(sub_array0, allocator);
sub_point.PushBack(sub_array1, allocator);
sub_doc.AddMember("points", sub_point, allocator);
// 嵌套坐标点完成
sub_doc.AddMember("shape_type", sub_str_shape_type, allocator);
sub_doc.AddMember("flags", sub_str_flags, allocator);
sub_doc.AddMember("group_id", sub_str_group_id, allocator);
sub_doc.AddMember("label", sub_str_label, allocator);
sub_doc.AddMember("line_color", sub_str_line_color_id, allocator);
sub_doc.AddMember("fill_color", sub_str_full_color_id, allocator);
ary.PushBack(sub_doc, allocator);
//成员2完成
}
//加入doc中
doc.AddMember("version", str_version, allocator);
doc.AddMember("flags", str_flags, allocator);
doc.AddMember("imageData", str_imageData, allocator);
doc.AddMember("imageWidth", imageWidth, allocator);
doc.AddMember("imageHeight", imageHeight, allocator);
doc.AddMember("imagePath", str_imagePath, allocator);
doc.AddMember("shapes", ary, allocator);
sub_line_color_array0.PushBack(0, allocator).PushBack(255, allocator).PushBack(0, allocator).PushBack(128,
allocator);
sub_full_color_array1.PushBack(255, allocator).PushBack(0, allocator).PushBack(0, allocator).PushBack(128,
allocator);
doc.AddMember("lineColor", sub_line_color_array0, allocator);
doc.AddMember("fillColor", sub_full_color_array1, allocator);
//转化为string
rapidjson::StringBuffer buffer;
rapidjson::Writer<rapidjson::StringBuffer> write(buffer);
doc.Accept(write);
std::string json = buffer.GetString();
// Output {"project":"rapidjson","stars":11}
std::cout << json << std::endl;
std::ofstream fout;
std::string destination_name = frame_name + ".json";
fout.open(destination_name); //可以使绝对和相对路径,用\\隔开目录,test, test.json, test.txt 都行,不局限于文件格式后缀,只要是文本文档
fout << buffer.GetString();
fout.close();
}
void parseJson(std::string jsonFile) {
std::ifstream config(jsonFile);
std::string strConfig((std::istreambuf_iterator<char>(config)),
std::istreambuf_iterator<char>());
rapidjson::Document document;
rapidjson::Value Obj,item,subItem;
if (!document.Parse(strConfig.c_str()).HasParseError()) {
if (document.HasMember("version") && document["version"].IsString()) {
std::cout << "version:" << document["version"].GetString() << std::endl;
}
if (document.HasMember("flags") && document["flags"].IsObject()) {
std::cout << "flags:{}" << std::endl;
}
if (document.HasMember("imageData") && document["imageData"].IsNull()) {
std::cout << "imageData:null" << std::endl;
}
if (document.HasMember("imageWidth") && document["imageWidth"].IsInt()) {
std::cout << "imageWidth:" << document["imageWidth"].GetInt() << std::endl;
}
if (document.HasMember("imageHeight") && document["imageHeight"].IsInt()) {
std::cout << "imageHeight:" << document["imageHeight"].GetInt() << std::endl;
}
if (document.HasMember("imagePath") && document["imagePath"].IsString()) {
std::cout << "imagePath:" << document["imagePath"].GetString() << std::endl;
}
if (document.HasMember("shapes") && document["shapes"].IsArray()) {
Obj = document["shapes"];
for(int i=0;i<Obj.Size();i++)
{
item= Obj[i];
if (item.HasMember("points") && item["points"].IsArray()) {
subItem=item["points"];
if (subItem.IsArray()){
for(int j=0;j<subItem.Size();j++){
std::cout<<"points:"<<subItem[0][j].GetDouble()<<" "<<subItem[1][j].GetDouble()<<std::endl;
}
}
}
if (item.HasMember("shape_type") && item["shape_type"].IsString()){
std::cout<<"shape_type:"<<item["shape_type"].GetString()<<std::endl;
}
if (item.HasMember("flags") && item["flags"].IsObject()){
std::cout<<"flags:{}"<<std::endl;
}
if (item.HasMember("group_id") && item["group_id"].IsNull()){
std::cout<<"group_id::null"<<std::endl;
}
if (item.HasMember("label") && item["label"].IsString()){
std::cout<<"label::"<<item["label"].GetString()<<std::endl;
}
if (item.HasMember("line_color") && item["line_color"].IsNull()){
std::cout<<"line_color::null"<<std::endl;
}
if (item.HasMember("fill_color") && item["fill_color"].IsNull()){
std::cout<<"fill_color::null"<<std::endl;
}
}
}
if (document.HasMember("lineColor") && document["lineColor"].IsArray()) {
std::cout << "lineColor:";
for(int i(0);i<document["lineColor"].Size();i++){
std::cout<< document["lineColor"][i].GetDouble()<<",";
}
std::cout<<std::endl;
}
if (document.HasMember("fillColor") && document["fillColor"].IsArray()) {
std::cout << "fillColor:";
for(int i(0);i<document["fillColor"].Size();i++){
std::cout<< document["fillColor"][i].GetDouble()<<",";
}
std::cout<<std::endl;
}
}
}
int main() {
string imagePath = "region.jpg";
int imageHeight = 1080;
int imageWidth = 1920;
std::vector<RectPoint> object = {
{21.64102564102564,
421.56410256410254,
530.6153846153846,
610.025641025641,
"left_restrict_region"
},
{1210.102564102564,
424.1282051282052,
1861.3846153846152,
616.4358974358975,
"right_restrict_region"
},
{533.1794871794873,
422.8461538461537,
1199.8461538461538,
613.8717948717948,
"public_region"}
};
std::string::size_type iPos = imagePath.find_last_of('\\') + 1;
std::string base_name = imagePath.substr(iPos, imagePath.length() - iPos);
std::string label_ext = base_name;
std::string frame_name = label_ext.substr(0, label_ext.rfind("."));
std::cout<<"----------------------------------constructJson ----------------------------------------"<<std::endl;
constructJson(imagePath,frame_name, imageHeight, imageWidth,object);
std::cout<<"----------------------------------parseJson ----------------------------------------"<<std::endl;
parseJson( frame_name + ".json");
return 0;
}
测试结果
F:\untitled10\cmake-build-debug\untitled10.exe
----------------------------------constructJson ----------------------------------------
{"version":"3.16.7","flags":{},"imageData":null,"imageWidth":1920,"imageHeight":1080,"imagePath":"region.jpg","shapes":[
{"points":[[21.64102554321289,421.5641174316406],[530.6153564453125,610.025634765625]],"shape_type":"rectangle","flags":
{},"group_id":null,"label":"left_restrict_region","line_color":null,"fill_color":null},{"points":[[1210.1025390625,424.1
282043457031],[1861.3846435546875,616.4359130859375]],"shape_type":"rectangle","flags":{},"group_id":null,"label":"right
_restrict_region","line_color":null,"fill_color":null},{"points":[[533.1795043945313,422.8461608886719],[1199.8461914062
5,613.8717651367188]],"shape_type":"rectangle","flags":{},"group_id":null,"label":"public_region","line_color":null,"fil
l_color":null}],"lineColor":[0,255,0,128],"fillColor":[255,0,0,128]}
----------------------------------parseJson ----------------------------------------
version:3.16.7
flags:{}
imageData:null
imageWidth:1920
imageHeight:1080
imagePath:region.jpg
points:21.641 530.615
points:421.564 610.026
shape_type:rectangle
flags:{}
group_id::null
label::left_restrict_region
line_color::null
fill_color::null
points:1210.1 1861.38
points:424.128 616.436
shape_type:rectangle
flags:{}
group_id::null
label::right_restrict_region
line_color::null
fill_color::null
points:533.18 1199.85
points:422.846 613.872
shape_type:rectangle
flags:{}
group_id::null
label::public_region
line_color::null
fill_color::null
lineColor:0,255,0,128,
fillColor:255,0,0,128,
测试结果(小伙伴的名字~昊天宗)
版权声明:本文为CSDN博主「sxj731533730」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/sxj731533730/article/details/122942347
暂无评论