CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2017
    Posts
    1

    Edit txt file to correct object detection results

    Hello all,

    I have evaluated an object detection method and I wrote my detection results to a txt file.
    The txt file is written in the following format:

    0001.jpg
    8
    716 265 87 87
    587 298 95 95
    443 320 94 94
    327 358 86 85
    214 368 100 99
    443 324 91 91
    590 305 91 91
    324 359 92 93
    721 260 91 91
    207 372 107 106
    0002.jpg
    8
    589 302 89 88
    715 266 85 85
    0003.jpg
    8
    588 303 89 89
    446 325 87 87
    327 360 88 89


    which means that in picture 0001.jpg, I have 8 detection bounding boxes and below are their coordinates.
    The problem is I made a mistake and instead of writing the correct number of bounding boxes I wrote 8 under every image name. How can I correct my txt file and write the proper
    number of bounding boxes by counting the number of boxes under each image name?

    For example I want to produce a new txt that will be in the following format:

    0001.jpg
    10
    716 265 87 87
    587 298 95 95
    443 320 94 94
    327 358 86 85
    214 368 100 99
    443 324 91 91
    590 305 91 91
    324 359 92 93
    721 260 91 91
    207 372 107 106
    0002.jpg
    2
    589 302 89 88
    715 266 85 85
    0003.jpg
    3
    588 303 89 89
    446 325 87 87
    327 360 88 89

    This is a beginners question but I am having real trouble fixing this and I cant afford to correct the original bug and run the detection experiment again. Thank you in advance any help will be highly appreciated.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,825

    Re: Edit txt file to correct object detection results

    Assuming that these files are indeed text files with each line terminated with a \n and that the first line is the number of remaining lines in the file, then to alter this number the file will have to be read, the new number calculated and then the file re-written with the correct number. As the new number may not have the same number of digits as the existing, it simply can't be overwritten. So consider
    Code:
    #include <iostream>
    #include <fstream>
    #include <vector>
    #include <string>
    using namespace std;
    
    int main(int argc, char* argv[])
    {
    	if (argc != 2) {
    		cout << "usage: " << argv[0] << " <filename>" << endl;
    		return 1;
    	}
    
    	ifstream ifs(argv[1]);
    
    	if (!ifs.is_open()) {
    		cout << "Cannot open file " << argv[1] << endl;
    		return 2;
    	}
    
    	string line;
    	vector<string> vs;
    
    	while (getline(ifs, line))
    		vs.push_back(line);
    
    	ifs.close();
    	vs.erase(vs.begin());
    
    	ofstream ofs(argv[1]);
    
    	if (!ofs.is_open()) {
    		cout << "Cannot open file " << argv[1] << endl;
    		return 2;
    	}
    
    	ofs << vs.size() << endl;
    
    	for (const auto& v : vs)
    		ofs << v << endl;
    
    	ofs.close();
    }
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured