简体   繁体   English

EmguCv使用哪种策略进行SVM分类?

[英]Which strategy is used in EmguCv for SVM Classification?

Currently I am doing my academic Project (Social Media Website). 目前,我正在做我的学术项目(社交媒体网站)。 My intention is when a user post an image the system should recognize the face and label it with her name. 我的意图是,当用户发布图像时,系统应识别出该面部并为其贴上标签。

As prior work I created 5 users. 作为先前的工作,我创建了5个用户。 For training Images, when a user set her profile photo the system will detect it using EmguCv (DetectHaarCascade) and save the image as a bitmap in a folder. 对于训练图像,当用户设置她的个人资料照片时,系统将使用EmguCv(DetectHaarCascade)将其检测出来并将图像另存为位图在文件夹中。 The label of that face will be the User Id of the User, and the label is saved in a text file within the folder. 该脸部的标签将是用户的用户ID,并且该标签将保存在文件夹内的文本文件中。 As training images I uploaded and labeled 10 images for each user. 作为训练图像,我为每个用户上传并标记了10张图像。

The next part is when a user post a photo. 下一部分是用户发布照片的时间。 The system should recognize the face and label it with her name. 系统应识别该脸部并为其贴上名字。 I am using SVM for recognition and classification. 我正在使用SVM进行识别和分类。

My detection code: 我的检测代码:

    //Face Detection
    MCvAvgComp[][] facesDetected = gray.DetectHaarCascade(
                      haar,
                      1.2,
                      4,
                      HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,
                      new Size(20, 20));
    foreach (MCvAvgComp f in facesDetected[0])
    {
     result = currentFrame.Copy(f.rect).Convert<Gray, byte>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
     // at the beginning i am added all the existing images to an List<Image<Gray, byte>> trainingImages and labels to List<string> labels 
     trainingImages.Add(result );
     labels.Add(Session["UserId"].ToString())
     File.WriteAllText((Server.MapPath("~/TrainedFaces/TrainedLabels.txt")), trainingImages.ToArray().Length.ToString() + "%");

       for (int i = 1; i < trainingImages.ToArray().Length + 1; i++)
       {
        //saving the trained images and labells
        trainingImages.ToArray()[i - 1].Save(Server.MapPath("~/TrainedFaces/") + "face" + i + ".bmp");
        File.AppendAllText(Server.MapPath("~/TrainedFaces/TrainedLabels.txt"), labels.ToArray()[i - 1] + "%");
       }
   }

My SVM training code: 我的SVM培训代码:

/*
1. Loaded all the images to trainingImages
2.Loaded all the labels to labels
*/

// Converting My labesl and images to Matrix for preparing training data and training label    

     Matrix<float> TrainindData = new Matrix<float>(trainingImages.Count, 100 * 100);
        int ii = 0;

        foreach (Image<Gray, float> img in trainingImages)
        {
            int jj = 0;
            Matrix<float> Imagemtrx = new Matrix<float>(img.Width, img.Height);
            img.CopyTo(Imagemtrx);
            for (int k = 0; k < Imagemtrx.Rows; k++)
            {
                for (int j = 0; j < Imagemtrx.Cols; j++)
                {
                    TrainindData.Data[ii, jj] = Imagemtrx[k, j];
                    jj++;
                }
            }
            ii++;
        }
        Matrix<float> TrainedLabels = new Matrix<float>(labels.Count, 1);
        int kk = 0;
        foreach (int lab in labels)
        {
            TrainedLabels[kk, 0] = lab;
            kk++;
        }

     SVM model = new SVM();

     SVMParams p = new SVMParams();
        p.KernelType = Emgu.CV.ML.MlEnum.SVM_KERNEL_TYPE.LINEAR;
        //p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.ONE_CLASS;
        p.SVMType = Emgu.CV.ML.MlEnum.SVM_TYPE.C_SVC;
        p.C = 1;
        p.TermCrit = new MCvTermCriteria(100, 0.00001);

        bool trained = model.Train(TrainindData, TrainedLabels, null, null, p);

My recognition and classification code: 我的识别和分类代码:

MCvAvgComp[][] faces=grayFrame.DetectHaarCascade(haar,1.2,10,HAAR_DETECTION_TYPE.DO_CANNY_PRUNING,new Size(20, 20));
foreach (MCvAvgComp face in faces[0])
        {
            InputFrame.Draw(face.rect, new Bgr(Color.Red), 1);
            t = t + 1;
            Image<Gray, float> result = InputFrame.Copy(face.rect).Convert<Gray, float>().Resize(100, 100, Emgu.CV.CvEnum.INTER.CV_INTER_CUBIC);
            //result._EqualizeHist();
            Matrix<float> TestImageMatix = new Matrix<float>(result.Width, result.Height);
            result.CopyTo(TestImageMatix);
            Matrix<float> TestData = new Matrix<float>(1, result.Width * result.Height);
            int z = 0;
            for (int k = 0; k < TestImageMatix.Rows; k++)
            {
                for (int j = 0; j < TestImageMatix.Cols; j++)
                {
                    TestData.Data[0, z] = TestImageMatix[k, j];
                    z++;
                }
            }
            //Here I will Get the UserId as class label. I can find name from database using this Id
            float result1 = model.Predict(TestData);

Now the problem is that when I upload an image that belongs to any of existing class, it will correctly recognize and identify the person. 现在的问题是,当我上传属于任何现有类别的图像时,它将正确识别并识别该人。 But when I post a different photo (of a person that is not there in social media), then it's assigned to one of the existing class label. 但是,当我发布另一张(社交媒体中不存在的人的)照片时,会将其分配给现有班级标签之一。

My questions are: 我的问题是:

  1. I Want to identify only the right person. 我只想确定合适的人。 Remaining can be labeled as Unknown or something else (I dont Know whether any other method needed or not) 剩余的可以标记为“未知”或其他(我不知道是否需要其他方法)

  2. I read about One Vs One and One Vs All Strategies. 我读到了关于一对一的策略和一对一的所有策略。 Which one is used in my code? 我的代码中使用了哪一个?

  3. If no one is used then how to implement them?. 如果没有人使用,那么如何实现呢?

  4. Emgu CV already includes SVM. Emgu CV已包含SVM。 Which type is used in It? It中使用哪种类型?

I read about One Vs One and One Vs All Strategies. 我读到了关于一对一的策略和一对一的所有策略。 Which one is used in my code? 我的代码中使用了哪一个? If no one is used then how to implement them? 如果没有人使用该如何实施? Emgu CV already includes SVM. Emgu CV已包含SVM。 Which type is used in it? 它使用哪种类型?

You are using One vs All strategy, which is implemented in OpenCV using n*(n-1)/2 One Vs One classifiers ( n is the number of labels). 您使用的是“一对多”策略,该策略在OpenCV中使用n*(n-1)/2 1到1个分类器( n是标签数)实现。

I Want to Identify only the right person. 我只想确定合适的人。 Remaining can be labeled as Unknown or something else( I dont Know whether any other method needed or not) 剩余的可以标记为“未知”或其他名称(我不知道是否需要其他方法)

You can built n*(n-1)/2 One Vs One classifiers, and then get the raw response from predict (works only with 2-class problems). 您可以构建n*(n-1)/2 1对1个分类器,然后从predict获得原始响应(仅适用于2类问题)。 The majority of responses identify the output class. 大多数响应标识输出类别。 If the maximum response for the classified class is under a threshold, you can say that none of the class if correctly recognized. 如果分类类别的最大响应低于阈值,则可以说没有一个类别被正确识别。

Or you can use n One class classifiers, and again check if the response for each is under a given threshold. 或者,您可以使用n个分类器,然后再次检查每个分类器的响应是否在给定的阈值以下。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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