c++------------提取文件中的信息
对于文件比较复杂的时候,为了获取文件中的信息,需要一些比较特殊的函数,比如,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++------------提取文件中的信息的更多相关文章
- python学习笔记——爬虫中提取网页中的信息
1 数据类型 网页中的数据类型可分为结构化数据.半结构化数据.非结构化数据三种 1.1 结构化数据 常见的是MySQL,表现为二维形式的数据 1.2 半结构化数据 是结构化数据的一种形式,并不符合关系 ...
- python提取文件中的方法名称
#提取文件中的方法名称 # -*- coding:utf-8 -*- def Query_Method(filepath): file = open(filepath,'r',encoding= 'U ...
- (转)linux sudo 重定向,实现只有系统管理员才有权限操作的文件中写入信息
众所周知,使用 echo 并配合命令重定向是实现向文件中写入信息的快捷方式. 本文介绍如何将 echo 命令与 sudo 命令配合使用,实现向那些只有系统管理员才有权限操作的文件中写入信息. 比如 ...
- [翔哥高手无敌之路]0-002.如何提取apk中的信息?
面对一款apk软件,我们如何去获取它的信息,如何获取它的版本号,包名,或者ID,用户权限,这些信息都隐藏在apk包中的AndroidManifest.xml文件中,解开它我们就能获取任何想要的信息.但 ...
- c# 如何使用DLL的config文件中的信息
我知道用c#编写的exe程序可以读取config文件中的配置信息,比如Test.exe,可以在与Test.exe相同目录下放置一个config文件:Test.exe.config,用System.Co ...
- 向properties文件中写入信息(针对获取properties文件失败的总结)
前段时间项目需要将某个属性动态的写入项目发布路径下的properties文件中;但是实际发布时发现找不到maven项目resource路径下的project.properties文件,调试多次代码如下 ...
- SpringBoot使用注解(@value)读取properties(yml)文件中 配置信息
为了简化读取properties文件中的配置值,spring支持@value注解的方式来获取,这种方式大大简化了项目配置,提高业务中的灵活性. 1. 两种使用方法1)@Value("#{co ...
- Python之通配符--提取文件中的内容并输出
前言:我的学习进度其实没有那么快的,因为现在是网络工程师实习,只有晚上一点时间和周末有空,所以周一到周天的学习进度很慢,今天之所以突然跳到通配符是因为工作需要,大体讲一下我的工作需求:网络工程师就是写 ...
- 134、TensorFlow检查点checkpoint文件中的信息
# 1.你想创建多少Saver对象就可以创建多少,如果你需要去保存和恢复不同的子图模型 # 同样的变量可以在不同的saver对象中被加载 # 只有在Saver.restore()方法被调用的时候才会对 ...
随机推荐
- PostgreSQL创建数据库用户
注:版本不同,命令的执行路径可能有些不同. 一. 创建数据库用户freeswitch sudo -u postgres /usr/pgsql-9.5/bin/createuser -s -e free ...
- c#asp.net url 传递中文参数要使用 System.Web.HttpUtility.UrlEncode 而不能使用Server.UrlEncode
最近网站里的参数包括中文的例如: http://www.taiba/Tag%b0%ae%c7%e9.html 已开始使用 Server.UrlEncode来做的,但发现,有一些中文在url重写的是说找 ...
- python opencv 按一定间隔截取视频帧
前言关于opencvOpenCV 是 Intel 开源计算机视觉库 (Computer Version) .它由一系列 C 函数和少量 C++ 类构成,实现了图像处理和计算机视觉方面的很多通用算法. ...
- mysql数据库1129错误
错误:Host is blocked because of many connection errors; unblock with 'mysqladmin flush-hosts' 原因: 同一个i ...
- Android百日程序 开篇章:Intent打开网页
学习一下人家100日写100个网页的做法,我也用100日写100个完整的Android程序. 这些程序的最基本要求: 1 完整性-每一个程序都必须是独立可执行的 2 不反复性-所用的重点知识点都不一样 ...
- RecyclerView中实现headerView,footerView功能
之前用com.bartoszlipinski.recyclerviewheader.RecyclerViewHeader 不过局限性有点大. (com.bartoszlipinski.recycler ...
- 通过ip查找能应机器的MAC
例如:10.100.0.61 这些都是基于linux系统: 首先:ping 一下这个ip 然后arp 10.100.0.61就可以找出主机的MAC地址
- 菜鸟调错(二)——EJB3.0部署消息驱动Bean抛javax.naming.NameNotFoundException异常
在部署EJB的消息驱动Bean时遇到了如下的错误: ERROR [org.jboss.resource.adapter.jms.inflow.JmsActivation] (WorkManager(2 ...
- vim学习笔记(9):vim显示文件名
如何让vim在编辑模式下显示文件名 在根目录下:~/.vimrc 中追加一行: set laststatus=2 shell 下执行命令: echo "set laststatus=2&qu ...
- iOS开发-应用管理
// // ViewController.m // 21-应用管理-1 // // Created by hongqiangli on 2017/8/2. // Copyright © 201 ...