コード例 #1
0
TrainingData View::readImageClassMapFile(QString const& fileName) {
    QFile file(fileName);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        FileOperationException(QString("Can not open '%1' file").arg(fileName)).raise();

    _classNumberClassNameMap.clear();
    int lastImageClassNumber = -1;
    TrainingData trainingData;
    QTextStream inputTextStream(&file);
    while (!inputTextStream.atEnd()) {
        QString line = inputTextStream.readLine();
        QStringList imageNameAndClass = line.split(',', QString::SkipEmptyParts);
        QString imageName = combinePathIfRelative(imageNameAndClass.first(), fileName);

        if(QFile::exists(imageName) == false)
            FileOperationException(QString("File '%1' is not found").arg(imageName)).raise();
        QString imageClass = imageNameAndClass.last();
        int imageClassNumber;
        if(_classNumberClassNameMap.values().contains(imageClass)) {
            imageClassNumber = _classNumberClassNameMap.key(imageClass);
        } else {
            imageClassNumber = ++lastImageClassNumber;
            _classNumberClassNameMap[imageClassNumber] = imageClass;
        }
        trainingData.add(cv::imread(imageName.toStdString(), CV_LOAD_IMAGE_GRAYSCALE), imageClassNumber);
    }
    file.close();

    return trainingData;
}