4. Call the model to obtain the results after model analysis

1. On the basis of the previous project, add a folder ModelService, which will store the predicted service we call

2. Add a class KouzhaoService to call the prediction model (we’ll call it the mask prediction service here)

3. Core code

public class KouzhaoService
{
    /// <summary>
    ///Obtaining forecast results
    /// </summary>
    /// <param path="">Image address that needs to be predicted</param>
    /// <returns></returns>
    public string GetPredictionResult(string path)
    {
        // Create a single instance of the sample data to input the first row of the dataset to the model
        ModelInput sampleData = new ModelInput()
        {
            ImageSource = path,
        };
        // Get the forecast result
        var predictionResult = ConsumeModel.Predict(sampleData);
        returnpredictionResult.Prediction; }}Copy the code

3.1 Complete Code:

using KouzhaoML.Model; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; The namespace kouzhaowebapi. ModelService {public class KouzhaoService {/ / / < summary > / / / / / / access to predict the result < / summary > / / / < param </param> // <returns></returns> public string GetPredictionResult(string path) {// ModelInput sampleData = new ModelInput() {ImageSource = path,}; Var predictionResult = ConsumeModel.Predict(sampleData); return predictionResult.Prediction; }}}Copy the code

4. Go back to our WebAPI and call this prediction

Core code:

New KouzhaoService().getPredictionResult (image path);Copy the code

Complete code:

using KouzhaoML.Model; using kouzhaowebapi.ModelService; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace kouzhaowebapi.Controllers { [ApiController] public class DiscernController : ControllerBase {/// <summary> // Masks validation / / / < / summary > / / / < param name = "stream" > < param > / / / < param name = "fileName" > < param > [HttpPost (" upload ")] [Route(" API /discern/ Kouzhao ")] public object UploadFile([FromForm] IFormCollection collection) {// Declare the result string result = ""; FormFileCollection filelist = (FormFileCollection)collection.Files; If (filelist! = null && filelist.any ()) {IFormFile file = filelist[0]; Random Random = new Random(); string r = ""; int i; for (i = 1; i < 11; i++) { r += random.Next(0, 9).ToString(); } // File path string Tpath = "/file/"; string name = file.FileName; string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r; string FilePath = @"C:\" + Tpath; / / get the file type string type = System. IO. Path. GetExtension (name); DirectoryInfo di = new DirectoryInfo(FilePath); if (! di.Exists) { di.Create(); } // FilePath var filefullname = FilePath + FileName + type; Using (FileStream fs = system.io.file.create (fileFullName)) {// Copy File file.copyto (fs); // Flush buffer data fs.flush (); fs.Close(); fs.Dispose(); } // result = "file uploaded successfully "; // Call the prediction model and assign the result to the result variable, which is returned as the result result = new KouzhaoService().getPredictionResult (fileFullName); } return result; }}}Copy the code

5. Next, run the project. We run the project using the console interface. Press F5 to run the project and get the following, which means the program is started

6. Upload images using the Postman test









Next we’re going to show a picture of a mask on





[Img-cbhmehhV-1613978818608]The upload – images. Jianshu. IO/upload_imag…)]

This is all the content of invoking the model to obtain the results after model analysis. The follow-up will continue to show how to access the wechat public account

Welcome to qq group communication: 704028989