雄辩的ORM与多个模型

时间:2017-04-10 10:06:29

标签: php mysql laravel orm eloquent

我正在尝试创建一个新的Web应用程序,目前我创建了一个静态数组,其中包含一些如下所示的信息:

match

我也有这样的模特:

  • 用户
  • 学校
  • 等级
  • 课程

Lessons表有 school_id 外键。

学校表有 admin_id 外键。

成绩表包含 lesson_id teacher_id user_id 外键。

我生成像之前所示的动态数组一样麻烦。 我尝试使用

private void DetectAndRecognizeFaces()
{
    Image<Gray, byte> grayframe = ImageFrame.Convert<Gray, byte>();

    //Assign user-defined Values to parameter variables:
    minNeighbors = int.Parse(comboBoxMinNeigh.Text);  // the 3rd parameter
    windowsSize = int.Parse(textBoxWinSize.Text);   // the 5th parameter
    scaleIncreaseRate = Double.Parse(comboBoxScIncRte.Text); //the 2nd parameter

    //detect faces from the gray-scale image and store into an array of type 'var',i.e 'MCvAvgComp[]'
    var faces = haar.DetectMultiScale(grayframe, scaleIncreaseRate, minNeighbors, Size.Empty); //the actual face detection happens here

    MessageBox.Show("Total Faces Detected: " + faces.Length.ToString());

    Bitmap BmpInput = grayframe.ToBitmap();
    Bitmap ExtractedFace;   //empty
    Graphics grp;
    //MCvFont font = new MCvFont(FONT.CV_FONT_HERSHEY_TRIPLEX, 0.5d, 0.5d);

    faceRecognizer.Load(recognizeFilePath);
    foreach (var face in faces)
    {
        t = t + 1;
        result = ImageFrame.Copy(face).Convert<Gray, byte>().Resize(100, 100, Inter.Cubic);

        //set the size of the empty box(ExtractedFace) which will later contain the detected face
        ExtractedFace = new Bitmap(face.Width, face.Height);

        //assign the empty box to graphics for painting
        grp = Graphics.FromImage(ExtractedFace);
        //graphics fills the empty box with exact pixels of the face to be extracted from input image
        grp.DrawImage(BmpInput, 0, 0, face, GraphicsUnit.Pixel);



        string name = Recognise(result);
        if (name == "Unknown")
        {
            ImageFrame.Draw(face, new Bgr(Color.Red), 3);

            MessageBox.Show("Face Name is: " + name.ToString());
            ImageFrame.Draw(name, new Point(face.X - 2, face.Y - 2), FontFace.HersheyComplex, 0.5,
                new Bgr(0, 0, 255), 1, LineType.EightConnected, bottomLeftOrigin);
        }
        else
        {
            ImageFrame.Draw(face, new Bgr(Color.Green), 3);

            MessageBox.Show("Face Name is: " + name.ToString());
            ImageFrame.Draw(name, new Point(face.X - 2, face.Y - 2), FontFace.HersheyComplex, 0.5,
                new Bgr(0, 255, 0), 1, LineType.EightConnected, bottomLeftOrigin);
        }


    CamImageBox.Image = ImageFrame;

}

public string Recognise(Image<Gray, byte> Input_image, int Eigen_Thresh = -1)
{
    if (_IsTrained)
    {
        faceRecognizer.Load(recognizeFilePath);
        FaceRecognizer.PredictionResult ER = faceRecognizer.Predict(Input_image);

        if (ER.Label == -1)
        {
            Eigen_Label = "Unknown";
            Eigen_Distance = 0;
            return Eigen_Label;
        }
        else
        {
            Eigen_Label = Names_List[ER.Label];
            Eigen_Distance = (float)ER.Distance;
            if (Eigen_Thresh > -1) Eigen_threshold = Eigen_Thresh;

            //Only use the post threshold rule if we are using an Eigen Recognizer 
            //since Fisher and LBHP threshold set during the constructor will work correctly 
            switch (Recognizer_Type)
            {
                case ("EMGU.CV.EigenFaceRecognizer"):
                    if (Eigen_Distance > Eigen_threshold) return Eigen_Label;
                    else return "Unknown";
                case ("EMGU.CV.LBPHFaceRecognizer"):
                case ("EMGU.CV.FisherFaceRecognizer"):
                default:
                    return Eigen_Label; //the threshold set in training controls unknowns
            }
        }

    }
    else return "";
}

private void LoadTrainingSet()
{
    Bitmap bmpImage;

    for (int i = 0; i < totalRows; i++)
    {
        byte[] fetchedBytes = (byte[])dataTable.Rows[i]["FaceImage"];
        MemoryStream stream = new MemoryStream(fetchedBytes);
        //stream.Write(fetchedBytes, 0, fetchedBytes.Length);
        bmpImage = new Bitmap(stream);
        trainingImages.Add(new Emgu.CV.Image<Gray, Byte>(bmpImage).Resize(100, 100, Inter.Cubic));

        //string faceName = (string)dataTable.Rows[i]["FaceName"];
        int faceName = (int)dataTable.Rows[i]["FaceID"];
        NameLabels.Add(faceName);
        NameLable = (string)dataTable.Rows[i]["FaceName"];
        Names_List.Add(NameLable);
        //ContTrain = NameLabels[i];
    }
    LoadTrainedData();
}

public void LoadTrainedData()
{
    if (trainingImages.ToArray().Length != 0)
    {
        var faceImages = new Image<Gray, byte>[trainingImages.Count()];
        var facesIDs = new int[NameLabels.Count()];
        //var facesNames = new string[Names_List.Count()];
        //int[] faceLabels = new int[NameLabels.Count()];
        //MCvTermCriteria termCrit = new MCvTermCriteria(ContTrain, 0.001);
        for (int i = 0; i < trainingImages.ToArray().Length; i++)
        {
            faceImages[i] = trainingImages[i];
            facesIDs[i] = NameLabels[i];

        }

        try
        {
            faceRecognizer.Train(faceImages, facesIDs);
            faceRecognizer.Save(recognizeFilePath);
            _IsTrained = true;
        }
        catch (Exception error)
        {
            MessageBox.Show(error.ToString());
        }
    }

}

但它并没有像我想的那样做任何事情。

也许有人可以给我一些指示?

2 个答案:

答案 0 :(得分:1)

您可以尝试这样:

App\User::with('school', 'grades', 'lessons')->find(1)->toArray();

你应该得到一个类似你想要的数组。

答案 1 :(得分:0)

出于某种原因,它实际上是像我在第一篇文章中写的那样工作。

不知道为什么它首先不起作用。

很抱歉在互联网上使用空间。 :d