I have created an XML file, but when I try to read it in I get an error about a duplicate key. When I opened the XML file I noticed that nothing was nested and everything was treated as it's own individual node. I was wondering if there was a way to properly create the XML file and be able to parse through it as well. Below I have included my code for writing an XML file out, my XML output, and what I would like it to be.
XML Code:
// Open up the file so that we can write to it
cv::FileStorage fs;
fs.open(szFileName, cv::FileStorage::WRITE);
// Check if we actually created the file
if(fs.isOpened())
{
fs << "Total_Images" << (int)vImages.size();
//cv::write(fs, "Total Images", (int)vImages.size());
for(int i = 0; i < (int)vImages.size(); i++)
{
char szSize[8];
string szNode;
szNode = "image_";
itoa(i, szSize, 10);
szNode += szSize;
fs << "Node" << szNode;
fs << "Width" << vImages[i]->GetWidth();
fs << "Height" << vImages[i]->GetHeight();
fs << "Keypoint_Size" << (int)vImages[i]->GetKeyPoints().size();
for(int j = 0; j < vImages[i]->GetKeyPoints().size(); j++)
{
char szSize[256];
fs << "Angle" << vImages[i]->GetKeyPoints()[j].angle;
fs << "Class" << vImages[i]->GetKeyPoints()[j].class_id;
fs << "Octave" << vImages[i]->GetKeyPoints()[j].octave;
fs << "Pt_X" << vImages[i]->GetKeyPoints()[j].pt.x;
fs << "Pt_Y" << vImages[i]->GetKeyPoints()[j].pt.y;
fs << "Response" << vImages[i]->GetKeyPoints()[j].response;
fs << "Size" << vImages[i]->GetKeyPoints()[j].size;
}
fs << "Descriptors" << vImages[i]->GetDescriptors();
}
fs.release();
}
XML Output:
<?xml version="1.0"?>
<opencv_storage>
<Total_Images>1</Total_Images>
<Node>image_0</Node>
<Width>500</Width>
<Height>266</Height>
<Keypoint_Size>268</Keypoint_Size>
<Angle>8.7332672119140625e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7401049804687500e+002</Pt_X>
<Pt_Y>6.5084960937500000e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1386331558227539e+001</Size>
<Angle>9.3859291076660156e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7674386596679687e+002</Pt_X>
<Pt_Y>6.4983604431152344e+001</Pt_Y>
<Response>0.</Response>
<Size>1.2262072563171387e+001</Size>
<Angle>9.8604904174804688e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.1859683990478516e+002</Pt_X>
<Pt_Y>6.6855834960937500e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1112688064575195e+001</Size>
...
Expected Output
<?xml version="1.0"?>
<opencv_storage>
<Total_Images>1</Total_Images>
<Node>image_0
<Width>500</Width>
<Height>266</Height>
<Keypoint_Size>268</Keypoint_Size>
<KeyPoint>
<Angle>8.7332672119140625e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7401049804687500e+002</Pt_X>
<Pt_Y>6.5084960937500000e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1386331558227539e+001</Size>
</KeyPoint>
<KeyPoint>
<Angle>9.3859291076660156e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7674386596679687e+002</Pt_X>
<Pt_Y>6.4983604431152344e+001</Pt_Y>
<Response>0.</Response>
<Size>1.2262072563171387e+001</Size>
</KeyPoint>
<KeyPoint>
<Angle>9.8604904174804688e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.1859683990478516e+002</Pt_X>
<Pt_Y>6.6855834960937500e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1112688064575195e+001</Size>
</KeyPoint>
...
</Node>
Thanks
You can embedd a node inside another node.
Do it like this (using "{" and "}" ):
FileStorage fs; // Open it and check that it is opened;
fs << "SimpleData" << 1;
fs << "Structure << "{";
fs << "firstField" << 1;
fs << "secondField" << 2;
fs << "}"; // End of structure node
fs << "SimpleData2" << 2;
Actually we can see the "Duplicated key" error in opencv/modules/core/src/persistence.cpp line 711
The key seems have sth to do with the node name in your xml file: once you have many nodes with the same name like below,it will has duplicated key.And even you haven't read but just open this file,it will occur.So, just modify your node names,how about add an index "node_1"?
<opencv_storage>
<mynodes>
<node>1</node>
<node>2</node>
<node>3</node>
<node>4</node>
...
<node>10</node>
</mynodes>
You can also try to use JSON which is more fash and simple to interpret
File storage is not designed for this type of use, however you can use a XML-parser/writter, the easiest, fastest and most fashion one is Xerces-C. You can download it here
Actually, the filestorage object is made to store simple datas like calibration from one runtime to the other... From what i know there is no way to structure the nodes between each others...
Julien,
EDIT :
READ DATA FROM A MAT
for(int i=0;i<img.rows;i++) {
unsigned char* row = img.data + i * img.step;
for(int j=0;j<img.cols;j++) {
unsigned char* channB = (row + 3*j);
unsigned char* channG = (row + 3*j + 1);
unsigned char* channR = (row + 3*j + 2);
}
}
WRITE WITH XERCES-C
The "DOMPrint" example shows how to write in a document with a serializer...
However, let me tell you that i think that OpenCV is enough for your example, is it a big problem that yuor datas are in nodes next to each other ? I'll try to find an easier way, let me think of it, i'll post back here (still, you sould try Xerces-C it's great and usefull)
Related
I'll cut the the short of it. I'm attempting to understand the filesystem library but there's very little information I've been able to find. I managed to get it to compile and understand the filesystem::path type variable really well but don't seem to understand how to get filesystem::directory_iterator to work properly. I'm not sure if I'm using it for a purpose it wasn't design for. So here is what I'm attempting to do:
I wanted to create a program that opens every text file within a specified folder. For this I need to obtain the folder name and path but I want the program to be able to obtain this information on itself and dynamically so if I add or remove textFiles it'll have the logic to function.
I'm able to create a directory_iterator variable that it manages to hold the first file with me just giving it the directory like this:
const char address[]{ "C:\\Users\\c-jr8\\ProgramBranch\\PersonalPlatform\\log extruder\\logs" };
fs::directory_iterator myIterator(address);
When testing the code in the folder I have four textFiles called "attempt 1" to "attempt 4". When reading the information on:
https://learn.microsoft.com/en-us/cpp/standard-library/directory-iterator-class?view=vs-2019#op_star
It mentions two functions to move the path object within the iterator to the next file. The first is increment(), which is the intendent method for iterating through the files, and operation++().
Now increment() hasn't been able to work for me cause it takes in a erro_code type variable and I haven't been able to find much information about how to implement this with the filesystem_errorcode variable or however it's meant to be used. The operation++() works beautifully and provides me with the path to every file but I was having issues with managing the code to detect when the operate++() functions leads to no other files. Once it iterates through every file it sorts of crashes when it keeps moving to the next. Here's that piece of code:
string tempString;
for (int i = 0; i < 5; i++) { //Here the limiting is 5 so it'll iterate onces more than the numbers of files unpurpose to see how it responses.
tempString = myIterator.operator*().path().generic_string();
ifstream tempFile(tempString);
if (!tempFile.is_open()) {
cout << "Looking at file: " << i + 1 << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
//do things with file...
}
tempFile.close();
myIterator.operator++();
}
What I want if to find a way to stop the for loop once it the iterator goes off the final file.
whichever information about how the filesystem library works it would be very much appreciated.
std::directory_iterator is a classic iterator that allows for iterating over ranges, and those are usually designated by a pair of iterators, one indicating the beginning of a sequence and another representing the past-the-end iterator.
Some iterator types, like those providing access to streams, don't have an actual end location in memory. A similar situation applies to a directory iterator. In such a case, the idiomatic approach is to use a default-constructed iterator object that will serve as an end indicator.
Having said that, you could rewrite your loop to:
for (fs::directory_iterator myIterator(address), end{}; myIterator != end; ++myIterator) {
Alternatively, you can utilize a range-based for loop:
for (auto& p : fs::directory_iterator(address)) {
tempString = p.path().generic_string();
// ...
Also, note that iterators' interface is supposed to look/behave like a pointer, hence it uses operator overloading to allow for concise syntax. So instead of:
myIterator.operator++();
you should be using:
++myIterator;
Similarly, instead of:
myIterator.operator*().path().generic_string();
juse use:
(*myIterator).path().generic_string();
or:
myIterator->path().generic_string();
You should compare myIterator with a default constructed directory_iterator to check if the last file has been processed. You can also use a much simpler form to access the operators (shown in the code below):
string tempString;
// loop until myIterator == fs::directory_iterator{}
for(size_t i = 1; myIterator != fs::directory_iterator{}; ++i) {
// access path() through the iterators operator->
tempString = myIterator->path().generic_string();
ifstream tempFile(tempString);
if(!tempFile.is_open()) {
cout << "Looking at file: " << i << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
std::cout << tempString << " opened\n";
}
// tempFile.close(); // closes automatically when it goes out of scope
// simpler form to use myIterator.operator++():
++myIterator;
}
An even simpler approach would be to use a range-based for-loop:
for(const fs::directory_entry& dirent : fs::directory_iterator(address)) {
const fs::path& path = dirent.path();
ifstream tempFile(path);
if(!tempFile) {
cout << "Looking at file: " << path << "; failed to open.\n\n";
cin.get();
return 0;
}
std::cout << path << " opened\n";
}
This is part of my code, of course I have #include fstream>, and to basically run you throught what the code should do at this point is open a file I have name "flights.csv" and in a function called readFlights count how many flights are in the file and then return the size, but when ran it returns a size of 0 when there are 5 in the file, and gives me my error file open message.
int main()
{
ifstream in("flights.csv");
if(!in.is_open()) cout << "ERROR: File open" << endl;
Flights flightsList[100];
const int SIZE = readFlights(in, flightsList);
cout << "Size: " << SIZE <<endl;
here is the function for counting the size and putting it into an array.
int readFlights(ifstream& in, Flights flightsList[100]){
//Reads the file flights.csv and returns a size used for array.
string pdStore, dmStore;
int size= 0;
while(in.good()){
getline(in, flightsList[size].fromCity,',');
getline(in, flightsList[size].toCity,',');
getline(in, pdStore,',');
flightsList[size].priceDollars = stoi(pdStore);
getline(in, dmStore,'\n');
flightsList[size].distanceMiles = stoi(dmStore);
size++;
}
return size;
}
I thought it was interesting but I don't know if this is necessary but inside code blocks the .csv part of the file in ifstream in("flights.csv"); was underlined with the red jagged lines.
I found the answer, I had no clue I had to include the file into the project folder itself...
So, I'm trying to create a text file that can be written to and used to seed a random number generator, but I need to be able to write to and call on the file in all of my functions. The file has to be named with the user's first and last name so I found some code that would create a text file and rename it, I'm just having trouble calling the file in my other functions. I included the beginning part of one of my other functions after the code for the text file creation. I'm going to need to send a time stamp to the file and use that time to seed my RNG.
int user_file_name()
{
string tstamp = get_timestamp();
//Creating input/output file using user's name
ofstream user_file;
string filename;
cout << "What is your first and last name?\n" << endl;
getline(cin, filename);
filename += ".txt";
cout << "Thank you, " << filename << "." << endl << endl;
user_file.open(filename.c_str());
user_file << tstamp;
user_file.close();
return 0;
}
int addition()
{
char DIFFICULTY;
difficulty_menu();
cin >> DIFFICULTY;
get_timestamp();
string tstamp = get_timestamp();
Why not just have user_file_name return the string filename instead of just an int? You can then store that filename somewhere so that other areas of your program can access it.
When the user types in a word the program is searching for a match in an array, if there is a match the program prints out the string from the array.The code below does just that. My goal is to make it so when a word matches with a a word in the array the program should print an info not only the word. I thought I can fill the array with functions, but its not working. Is it even possible ?
I am using league of legend hero names because they are a lot and I know them and it doesent take me a lot of time to think of names that way :D
The idea is if the user types in voly, the program finds voly in the array and prints out(for example) his starting life, armor, mr and so on.
I tryed a lot of thing with functions but I cant make it work.
#include <iostream>
#include <string>
using namespace std;
string voly(string holder,string heroName);
int main(){
const int numH = 10;
string holder;
string heroName;
string heroList[numH] = {"voly", "teemo", "vladimir", "morgana", "jax", "ekko", "anivia", "nunu", "ashe", "tresh" };
cout << "Enter hero name.\n" << endl;
cin >> heroName;
for (int i = 0; i < numH; i++){
holder = heroList[i];
if (heroName == holder){
cout << holder << endl;
}
}
system("PAUSE");
return 0;
}
string voly(string holder, string heroName) {
cout << "Voly is the best" << endl;
}
Try to learn about structs. You can utilize them to encapsulate all hero information in a hero struct, as follows. This serves as kind of a prototype for each hero:
struct hero {
string name;
int hp;
int mana;
float mreg;
...
void printMe() { cout << 'hp: ' << hp << endl << 'mana: ' << mana << endl << ...; }
}
Using the printMe() function of that specific hero object, you can print its values.
Then, for each hero, create a struct object and add them to the array.
hero* pointer = new hero[numH];
pointer[0] = new hero {
name: "voly",
hp: 150
};
pointer[1] = new hero {
...
};
(Try considering some import function via a .CSV file or so.)
By using your for loop, compare the name:
for (int i = 0; i < numH; i++){
if (heroName == pointer[i].name){
pointer[i]->printMe();
}
}
Try using Google to find tutorials on that. Unfortunately, I'm not quite sure about the syntax in C++, maybe someone can help me out.
Good Luck!
I have a problem with the SQLite3 C API. I'm trying to read some information from the places.sqlite database that Firefox uses to store many things. What I'm interested in is to retrieve the last visited website and the window title associated.
I wrote this piece of code:
query = "SELECT `url`, `title` FROM `moz_places` WHERE `id`=(SELECT `place_id` FROM `moz_historyvisits` ORDER BY `id` DESC LIMIT 1)";
stmt = NULL;
if (sqlite3_prepare_v2(db, query.c_str(), strlen(query.c_str()) + 1, &stmt, NULL) != SQLITE_OK)
cerr << sqlite3_errmsg(db) << endl;
else
{
while ((ret = sqlite3_step(stmt)) == SQLITE_ROW)
cout << sqlite3_column_text(stmt, 0) << endl << sqlite3_column_text(stmt, 1) << endl;
if (ret != SQLITE_DONE)
cerr << sqlite3_errmsg(db) << endl;
}
When there are no special characters it works fine, but if the window title contains "é", "è" or "•" for example, I get those wonderful characters "é", "è" or "•".
I did some research and found that some people actually got non-UTF8 encoded databases. So I checked the places.sqlite database encoding with both SQLite Manager and the "PRAGMA encoding" request, both say that it is UTF-8 encoded.
Then I created my own database, encoded in UTF-8, entered some special characters in a table and it didn't work either. So I thought that maybe QtCreator (which I'm using) can't show special characters, I tried to get the foreground window title with GetForegroundWindow() and print it in QtCreator application output. It successfully showed the special characters of some window titles.
What am I missing here ?
Thank you.
Weel, seems like I managed to fix that problem by using this function I wrote:
wchar_t *char_to_wchar(const char *str)
{
wchar_t *wbuf;
int wsz;
if (!str)
return (NULL);
wbuf = NULL;
wsz = MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, 0);
if (wsz)
wbuf = (wchar_t*)malloc(wsz * sizeof(*wbuf));
else
cerr << "MultiByteToWideChar fail getting required size: " << GetLastError() << endl;
if (wbuf)
{
if (!MultiByteToWideChar(CP_UTF8, 0, str, -1, wbuf, wsz))
cerr << "MultiByteToWideChar fail: " << GetLastError() << endl;
}
return (wbuf);
}
Still, I found it weird that with a function like GetWindowText() I manage to correctly print "é" and other caracters from a simple char *, but here I must use a wchar_t *...
Well, it works at least, let's hope it was the best solution :)