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()方法被调用的时候才会对 ...
随机推荐
- hive SQL 字母大小写转换
lower(string A) lcase(string A) 将文本字符串转换成字母全部小写形式 upper(string A) ucase(string A) 将文本字符串转换成字母全部大写形式
- 上传下载 demo
import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.springf ...
- Spring 注解@Component,@Service,@Controller,@Repository
Spring 注解@Component,@Service,@Controller,@RepositorySpring 2.5 中除了提供 @Component 注释外,还定义了几个拥有特殊语义的注释, ...
- C++的string类常见用法
C++的string常见用法,在网上看到一篇,但是不能在c++11编译器下运行,我修改了下,还没改完: #include<iostream> #include<string> ...
- sql server中调用c#写的dll里的方法
最近有一项目: 一超市管理系统单机版,运行在WIN2003+SQL2005上,每天超市关门都都会关电脑,现客户要新加功能,每天关门下班后回家可以上网查看超市管理系统的数据库里的相关数据,然后再做一些原 ...
- ASP.NET CORE做的网站运行在docker上(不用dockerfile文件部署)
按网上的做法用dockerfile文件是可以弄得出来的,http://www.docker.org.cn/article/119.html, 不过我想把网站文件放在外面硬盘目录,再映射进去,这样只要在 ...
- 菜鸟学SSH(八)——Hibernate对象的三种状态
前面写了几篇关于SSH的博客,但不是Struts就是Spring,Hibernate还从来没写过呢.说好是SSH的,怎么可以光写那两个,而不写Hibernate呢对吧.今天就先说说Hibernate对 ...
- Advanced DataStream API Low-latency Event Time Join
http://training.data-artisans.com/exercises/eventTimeJoin.html
- 每天一个linux命令(7):whereis 命令
whereis命令只能用于程序名的搜索,而且只搜索二进制文件(参数-b).man说明文件(参数-m)和源代码文件(参数-s).如果省略参数,则返回所有信息. 和find相比,whereis查找的速度非 ...
- TCP三次握手,四次分手
1.什么是HTTP连接 http是建立在TCP协议之上的一种应用. 最显著的特点是每次请求,都需要服务器响应,请求结束后,会主动释放连接. 1)在HTTP 1.0中,客户端的每次请求都要建立一次单独的 ...