对于文件比较复杂的时候,为了获取文件中的信息,需要一些比较特殊的函数,比如,getline()、replace()、atoi,atof等

例子一,读取以下文件中的数据,并保存进一个类里面。

首先,类的定义如下,感觉是struct,但是按照struct的处理,我这段代码出错了,不知道什么问题,暂时po出来吧,有空看看。

struct ImageLabel{
std::string imagePath;//save图片路径名
int faceBox[];//输入点集的最外层包络矩形rect四个参数
int landmarkPos[*LandmarkPointsNum];//输入点集 private:
friend class cereal::access;
/**
* Serialises this class using cereal.
*
* @param[in] ar The archive to serialise to (or to serialise from).
*/
template<class Archive>
void serialize(Archive& ar)
{
ar(imagePath, faceBox, landmarkPos);
}
};

然后读入一系列如下文件,每个文件的数据保存格式相同,因此定义了一个vector,来顺序存储每一个文件对应的类

文件如下:*.pts文件

version:
n_points:
{
446.000 91.000
449.459 119.344
450.957 150.614
460.552 176.986
471.486 202.157
488.087 226.842
506.016 246.438
524.662 263.865
553.315 271.435
578.732 266.260
599.361 248.966
615.947 220.651
627.439 197.999
635.375 179.064
642.063 156.371
647.302 124.753
646.518 92.944
470.271 117.870
486.218 109.415
503.097 114.454
519.714 120.090
533.680 127.609
571.937 123.590
585.702 117.155
602.344 109.070
620.077 103.951
633.964 111.236
554.931 145.072
554.589 161.106
554.658 177.570
554.777 194.295
532.717 197.930
543.637 202.841
555.652 205.483
565.441 202.069
576.368 197.061
487.474 136.436
499.184 132.337
513.781 133.589
527.594 143.047
513.422 144.769
499.117 144.737
579.876 140.815
590.901 130.008
605.648 128.376
618.343 132.671
606.771 140.525
593.466 141.419
519.040 229.040
536.292 221.978
}

所有pts文件名都保存在一个TXT文件中,对应的图像名也保存在一个TXT文件中,我们需要把图片的文件路径加文件名保存到类里面,为了处理方便我们用了replace函数,直接替换pts文件名的的后三位,得到新的文件名。

void readpic(std::vector<ImageLabel> &Imagelabels){
cout<<"test readpic"<<endl; string filePath = "/Users/anitafang/Downloads/Datasets/300W/300w/01_Indoor/";
//打开png图片名存储的TXT文件
ifstream pngfile;
pngfile.open("/Users/anitafang/Downloads/Datasets/300W/300w/01_Indoor/pnglist.txt");
if(!pngfile.is_open()){
cout<<"不能打开文件!"<<endl;
}
//ImageLabel* mImageLabel = NULL;//保存图像信息的类
string line;//读取文件的每一行
while(getline(pngfile,line)){
//mImageLabel = new ImageLabel();
ImageLabel mImageLabel;
//mImageLabel->imagePath=filePath+line;//将文件全路径保存在 mImageLabel->imagePath
mImageLabel.imagePath=filePath+line;//将文件全路径保存在 mImageLabel->imagePath
cout<<line<<endl;
//得到pts文件路径
string ress="pts";
string ptss=filePath+line.replace(,,ress);
cout<<line.replace(,,ress)<<endl;
cout<<ptss<<endl;
//读取pts文件里面的数据
std::ifstream LabelsFile(ptss, std::ios::in);
if(!LabelsFile.is_open())
return;
//开始读取pts文件内容
int index=;
vector<Point2f> vp2f;
char line11[]={};//存放每行数据
//将文件逐行读取到string linestr中,然后对行的内容进行判断
while(LabelsFile.getline(line11, sizeof(line11))){
//从第四行开始把数据写进landmark数组中
if((index>=)&&(index<)){
string x = "";
string y = "";
std::stringstream word(line11);
word >> x;
word >> y;
cout<<atof(x.c_str())<<" "<<atof(y.c_str())<<endl;
mImageLabel.landmarkPos[index-] =atof(x.c_str());
mImageLabel.landmarkPos[index+LandmarkPointsNum-] =atof(y.c_str()); vp2f.push_back(Point2f(mImageLabel.landmarkPos[index-],mImageLabel.landmarkPos[index+LandmarkPointsNum-]));
cout<<"x:"<<mImageLabel.landmarkPos[index-]<<" y:"<<mImageLabel.landmarkPos[index+LandmarkPointsNum-]<<endl; }
index++; } Rect rect = boundingRect(vp2f); //根据得到的矩形 输入到facebox中
mImageLabel.faceBox[] = rect.x;
mImageLabel.faceBox[] = rect.y;
mImageLabel.faceBox[] = rect.width;
mImageLabel.faceBox[] = rect.height; cout<<"facebox"<<mImageLabel.faceBox[]<<mImageLabel.faceBox[]<<mImageLabel.faceBox[]<<mImageLabel.faceBox[]<<endl; // close file
LabelsFile.close();
//free the object
Imagelabels.push_back(mImageLabel); //mImageLabel == NULL; }
}

其中,因为GetLine读出来的数据是string类型,因此需要将数据转成int型,看到pts文件中数据是都是float型,因此先要将string转成float,用的是atof函数。

另外如果把 Imagelabels 当做类来处理只需要,最开始,ImageLabel mImageLabel;后面直接push_back,不需要delete。所有的引用都变成:mImageLabel.faceBox,不能用mImageLabel->landmarkPos。

另外一个升级的例子,处理起来很复杂,这里把ImageLabel当做一个struct来处理,这样需要new,也需要delete,引用的时候也是用的->,这个需要注意。

文件是:labels_ibug_300W.xml

稍微看一部分,了解下它的构成。

<?xml version='1.0' encoding='ISO-8859-1'?>
<?xml-stylesheet type='text/xsl' href='image_metadata_stylesheet.xsl'?>
<dataset>
<name>iBUG face point dataset - All images</name>
<comment>This folder contains data downloaded from:
http://ibug.doc.ic.ac.uk/resources/facial-point-annotations/ The dataset is actually a combination of the AFW, HELEN, iBUG, and LFPW
face landmark datasets. But the iBUG people have aggregated it all together
and gave them a consistent set of landmarks across all the images, thereby
turning it into one big dataset. Note that we have adjusted the coordinates of the points from the MATLAB convention
of being the first index to being the first index. So the coordinates in this
file are in the normal C -indexed coordinate system. We have also added left right flips (i.e. mirrors) of each image and also
appropriately flipped the landmarks. This doubles the size of the dataset.
Each of the mirrored versions of the images has a filename that ends with
_mirror.jpg. Finally, note that the bounding boxes are from dlib's default face detector. For the
faces the detector failed to detect, we guessed at what the bounding box would have been
had the detector found it and used that.</comment>
<images>
<image file='afw/1051618982_1.jpg'>
<box top='' left='' width='' height=''>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
</box>
</image>
<image file='afw/111076519_1.jpg'>
<box top='' left='' width='' height=''>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
</box>
</image>
<image file='afw/111076519_2.jpg'>
<box top='' left='' width='' height=''>
<part name='' x='' y=''/>
<part name='' x='' y=''/>
<part name='' x='' y=''/>

可以找到一些规律,按照这些规律来得到我们需要的数据,直接上代码:

void ReadLabelsFromFile(std::vector<ImageLabel> &Imagelabels, std::string Path = "labels_ibug_300W.xml"){
std::string ParentPath(trainFilePath);
std::ifstream LabelsFile(ParentPath+Path, std::ios::in);
if(!LabelsFile.is_open())
return;
std::string linestr;
while(std::getline(LabelsFile, linestr)){
linestr = trim(linestr);
linestr = replace(linestr, "</", "");
linestr = replace(linestr, "/>", "");
linestr = replace(linestr, "<", "");
linestr = replace(linestr, ">", "");
linestr = replace(linestr, "'", ""); std::vector<std::string> strNodes = split(linestr, " ");
static ImageLabel* mImageLabel = NULL;
switch (strNodes.size()) {
case 1:
if(strNodes[0] == "image"){
Imagelabels.push_back(*mImageLabel);
delete mImageLabel;
}
break;
case 2:
if(strNodes[0] == "image"){
mImageLabel = new ImageLabel();
mImageLabel->imagePath = ParentPath + split(strNodes[1], "=")[1];
// std::cout << mImageLabel->imagePath << std::endl;
// cv::Mat Image = cv::imread(mImageLabel->imagePath);
// cv::imshow("Image", Image);
// cv::waitKey(0);
}
break;
case 5:
if(strNodes[0] == "box"){
mImageLabel->faceBox[0] = atoi(split(strNodes[1], "=")[1].data());
mImageLabel->faceBox[1] = atoi(split(strNodes[2], "=")[1].data());
mImageLabel->faceBox[2] = atoi(split(strNodes[3], "=")[1].data());
mImageLabel->faceBox[3] = atoi(split(strNodes[4], "=")[1].data());
}
break;
case 4:
if(strNodes[0] == "part"){
int index = atoi(split(strNodes[1], "=")[1].data());
mImageLabel->landmarkPos[index] = atoi(split(strNodes[2], "=")[1].data());
mImageLabel->landmarkPos[index+LandmarkPointsNum] = atoi(split(strNodes[3], "=")[1].data());
}
break;
default:
break;
}
}
LabelsFile.close();
}
 

c++------------提取文件中的信息的更多相关文章

  1. python学习笔记——爬虫中提取网页中的信息

    1 数据类型 网页中的数据类型可分为结构化数据.半结构化数据.非结构化数据三种 1.1 结构化数据 常见的是MySQL,表现为二维形式的数据 1.2 半结构化数据 是结构化数据的一种形式,并不符合关系 ...

  2. python提取文件中的方法名称

    #提取文件中的方法名称 # -*- coding:utf-8 -*- def Query_Method(filepath): file = open(filepath,'r',encoding= 'U ...

  3. (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息

    众所周知,使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式. 本文介绍如何将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息.   比如 ...

  4. [翔哥高手无敌之路]0-002.如何提取apk中的信息?

    面对一款apk软件,我们如何去获取它的信息,如何获取它的版本号,包名,或者ID,用户权限,这些信息都隐藏在apk包中的AndroidManifest.xml文件中,解开它我们就能获取任何想要的信息.但 ...

  5. c# 如何使用DLL的config文件中的信息

    我知道用c#编写的exe程序可以读取config文件中的配置信息,比如Test.exe,可以在与Test.exe相同目录下放置一个config文件:Test.exe.config,用System.Co ...

  6. 向properties文件中写入信息(针对获取properties文件失败的总结)

    前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...

  7. SpringBoot使用注解(@value)读取properties(yml)文件中 配置信息

    为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性. 1. 两种使用方法1)@Value("#{co ...

  8. Python之通配符--提取文件中的内容并输出

    前言:我的学习进度其实没有那么快的,因为现在是网络工程师实习,只有晚上一点时间和周末有空,所以周一到周天的学习进度很慢,今天之所以突然跳到通配符是因为工作需要,大体讲一下我的工作需求:网络工程师就是写 ...

  9. 134、TensorFlow检查点checkpoint文件中的信息

    # 1.你想创建多少Saver对象就可以创建多少,如果你需要去保存和恢复不同的子图模型 # 同样的变量可以在不同的saver对象中被加载 # 只有在Saver.restore()方法被调用的时候才会对 ...

随机推荐

  1. php file_get_contents读取大容量文件方法

      当我们遇到文本文件体积很大时,比如超过几十M甚至几百M几G的大文件,用记事本或者其它编辑器打开往往不能成功,因为他们都需要把文件内容全部放到内存里面,这时就会发生内存溢出而打开错误,遇到这种情况我 ...

  2. Android Logcat信息级别解读

    Android Logcat信息级别解读 Logcat信息分为好几个级别,分别是:Assert(断言).Debug(调试).Error(错误).Info(信息).Verbose(详细).Warning ...

  3. mysql数据导入遇到的timestamp类型问题

    今天准备把最新的表导入自己以前的机子上做临时开发,在数据库导入的时候遇到一个问题:Incorrect table definition; there can be only one TIMESTAMP ...

  4. Mysql 导入CSV数据 语句 导入时出现乱码的解决方案

    1. 登陆mysql 2. use testdb 3. 执行导入语句 LOAD DATA LOCAL INFILE 'd://exportedtest2.csv' INTO TABLE usertab ...

  5. Android 4.1的新特性介绍

    原文:http://android.eoe.cn/topic/summary 果冻豆 - Android 4.1 通知系统 - Notifications 在Android 4.1系统上通知的功能大大 ...

  6. shell 知识点

        Q:1 Shell脚本是什么.它是必需的吗? 答:一个Shell脚本是一个文本文件,包含一个或多个命令.作为系统管理员,我们经常需要使用多个命令来完成一项任务,我们可以添加这些所有命令在一个文 ...

  7. ThinkPHP32 MODULE_ALLOW_LIST 存在的bug 不生效

    1)BUG: 假设存在Api Home Admin Member 模块.仅仅想让用户訪问 Api Home,不同意訪问Admin Member. 必须将Admin Member 写在 MODULE_D ...

  8. Golang之字符串格式化

    字符串格式化 // Go 之 字符串格式化 // // Copyright (c) 2015 - Batu // package main import ( "fmt" ) typ ...

  9. HTML5学习笔记(十四):变量作用域

    在JavaScript中,用var申明的变量实际上是有作用域的. 如果一个变量在函数体内部申明,则该变量的作用域为整个函数体,在函数体外不可引用该变量: function foo() { var x ...

  10. express中的会话存储方式

    会话存储需要用的express-session包 app.use(require('express-sessiion')()) express-session接收带有如下选项的配置对象: key;存放 ...