Commits

iorodeo  committed ba27cfe

Finshed adding pixel feature vector parameters to json configuration.

  • Participants
  • Parent commits 2997562

Comments (0)

Files changed (3)

File src/demo/fly_sorter/parameters.cpp

     // Parse file data
     float offsetTemp;
     std::vector<StumpData> stumpVectorTemp; 
-    QString parseErrMsg = QString("Unalbe to parse classifier file %1").arg(fileName);
+    QString parseErrMsg = QString("Unable to parse classifier file %1").arg(fileName);
 
     if (lineList.size() < 2)
     {
 const unsigned int PixelFeatureVectorParam::DEFAULT_GRAD_NORM_RADIUS = 5;
 const double PixelFeatureVectorParam::DEFAULT_GRAD_NORM_CONST = 0.005;  
 const unsigned int PixelFeatureVectorParam::DEFAULT_FILL_BNDRY_ERODE_RADIUS = 2;
+const unsigned int PixelFeatureVectorParam::COLOR_EDGE_VECTOR_MIN_SIZE = 2;
+const unsigned int PixelFeatureVectorParam::GRAD_MAG_EDGE_VECTOR_MIN_SIZE = 2;
+const unsigned int PixelFeatureVectorParam::GRAD_ORI_CENT_VECTOR_MIN_SIZE = 2;
+const unsigned int PixelFeatureVectorParam::BIN_PARAM_MIN_SIZE = 2;
 
 // TEMPORARY  
 std::vector<cv::Scalar> PixelFeatureVectorParam::createDefaultColorEdgeVector()
 const std::vector<float> PixelFeatureVectorParam::DEFAULT_GRAD_ORI_CENT_VECTOR = 
 createDefaultGradOriCentVector();
 
-// TEMPORARY 
 std::vector<BinParam> PixelFeatureVectorParam::createDefaultBinParam()
 {
     std::vector<BinParam> binParam;
 QVariantMap PixelFeatureVectorParam::toMap()
 {
     QVariantMap paramMap;
+    paramMap.insert("gradNormRadius", gradNormRadius);
+    paramMap.insert("gradNormConst", gradNormConst);
+    paramMap.insert("fillBndryErodeRadius", fillBndryErodeRadius);
+
+    // Convert colorEdgeVector to list of QVariants
+    QList<QVariant> colorEdgeList;
+    for (int i=0; i<colorEdgeVector.size(); i++)
+    {
+        QList<QVariant> data;
+        for (int j=0; j<3; j++)
+        {
+            data.push_back(QVariant(colorEdgeVector[i][j]));
+        }
+        colorEdgeList.push_back(QVariant(data));
+    }
+    paramMap.insert("colorEdgeVector", colorEdgeList);
+
+    // Convert gradMagEdgeVector to list of QVariants
+    QList<QVariant> gradMagEdgeList;
+    for (int i=0; i<gradMagEdgeVector.size(); i++)
+    {
+        gradMagEdgeList.push_back(QVariant(gradMagEdgeVector[i]));
+    }
+    paramMap.insert("gradMagEdgeVector", gradMagEdgeList);
+
+    // Convert gradOriCentVector to list of QVariants
+    QList<QVariant> gradOriCentList;
+    for (int i=0; i<gradOriCentVector.size(); i++)
+    {
+        gradOriCentList.push_back(QVariant(gradOriCentVector[i]));
+    }
+    paramMap.insert("gradOriCentVector", gradOriCentList);
+
+    // Convert binParam to list of QVariaints
+    QList<QVariant> binParamList;
+    for (int i=0; i<binParam.size(); i++)
+    {
+        QList<QVariant> valueList;
+        valueList.push_back(QVariant(binParam[i].numX));
+        valueList.push_back(QVariant(binParam[i].numY));
+        binParamList.push_back(QVariant(valueList));
+    }
+    paramMap.insert("binParam",binParamList);
+
     return paramMap;
 }
 
     if (paramMap.isEmpty())
     {
         rtnStatus.success = false;
-        rtnStatus.message = QString("pixel feature vector parameter map is empty");
+        rtnStatus.message = QString("pixelFeatureVector parameter map is empty");
         return rtnStatus;
     }
+
     // Get gradNormRadius
     // ----------------------------
-    // unsigned int gradNormRadius;
+    if (!paramMap.contains("gradNormRadius"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'gradNormRadius' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["gradNormRadius"].canConvert<unsigned int>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'gradNormRadius' to unsigned int");
+        return rtnStatus;
+    }
+    gradNormRadius = paramMap["gradNormRadius"].toUInt();
+
 
     // Get gradNormConst
     // ----------------------------
-    // double gradNormConst;
+    if (!paramMap.contains("gradNormConst"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'gradNormConst' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["gradNormConst"].canConvert<double>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'gradNormConst' to double");
+        return rtnStatus;
+    }
+    gradNormConst = paramMap["gradNormConst"].toDouble();
 
     // Get fillBndryErodeRadius
     // ----------------------------
-    // unsigned int fillBndryErodeRadius;
+    if (!paramMap.contains("fillBndryErodeRadius"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'fillBndryErodeRadius' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["fillBndryErodeRadius"].canConvert<unsigned int>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'fillBnderyErodeRadius' to double");
+        return rtnStatus;
+    }
+    fillBndryErodeRadius = paramMap["fillBndryErodeRadius"].toUInt();
 
     // Get colorEdgeVector
     // ----------------------------
-    // std::vector<cv::Scalar> colorEdgeVector;
+    if (!paramMap.contains("colorEdgeVector"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'colorEdgeVector' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["colorEdgeVector"].canConvert<QList<QVariant>>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter colorEdgeVector to QList<QVariant>");
+        return rtnStatus;
+    }
+
+    QList<QVariant> colorEdgeList = paramMap["colorEdgeVector"].toList();
+    if (colorEdgeList.size() < COLOR_EDGE_VECTOR_MIN_SIZE)
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("pixelFeatureVector -> colorEdgeVector must have at least"); 
+        rtnStatus.message += QString("%1 rows").arg(COLOR_EDGE_VECTOR_MIN_SIZE);
+        return rtnStatus;
+    }
+    std::vector<cv::Scalar> colorEdgeVectorTemp(colorEdgeList.size());
+
+    for (int i=0; i<colorEdgeList.size(); i++)
+    {
+        if (!colorEdgeList[i].canConvert<QList<QVariant>>())
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("Unable to convert pixelFeatureVector -> colorEdgeVector row to QList<QVariant>");
+            return rtnStatus;
+        }
+        QList<QVariant> rowList = colorEdgeList[i].toList();
+        if (rowList.size() != 3)
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("each row of pixelFeatureVector -> colorEdgeVector must have 3 elements.");
+            return rtnStatus;
+        }
+        for (int j=0; j<rowList.size();j++)
+        {
+            if (!rowList[j].canConvert<double>())
+            {
+                rtnStatus.success = false;
+                rtnStatus.message = QString("unable to convert pixelFeatureVector -> colorEdgeVector element to double");
+                return rtnStatus;
+            }
+            colorEdgeVectorTemp[i][j] = rowList[j].toDouble();
+        }
+    }
+    colorEdgeVector = colorEdgeVectorTemp;
 
     // Get gradMagEdgeVector
     // ----------------------------
     // std::vector<float> gradMagEdgeVector;     
+    if (!paramMap.contains("gradMagEdgeVector"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'gradMagEdgeVector' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["gradMagEdgeVector"].canConvert<QList<QVariant>>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'gradMagEdgeVector' to QList<QVariant>");
+        return rtnStatus;
+    }
+    QList<QVariant> gradMagEdgeList = paramMap["gradMagEdgeVector"].toList();
+    if (gradMagEdgeList.size() < GRAD_MAG_EDGE_VECTOR_MIN_SIZE)
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("pixelFeatureVector -> gradMagEdgeVector must have at least");
+        rtnStatus.message += QString("%1 elements").arg(GRAD_MAG_EDGE_VECTOR_MIN_SIZE);
+        return rtnStatus;
+    }
+    std::vector<float> gradMagEdgeVectorTemp(gradMagEdgeList.size());     
+    for (int i=0; i<gradMagEdgeList.size(); i++)
+    {
+        if (!gradMagEdgeList[i].canConvert<float>())
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("Unable to convert pixelFeatureVector -> gradMagEdgeVector element to float");
+            return rtnStatus;
+        }
+        gradMagEdgeVectorTemp[i] = gradMagEdgeList[i].toFloat();
+    }
+    gradMagEdgeVector = gradMagEdgeVectorTemp;
 
-    // Get gradOriCentVectory
+
+    // Get gradOriCentVector
     // ----------------------------
-    // std::vector<float> gradOriCentVector;
+    if (!paramMap.contains("gradOriCentVector"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'gradOriCentVector' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["gradOriCentVector"].canConvert<QList<QVariant>>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'gradOriCentVector' to QList<QVariant>");
+        return rtnStatus;
+    }
+    QList<QVariant> gradOriCentList = paramMap["gradOriCentVector"].toList();
+    if (gradOriCentList.size() < GRAD_ORI_CENT_VECTOR_MIN_SIZE)
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("pixelFeatureVector -> gradOriCentVector must have at least");
+        rtnStatus.message += QString("%1 elements").arg(GRAD_ORI_CENT_VECTOR_MIN_SIZE);
+        return rtnStatus;
+    }
+    std::vector<float> gradOriCentVectorTemp(gradOriCentList.size());
+    for (int i=0; i<gradOriCentList.size(); i++)
+    {
+        if (!gradOriCentList[i].canConvert<float>())
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("unable to convert pixelFeatureVector -> gradOriCentVector element to float");
+            return rtnStatus;
+        }
+        gradOriCentVectorTemp[i] = gradOriCentList[i].toFloat();
+    }
+    gradOriCentVector = gradOriCentVectorTemp;
+
 
     // Get binParam
     // ----------------------------
-    // std::vector<BinParam> binParam;
+    if (!paramMap.contains("binParam"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'binParam' not found in pixelFeatureVector parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["binParam"].canConvert<QList<QVariant>>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert pixelFeatureVector parameter 'binParam' to QList<QVariant>");
+        return rtnStatus;
+    }
+    QList<QVariant> binParamList = paramMap["binParam"].toList();
+    if (binParamList.size() < BIN_PARAM_MIN_SIZE)
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("pixelFeatureVector -> binParam must have at least");
+        rtnStatus.message += QString("%1 rows").arg(BIN_PARAM_MIN_SIZE);
+        return rtnStatus;
+    }
+    std::vector<BinParam> binParamTemp(binParamList.size());
+    for (int i=0; i<binParamList.size(); i++)
+    {
+        if (!binParamList[i].canConvert<QList<QVariant>>())
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("pixelFeatureVector -> binParam unable to convert row to QList<QVariant>");
+            return rtnStatus;
+        }
+        QList<QVariant> rowList = binParamList[i].toList();
+        if (rowList.size() != 2)
+        {
+            rtnStatus.success = false;
+            rtnStatus.message = QString("pixelFeatureVector -> binParam rows must have 2 elements");
+            return rtnStatus;
+        }
+        for (int j=0; j<2; j++)
+        {
+            if (!rowList[j].canConvert<unsigned int>())
+            {
+                rtnStatus.success = false;
+                rtnStatus.message = QString("pixelFeatureVector -> binParam unable to convert element to unsigned int");
+                return rtnStatus;
+            }
+        }
+        binParamTemp[i].numX = rowList[0].toUInt();
+        binParamTemp[i].numY = rowList[1].toUInt();
+    }
+    binParam = binParamTemp;
 
     return rtnStatus;
 }
         fillValuesList.push_back(QVariant(fillValuesLUV[i]));
     }
     paramMap.insert("fillValuesLUV", fillValuesList);
-    paramMap.insert("orientClassifierFile", orientClassifier.fileName);
+    paramMap.insert("orientationClassifierFile", orientClassifier.fileName);
+    paramMap.insert("pixelFeatureVector", pixelFeatureVector.toMap());
     return paramMap;
 }
 
         return rtnStatus;
     }
     QList<QVariant> fillValuesList = paramMap["fillValuesLUV"].toList();
+    if (fillValuesList.size() != 3)
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("hogPositionFitter parameter 'fillValuesLUV' must have length=3");
+        return rtnStatus;
+    }
     for (int i=0; i<3; i++)
     {
         QVariant var = fillValuesList.at(i);
         fillValuesLUV[i] = var.toDouble();
     }
 
-
     // Get classifier file name 
     // ------------------------
-    if (!paramMap.contains("orientClassifierFile"))
+    if (!paramMap.contains("orientationClassifierFile"))
     {
         rtnStatus.success = false;
-        rtnStatus.message = QString("'orientClassifierFile' not found in hogPositionFitter parameters");
+        rtnStatus.message = QString("'orientationClassifierFile' not found in hogPositionFitter parameters");
         return rtnStatus;
     }
-    if (!paramMap["orientClassifierFile"].canConvert<QString>())
+    if (!paramMap["orientationClassifierFile"].canConvert<QString>())
     {
         rtnStatus.success = false;
-        rtnStatus.message = QString("Unable to convert hogPositionFitter parameter 'orientClassifierFile' to QString");
+        rtnStatus.message = QString("Unable to convert hogPositionFitter parameter 'orientationClassifierFile' to QString");
         return rtnStatus;
     }
-     QString fileName = paramMap["orientClassifierFile"].toString();
+     QString fileName = paramMap["orientationClassifierFile"].toString();
 
     // Check that parameters can be loaded from file
     ClassifierParam classifierTemp;
     }
     orientClassifier = classifierTemp;
 
+    // Get Pixel feature vector
+    if (!paramMap.contains("pixelFeatureVector"))
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("'pixelFeatureVector' not found in hogPositionFitter parameters");
+        return rtnStatus;
+    }
+    if (!paramMap["pixelFeatureVector"].canConvert<QVariantMap>())
+    {
+        rtnStatus.success = false;
+        rtnStatus.message = QString("Unable to convert hogPosition Fitter paramseter 'pixelFeatureVector' to QVariantMap");
+        return rtnStatus;
+    }
+    QVariantMap pixelFeatureVectorParamMap = paramMap["pixelFeatureVector"].toMap();
+    rtnStatus = pixelFeatureVector.fromMap(pixelFeatureVectorParamMap);
+    if (!rtnStatus.success)
+    {
+        return rtnStatus;
+    }
+
+    rtnStatus.success = true;
+    rtnStatus.message = QString("");
     return rtnStatus;
-
 }
 
 

File src/demo/fly_sorter/parameters.hpp

         std::vector<cv::Scalar> colorEdgeVector;
         static const std::vector<cv::Scalar> DEFAULT_COLOR_EDGE_VECTOR;
         static std::vector<cv::Scalar> createDefaultColorEdgeVector();
+        static const unsigned int COLOR_EDGE_VECTOR_MIN_SIZE;
 
         std::vector<float> gradMagEdgeVector;     
         static const std::vector<float> DEFAULT_GRAD_MAG_EDGE_VECTOR;
         static std::vector<float> createDefaultGradMagEdgeVector();
+        static const unsigned int GRAD_MAG_EDGE_VECTOR_MIN_SIZE;
 
         std::vector<float> gradOriCentVector;
         static const std::vector<float> DEFAULT_GRAD_ORI_CENT_VECTOR;
         static std::vector<float> createDefaultGradOriCentVector();
+        static const unsigned int GRAD_ORI_CENT_VECTOR_MIN_SIZE;
 
         std::vector<BinParam> binParam;
         static const std::vector<BinParam> DEFAULT_BIN_PARAM;
         static std::vector<BinParam> createDefaultBinParam();
+        static const unsigned int BIN_PARAM_MIN_SIZE;
 
         PixelFeatureVectorParam();
         QVariantMap toMap();

File src/utility/json_utils.cpp

         unsigned int indentLevel = 0;
         unsigned int indentStep = JSON_INDENT_STEP;
         unsigned int indent = 0;
+        bool isInnerList = false;
 
         while (pos < jsonArray.size()) 
         {
             bool isSpecialChar = false;
-            if (jsonArray[pos] == '}' || jsonArray[pos] == ']')
+            if (jsonArray[pos] == '}')
             {
                 addNewLineToByteArray(jsonArrayNew);
                 indentLevel -= 1;
                 jsonArrayNew.append(jsonArray[pos]);
                 isSpecialChar = true;
             }
-            else if ((jsonArray[pos] =='{') || (jsonArray[pos] == '[') )
+            else if (jsonArray[pos] =='{') 
             {
                 jsonArrayNew.append(jsonArray[pos]);
                 addNewLineToByteArray(jsonArrayNew);
             else if (jsonArray[pos] == ',')
             {
                 jsonArrayNew.append(jsonArray[pos]);
-                addNewLineToByteArray(jsonArrayNew);
-                indent = indentLevel*indentStep;
-                addIndentToByteArray(jsonArrayNew, indent);
-                isSpecialChar = true;
+                if (!isInnerList)
+                {
+                    addNewLineToByteArray(jsonArrayNew);
+                    indent = indentLevel*indentStep;
+                    addIndentToByteArray(jsonArrayNew, indent);
+                    isSpecialChar = true;
+                }
+            }
+            else if (jsonArray[pos] == '[')
+            {
+                jsonArrayNew.append(jsonArray[pos]);
+                for (unsigned int tempPos=pos+1; tempPos<jsonArray.size(); tempPos++)
+                {
+                    isInnerList = false;
+                    if (jsonArray[tempPos] == ']')
+                    {
+                        isInnerList = true;
+                        break;
+                    }
+                    if (jsonArray[tempPos] == '[')
+                    {
+                        isInnerList = false;
+                        break;
+                    }
+                }
+                if (!isInnerList)
+                {
+                    addNewLineToByteArray(jsonArrayNew);
+                    indentLevel += 1;
+                    indent = indentLevel*indentStep;
+                    addIndentToByteArray(jsonArrayNew, indent);
+                    isSpecialChar = true;
+                }
+            }
+            else if (jsonArray[pos] == ']')
+            {
+                if (isInnerList)
+                {
+                    isInnerList = false;
+                    jsonArrayNew.append(jsonArray[pos]);
+                }
+                else
+                { 
+                    addNewLineToByteArray(jsonArrayNew);
+                    indentLevel -= 1;
+                    indent = indentLevel*indentStep;
+                    addIndentToByteArray(jsonArrayNew, indent);
+                    jsonArrayNew.append(jsonArray[pos]);
+                    isSpecialChar = true;
+                }
             }
             else
             {
         return jsonArrayNew;
     }
 
+    // Old version
+    // ------------------------------------------------------------------------
+    //QByteArray prettyIndentJson(QByteArray jsonArray)
+    //{
+    //    // Week and kludgey json pretty printer.
+    //    QByteArray jsonArrayNew;
+    //    unsigned int pos = 0;
+    //    unsigned int indentLevel = 0;
+    //    unsigned int indentStep = JSON_INDENT_STEP;
+    //    unsigned int indent = 0;
+
+    //    while (pos < jsonArray.size()) 
+    //    {
+    //        bool isSpecialChar = false;
+    //        if ((jsonArray[pos] == '}') || (jsonArray[pos] == ']'))
+    //        {
+    //            addNewLineToByteArray(jsonArrayNew);
+    //            indentLevel -= 1;
+    //            indent = indentLevel*indentStep;
+    //            addIndentToByteArray(jsonArrayNew, indent);
+    //            jsonArrayNew.append(jsonArray[pos]);
+    //            isSpecialChar = true;
+    //        }
+    //        else if ((jsonArray[pos] =='{') || (jsonArray[pos] == '[') )
+    //        {
+    //            jsonArrayNew.append(jsonArray[pos]);
+    //            addNewLineToByteArray(jsonArrayNew);
+    //            indentLevel += 1;
+    //            indent = indentLevel*indentStep;
+    //            addIndentToByteArray(jsonArrayNew, indent);
+    //            isSpecialChar = true;
+    //        }
+    //        else if (jsonArray[pos] == ',')
+    //        {
+    //            jsonArrayNew.append(jsonArray[pos]);
+    //            addNewLineToByteArray(jsonArrayNew);
+    //            indent = indentLevel*indentStep;
+    //            addIndentToByteArray(jsonArrayNew, indent);
+    //            isSpecialChar = true;
+    //        }
+    //        else
+    //        {
+    //            jsonArrayNew.append(jsonArray[pos]);
+    //        }
+
+
+    //        if (isSpecialChar)
+    //        {
+    //            pos++;
+    //            while (jsonArray[pos] == ' ')
+    //            {
+    //                pos++;
+    //            }
+
+    //        }
+    //        else
+    //        {
+    //            pos++;
+    //        }
+    //    }
+    //    return jsonArrayNew;
+    //}
+
 
     void addIndentToByteArray(QByteArray &array, unsigned int width)
     {