C++ method to extract information from a file

  • 2020-05-26 09:48:15
  • OfStack

For complex files, in order to get the information in the file, you need some special functions, such as getline(), replace (), atoi,atof, and so on

Example 1, read the data in the following file and save it into a class.

First of all, the definition of the class is as follows, which seems to be struct. However, according to the processing of struct, there is something wrong with this code. I don't know what the problem is.


struct ImageLabel{
  std::string imagePath;//save Image pathname 
  int faceBox[4];// The outermost envelope rectangle of the input point set rect4 A parameter 
  int landmarkPos[2*LandmarkPointsNum];// The input point set 

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);
  }
};

Then read in the following 1 series of files. Each file has the same data saving format, so 1 vector is defined to store the corresponding classes for each file in sequence

The file is as follows: *.pts file


version: 1
n_points: 68
{
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
}

All pts file names are saved in an TXT file, and the corresponding image names are also saved in an TXT file. We need to save the image file path and file name into the class. For the convenience of processing, we use the replace function to directly replace the last three bits of the pts file name to get a new file name.


void readpic(std::vector<ImageLabel> &Imagelabels){
   cout<<"test readpic"<<endl;

   string filePath = "/Users/anitafang/Downloads/Datasets/300W/300w/01_Indoor/";
  // Open the png The image name is stored TXT file 
  ifstream pngfile;
  pngfile.open("/Users/anitafang/Downloads/Datasets/300W/300w/01_Indoor/pnglist.txt");
    if(!pngfile.is_open()){
    cout<<" Cannot open file !"<<endl;
  }
  //ImageLabel* mImageLabel = NULL;// The class that holds the image information 
  string line;// Read each of the files 1 line 
  while(getline(pngfile,line)){
    //mImageLabel = new ImageLabel();
    ImageLabel mImageLabel;
    //mImageLabel->imagePath=filePath+line;// Save the file full path at  mImageLabel->imagePath
    mImageLabel.imagePath=filePath+line;// Save the file full path at  mImageLabel->imagePath
    cout<<line<<endl;
    // get pts The file path 
    string ress="pts";
    string ptss=filePath+line.replace(11,3,ress);
    cout<<line.replace(11,3,ress)<<endl;
    cout<<ptss<<endl;
    // read pts The data in the file 
    std::ifstream LabelsFile(ptss, std::ios::in);
    if(!LabelsFile.is_open())
      return;
   // Began to read pts The file content 
    int index=0;
    vector<Point2f> vp2f;
    char line11[1024]={0};// Store each row of data 
    // Read the file line by line string linestr , and then judge the contents of the row 
    while(LabelsFile.getline(line11, sizeof(line11))){
      // From the first 4 The line begins to write the data in landmark In the array 
      if((index>=3)&&(index<139)){
        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-3] =atof(x.c_str());
        mImageLabel.landmarkPos[index+LandmarkPointsNum-3] =atof(y.c_str());
        
        vp2f.push_back(Point2f(mImageLabel.landmarkPos[index-3],mImageLabel.landmarkPos[index+LandmarkPointsNum-3]));
        cout<<"x:"<<mImageLabel.landmarkPos[index-3]<<" y:"<<mImageLabel.landmarkPos[index+LandmarkPointsNum-3]<<endl;
        
      }
      index++;
      
    }
    
  
  Rect rect = boundingRect(vp2f);

  // Depending on the rectangle you get   The input to the facebox In the 
  mImageLabel.faceBox[0] = rect.x;
  mImageLabel.faceBox[1] = rect.y;
  mImageLabel.faceBox[2] = rect.width;
  mImageLabel.faceBox[3] = rect.height;
  
  cout<<"facebox"<<mImageLabel.faceBox[0]<<mImageLabel.faceBox[1]<<mImageLabel.faceBox[2]<<mImageLabel.faceBox[3]<<endl;
    
     // close file
  LabelsFile.close();
    //free the object
  Imagelabels.push_back(mImageLabel);

  //mImageLabel == NULL;
    
  }
}

Among them, since the data read by GetLine is of type string, it is necessary to convert the data into int. You can see that all the data in the pts file are of type float, so you should first convert string into float, using the atof function.

In addition, if you want to treat Imagelabels as a class, you only need to, at the beginning, ImageLabel mImageLabel; push_back, delete is not required. All references become: mImageLabel.faceBox, mImageLabel- cannot be used > landmarkPos.

Another example of upgrading is very complicated to deal with. Here, ImageLabel is treated as one struct, which requires new and also requires delete. It is also used when quoting - > That's something to pay attention to.

The file is: labels_ibug_300W.xml

Let's look at part 1 a little bit and see what it's made of.


<?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 68 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 1 being the first index to 0 being the first index. So the coordinates in this
file are in the normal C 0-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='206' left='469' width='216' height='216'>
   <part name='00' x='482' y='267'/>
   <part name='01' x='483' y='298'/>
   <part name='02' x='487' y='329'/>
   <part name='03' x='491' y='358'/>
   <part name='04' x='503' y='386'/>
   <part name='05' x='523' y='409'/>
   <part name='06' x='543' y='428'/>
   <part name='07' x='565' y='442'/>
   <part name='08' x='591' y='447'/>
   <part name='09' x='620' y='443'/>
   <part name='10' x='647' y='429'/>
   <part name='11' x='671' y='409'/>
   <part name='12' x='688' y='385'/>
   <part name='13' x='699' y='359'/>
   <part name='14' x='704' y='332'/>
   <part name='15' x='707' y='305'/>
   <part name='16' x='708' y='277'/>
   <part name='17' x='502' y='250'/>
   <part name='18' x='518' y='237'/>
   <part name='19' x='537' y='234'/>
   <part name='20' x='557' y='236'/>
   <part name='21' x='575' y='243'/>
   <part name='22' x='619' y='243'/>
   <part name='23' x='639' y='237'/>
   <part name='24' x='659' y='234'/>
   <part name='25' x='679' y='238'/>
   <part name='26' x='693' y='250'/>
   <part name='27' x='596' y='268'/>
   <part name='28' x='595' y='287'/>
   <part name='29' x='594' y='305'/>
   <part name='30' x='593' y='324'/>
   <part name='31' x='570' y='336'/>
   <part name='32' x='581' y='338'/>
   <part name='33' x='593' y='342'/>
   <part name='34' x='605' y='338'/>
   <part name='35' x='615' y='336'/>
   <part name='36' x='523' y='272'/>
   <part name='37' x='536' y='263'/>
   <part name='38' x='551' y='263'/>
   <part name='39' x='564' y='277'/>
   <part name='40' x='550' y='277'/>
   <part name='41' x='535' y='276'/>
   <part name='42' x='626' y='279'/>
   <part name='43' x='642' y='265'/>
   <part name='44' x='657' y='267'/>
   <part name='45' x='670' y='276'/>
   <part name='46' x='658' y='280'/>
   <part name='47' x='642' y='279'/>
   <part name='48' x='544' y='364'/>
   <part name='49' x='565' y='360'/>
   <part name='50' x='580' y='357'/>
   <part name='51' x='591' y='360'/>
   <part name='52' x='603' y='358'/>
   <part name='53' x='621' y='361'/>
   <part name='54' x='641' y='366'/>
   <part name='55' x='621' y='382'/>
   <part name='56' x='603' y='385'/>
   <part name='57' x='590' y='384'/>
   <part name='58' x='579' y='383'/>
   <part name='59' x='563' y='378'/>
   <part name='60' x='552' y='366'/>
   <part name='61' x='580' y='370'/>
   <part name='62' x='591' y='370'/>
   <part name='63' x='603' y='371'/>
   <part name='64' x='634' y='369'/>
   <part name='65' x='603' y='371'/>
   <part name='66' x='591' y='370'/>
   <part name='67' x='580' y='370'/>
  </box>
 </image>
 <image file='afw/111076519_1.jpg'>
  <box top='724' left='1122' width='150' height='150'>
   <part name='00' x='1126' y='765'/>
   <part name='01' x='1123' y='784'/>
   <part name='02' x='1123' y='804'/>
   <part name='03' x='1124' y='822'/>
   <part name='04' x='1131' y='839'/>
   <part name='05' x='1142' y='853'/>
   <part name='06' x='1157' y='865'/>
   <part name='07' x='1172' y='874'/>
   <part name='08' x='1190' y='878'/>
   <part name='09' x='1208' y='878'/>
   <part name='10' x='1225' y='873'/>
   <part name='11' x='1238' y='862'/>
   <part name='12' x='1249' y='846'/>
   <part name='13' x='1256' y='829'/>
   <part name='14' x='1261' y='810'/>
   <part name='15' x='1263' y='792'/>
   <part name='16' x='1264' y='774'/>
   <part name='17' x='1148' y='749'/>
   <part name='18' x='1160' y='745'/>
   <part name='19' x='1171' y='744'/>
   <part name='20' x='1183' y='748'/>
   <part name='21' x='1194' y='753'/>
   <part name='22' x='1227' y='756'/>
   <part name='23' x='1236' y='753'/>
   <part name='24' x='1244' y='753'/>
   <part name='25' x='1251' y='754'/>
   <part name='26' x='1256' y='759'/>
   <part name='27' x='1210' y='769'/>
   <part name='28' x='1210' y='779'/>
   <part name='29' x='1210' y='790'/>
   <part name='30' x='1209' y='800'/>
   <part name='31' x='1187' y='808'/>
   <part name='32' x='1196' y='811'/>
   <part name='33' x='1205' y='814'/>
   <part name='34' x='1212' y='813'/>
   <part name='35' x='1218' y='812'/>
   <part name='36' x='1159' y='766'/>
   <part name='37' x='1167' y='763'/>
   <part name='38' x='1176' y='764'/>
   <part name='39' x='1183' y='770'/>
   <part name='40' x='1175' y='770'/>
   <part name='41' x='1166' y='769'/>
   <part name='42' x='1225' y='776'/>
   <part name='43' x='1235' y='773'/>
   <part name='44' x='1243' y='774'/>
   <part name='45' x='1248' y='777'/>
   <part name='46' x='1241' y='779'/>
   <part name='47' x='1233' y='779'/>
   <part name='48' x='1160' y='825'/>
   <part name='49' x='1176' y='820'/>
   <part name='50' x='1190' y='822'/>
   <part name='51' x='1199' y='824'/>
   <part name='52' x='1209' y='825'/>
   <part name='53' x='1221' y='827'/>
   <part name='54' x='1229' y='833'/>
   <part name='55' x='1218' y='847'/>
   <part name='56' x='1205' y='852'/>
   <part name='57' x='1194' y='851'/>
   <part name='58' x='1184' y='849'/>
   <part name='59' x='1171' y='840'/>
   <part name='60' x='1165' y='827'/>
   <part name='61' x='1189' y='828'/>
   <part name='62' x='1199' y='830'/>
   <part name='63' x='1208' y='831'/>
   <part name='64' x='1225' y='834'/>
   <part name='65' x='1206' y='844'/>
   <part name='66' x='1196' y='844'/>
   <part name='67' x='1186' y='841'/>
  </box>
 </image>
 <image file='afw/111076519_2.jpg'>
  <box top='590' left='1028' width='180' height='180'>
   <part name='00' x='1050' y='620'/>
   <part name='01' x='1046' y='641'/>
   <part name='02' x='1040' y='663'/>

We can find some rules to get the data we need according to these rules and directly go to the code:


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();
}

Related articles: