简体   繁体   中英

OpenCV FileStorage - Error: Parsing error (icvYMLParseValue) Missing , between the elements

I am having a weird problem while writing and reading a .yml file with FileStorage in OpenCV.

When I write everything goes fine. I do this with the following routine:

if (saveLoad == MODEL::SAVE) {

            cv::FileStorage fs(detections_filename, cv::FileStorage::WRITE);
            fs << "detections_structure" << "[";
            for (auto & class_id : dpm_detections) {
                fs << "{:" << "class_id" << class_id.first << "second" << "[";
                for (auto & img_path : class_id.second) {
                    fs << "{:" << "img_path" << img_path.first << "second" << "["; 
                    for (auto & detection : img_path.second)
                        fs << "{:" << "classID" << detection.classID << "rect" << detection.rect << "score" << detection.score << "}";
                    fs << "]" << "}";
                }
                fs << "]" << "}";
            }
            fs << "]";
            fs.release();
        }

The structure I am storing is a std::map<int, std::map<std::string, std::vector<cv::dpm::DPMDetector::ObjectDetection>>> . The problem appears while I try to read back the written .yml file.

Here's my (debug) code:

if (saveLoad == MODEL::LOAD) {

        cv::FileStorage fs(detections_filename, cv::FileStorage::READ);
        std::cout << "Here" << std::endl;

        cv::FileNode dpm_class = fs["detections_structure"];
        std::cout << "Here" << std::endl;

        cv::FileNodeIterator itclass = dpm_class.begin(), itclass_end = dpm_class.end();
        std::cout << "Here" << std::endl;

        for (; itclass != itclass_end; ++itclass) {
                std::cout << "ID :" << (int)(*itclass)["class_id"] << std::endl;
    }

    fs.release();
    }

However, I dont get even the first "Here" printed out. Instead I get this error: OpenCV(4.0.0-pre) Error: Parsing error (icvYMLParseValue) in DPMDetections_test2.yml(6): Missing , between the elements, file c:\\opencv_lastversion\\source\\opencv-master\\modules\\core\\src\\persistence_yml.cpp, line 431

I have been looking around for a solution but unfortunatelly I have found nothing so far. Does anybody have any idea of how to solve this?

Here is the .ylm file I want to read from:

%YAML:1.0
---
detections_structure:
   - { class_id:1, second:
       - {
           img_path:"C:/Users/David Romero/Documents/MasterThesis/Object Detection/Dataset/voc2006_test/VOCdevkit/VOC2006/PNGImages/000001.png",
           second:
           - { classID:0, rect:[ 0, 18, 82, 46 ],
               score:-7.9933482408523560e-01 }
           - { classID:0, rect:[ -83, 139, 250, 139 ],
               score:-1.0683509111404419e+00 }
           - { classID:0, rect:[ -194, 48, 339, 291 ],
               score:-1.0902605056762695e+00 }
           - { classID:0, rect:[ 334, 83, 97, 84 ],
               score:-1.1376963853836060e+00 }
           - { classID:0, rect:[ -367, 0, 514, 441 ],
               score:-1.1380331516265869e+00 }
           - { classID:0, rect:[ 253, 253, 295, 253 ],
               score:-1.1537185907363892e+00 }
           - { classID:0, rect:[ 207, 0, 364, 311 ],
               score:-1.1885852813720703e+00 } }
       - {
           img_path:"C:/Users/David Romero/Documents/MasterThesis/Object Detection/Dataset/voc2006_test/VOCdevkit/VOC2006/PNGImages\\000002.png",
           second:
           - { classID:0, rect:[ 73, 0, 515, 441 ],
               score:1.4547452330589294e-01 }
           - { classID:0, rect:[ -90, 90, 814, 453 ],
               score:-8.4430181980133057e-01 }
           - { classID:0, rect:[ -181, -181, 814, 452 ],
               score:-9.3141359090805054e-01 }
           - { classID:0, rect:[ 571, 0, 182, 155 ],
               score:-1.1792818307876587e+00 }
           - { classID:0, rect:[ 548, -42, 296, 253 ],
               score:-1.1856086254119873e+00 } } }
   - { class_id:2, second:
       - {
           img_path:"C:/Users/David Romero/Documents/MasterThesis/Object Detection/Dataset/voc2006_test/VOCdevkit/VOC2006/PNGImages\\000001.png",
           second:
           - { classID:0, rect:[ 248, 147, 55, 101 ],
               score:-5.5472081899642944e-01 }
           - { classID:0, rect:[ 275, 19, 59, 109 ],
               score:-6.2268644571304321e-01 }
           - { classID:0, rect:[ 291, 230, 84, 97 ],
               score:-7.4705564975738525e-01 }
           - { classID:0, rect:[ 59, 216, 118, 217 ],
               score:-8.4094178676605225e-01 }
           - { classID:0, rect:[ 240, 144, 64, 176 ],
               score:-9.1522359848022461e-01 }
           - { classID:0, rect:[ 147, 18, 110, 202 ],
               score:-9.3370252847671509e-01 }
           - { classID:0, rect:[ 18, 349, 129, 147 ],
               score:-9.3875211477279663e-01 }
           - { classID:0, rect:[ 274, 17, 103, 188 ],
               score:-9.4316035509109497e-01 }
           - { classID:0, rect:[ 48, 12, 73, 133 ],
               score:-9.4868600368499756e-01 }
           - { classID:0, rect:[ 55, 0, 128, 147 ],
               score:-9.6831935644149780e-01 }
           - { classID:0, rect:[ 0, 0, 104, 119 ],
               score:-1.0182489156723022e+00 }
           - { classID:0, rect:[ 12, 272, 78, 143 ],
               score:-1.0215739011764526e+00 }
           - { classID:0, rect:[ 157, 374, 59, 108 ],
               score:-1.0230016708374023e+00 }
           - { classID:0, rect:[ 59, 137, 137, 158 ],
               score:-1.0545262098312378e+00 }
           - { classID:0, rect:[ 0, 137, 205, 377 ],
               score:-1.0558906793594360e+00 }
           - { classID:0, rect:[ -32, 0, 128, 352 ],
               score:-1.0605969429016113e+00 }
           - { classID:0, rect:[ -39, 275, 78, 217 ],
               score:-1.0688495635986328e+00 }
           - { classID:0, rect:[ 0, 55, 390, 446 ],
               score:-1.0950204133987427e+00 }
           - { classID:0, rect:[ 254, -24, 85, 96 ],
               score:-1.1183072328567505e+00 }
           - { classID:0, rect:[ -29, -59, 208, 238 ],
               score:-1.2255374193191528e+00 }
           - { classID:0, rect:[ 240, -34, 120, 136 ],
               score:-1.2673699855804443e+00 }
           - { classID:0, rect:[ -59, 149, 118, 328 ],
               score:-1.2912225723266602e+00 }
           - { classID:0, rect:[ 121, -48, 170, 193 ],
               score:-1.3424271345138550e+00 }
           - { classID:0, rect:[ 0, -181, 316, 362 ],
               score:-1.3905403614044189e+00 }
           - { classID:0, rect:[ 196, 354, 276, 315 ],
               score:-1.3929604291915894e+00 }
           - { classID:0, rect:[ -42, 422, 147, 169 ],
               score:-1.4137116670608521e+00 }
           - { classID:0, rect:[ 250, -27, 195, 222 ],
               score:-1.4380358457565308e+00 } }
       - {
           img_path:"C:/Users/David Romero/Documents/MasterThesis/Object Detection/Dataset/voc2006_test/VOCdevkit/VOC2006/PNGImages\\000002.png",
           second:
           - { classID:0, rect:[ 0, 0, 448, 512 ],
               score:-5.0280290842056274e-01 }
           - { classID:0, rect:[ 541, 265, 59, 109 ],
               score:-9.1512054204940796e-01 }
           - { classID:0, rect:[ 0, -337, 591, 674 ],
               score:-1.0444867610931396e+00 }
           - { classID:0, rect:[ 519, 233, 156, 286 ],
               score:-1.0620992183685303e+00 }
           - { classID:0, rect:[ 533, 0, 194, 533 ],
               score:-1.1968121528625488e+00 }
           - { classID:0, rect:[ -36, -36, 72, 201 ],
               score:-1.3545994758605957e+00 } } }
   - { class_id:3, second:
       - {
           img_path:"C:/Users/David Romero/Documents/MasterThesis/Object Detection/Dataset/voc2006_test/VOCdevkit/VOC2006/PNGImages\\000001.png",
           second:
           - { classID:0, rect:[ 18, 257, 147, 165 ],
               score:-9.3907463550567627e-01 }
           - { classID:0, rect:[ 96, 352, 128, 144 ],
               score:-9.9405372142791748e-01 } } }

Any help will be much appreciated. Thank you all!

David

So, I "solved" the problem. What I did was to save the file not as .yml but as .json. When changing the filename to a .json and reading from the same file, the routines work perfectly. It seems there's a bug in the source code of OpenCV. Therefore, If someone happens to encounter this problem, I recomend to avoid using other formats than .json when storing information with cv::FileStorage . I hope this gets to help someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM