From 9c998f76142f3030358cd85f604fb7044fb3790d Mon Sep 17 00:00:00 2001 From: Lance Date: Mon, 5 Aug 2024 17:12:21 +0800 Subject: [PATCH] update net6 --- .../Controllers/DiscernController.cs | 94 +++++++++---------- Mask.ML.WebApi/Mask.ML.WebApi.csproj | 6 +- Mask.ML.WebApi/Startup.cs | 17 +++- .../Mask.MLML.ConsoleApp.csproj | 4 +- Mask.MLML.ConsoleApp/ModelBuilder.cs | 9 +- Mask.MLML.Model/ConsumeModel.cs | 4 +- 6 files changed, 72 insertions(+), 62 deletions(-) diff --git a/Mask.ML.WebApi/Controllers/DiscernController.cs b/Mask.ML.WebApi/Controllers/DiscernController.cs index 93c5271..9a316d5 100644 --- a/Mask.ML.WebApi/Controllers/DiscernController.cs +++ b/Mask.ML.WebApi/Controllers/DiscernController.cs @@ -8,6 +8,7 @@ using System.Linq; namespace Mask.ML.WebApi.Controllers { [ApiController] + [Route("api/[controller]")] public class DiscernController : ControllerBase { /// @@ -16,61 +17,54 @@ namespace Mask.ML.WebApi.Controllers /// /// [HttpPost("upload")] - [Route("api/discern/upload")] - public IActionResult UploadFile([FromForm] IFormCollection collection) + public IActionResult UploadFile(IFormFile file) { //申明返回的结果 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(); - } - //文件路径 - string FilePath = AppDomain.CurrentDomain.BaseDirectory+"/TempFiles/"; - string name = file.FileName; - string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r; - //获取文件类型 - string type = System.IO.Path.GetExtension(name); - DirectoryInfo di = new DirectoryInfo(FilePath); - if (!di.Exists) - { - di.Create(); - } - //文件保存的路径 - var filefullname = FilePath + FileName + type; - using (FileStream fs = System.IO.File.Create(filefullname)) - { - // 复制文件 - file.CopyTo(fs); - // 清空缓冲区数据 - fs.Flush(); - fs.Close(); - fs.Dispose(); - } - //成功提示赋值到返回结果中 - //result = "文件上传成功"; - // 创建样例数据的单个实例对模型输入数据集的第一行 - ModelInput sampleData = new ModelInput() - { - ImageSource = filefullname, - }; - // 获取预测结果 - var predictionResult = ConsumeModel.Predict(sampleData); - //System.IO.File.Delete(filefullname); - return Ok(predictionResult.Prediction); + //做随机数,用到文件夹名字上,防重名 + Random random = new Random(); + string r = ""; + int i; + for (i = 1; i < 11; i++) + { + r += random.Next(0, 9).ToString(); } - return NoContent(); + //文件路径 + string FilePath = AppDomain.CurrentDomain.BaseDirectory + "/TempFiles/"; + string name = file.FileName; + string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r; + //获取文件类型 + string type = System.IO.Path.GetExtension(name); + DirectoryInfo di = new DirectoryInfo(FilePath); + if (!di.Exists) + { + di.Create(); + } + //文件保存的路径 + var filefullname = FilePath + FileName + type; + using (FileStream fs = System.IO.File.Create(filefullname)) + { + // 复制文件 + file.CopyTo(fs); + // 清空缓冲区数据 + fs.Flush(); + fs.Close(); + fs.Dispose(); + } + //成功提示赋值到返回结果中 + //result = "文件上传成功"; + + // 创建样例数据的单个实例对模型输入数据集的第一行 + ModelInput sampleData = new ModelInput() + { + ImageSource = filefullname, + }; + // 获取预测结果 + var predictionResult = ConsumeModel.Predict(sampleData); + //System.IO.File.Delete(filefullname); + return Ok(predictionResult.Prediction); + } } diff --git a/Mask.ML.WebApi/Mask.ML.WebApi.csproj b/Mask.ML.WebApi/Mask.ML.WebApi.csproj index bb1dd8b..531acfe 100644 --- a/Mask.ML.WebApi/Mask.ML.WebApi.csproj +++ b/Mask.ML.WebApi/Mask.ML.WebApi.csproj @@ -1,11 +1,11 @@ - + - net5.0 + net6.0 - + diff --git a/Mask.ML.WebApi/Startup.cs b/Mask.ML.WebApi/Startup.cs index 74c6582..cd95bbb 100644 --- a/Mask.ML.WebApi/Startup.cs +++ b/Mask.ML.WebApi/Startup.cs @@ -25,7 +25,15 @@ namespace Mask.ML.WebApi // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { - + services.AddSwaggerGen(c => + { + c.SwaggerDoc("v1", new OpenApiInfo + { + Title = "Mask.ML", + Version = "v1", + Description = "˵ĵ" + }); + }); services.AddControllers(); } @@ -37,6 +45,13 @@ namespace Mask.ML.WebApi app.UseDeveloperExceptionPage(); } + app.UseSwagger(); + app.UseSwaggerUI(c => + { + c.SwaggerEndpoint("/swagger/v1/swagger.json", "Mask.ML v1"); + c.RoutePrefix = string.Empty;//øĿ¼Ϊswagger,ֵÿ + }); + app.UseRouting(); app.UseAuthorization(); diff --git a/Mask.MLML.ConsoleApp/Mask.MLML.ConsoleApp.csproj b/Mask.MLML.ConsoleApp/Mask.MLML.ConsoleApp.csproj index ec6154d..110488b 100644 --- a/Mask.MLML.ConsoleApp/Mask.MLML.ConsoleApp.csproj +++ b/Mask.MLML.ConsoleApp/Mask.MLML.ConsoleApp.csproj @@ -1,8 +1,8 @@ - + Exe - netcoreapp3.1 + net6.0 diff --git a/Mask.MLML.ConsoleApp/ModelBuilder.cs b/Mask.MLML.ConsoleApp/ModelBuilder.cs index 8b749c0..fc30528 100644 --- a/Mask.MLML.ConsoleApp/ModelBuilder.cs +++ b/Mask.MLML.ConsoleApp/ModelBuilder.cs @@ -13,17 +13,18 @@ namespace Mask_MLML.ConsoleApp { public static class ModelBuilder { - private static string TRAIN_DATA_FILEPATH = @"C:\Users\HUAWEI\AppData\Local\Temp\b441e20c-6f79-4437-a21a-be0014456e13.tsv"; - private static string MODEL_FILEPATH = @"C:\Users\HUAWEI\AppData\Local\Temp\MLVSTools\Mask.MLML\Mask.MLML.Model\MLModel.zip"; + private static string TRAIN_DATA_FILEPATH = @"\b441e20c-6f79-4437-a21a-be0014456e13.tsv"; + private static string MODEL_FILEPATH = @"\MLModel.zip"; // Create MLContext to be shared across the model creation workflow objects // Set a random seed for repeatable/deterministic results across multiple trainings. private static MLContext mlContext = new MLContext(seed: 1); public static void CreateModel() { + // Load Data IDataView trainingDataView = mlContext.Data.LoadFromTextFile( - path: TRAIN_DATA_FILEPATH, + path: Path.Combine(AppDomain.CurrentDomain.BaseDirectory, TRAIN_DATA_FILEPATH), hasHeader: true, separatorChar: '\t', allowQuoting: true, @@ -39,7 +40,7 @@ namespace Mask_MLML.ConsoleApp Evaluate(mlContext, trainingDataView, trainingPipeline); // Save model - SaveModel(mlContext, mlModel, MODEL_FILEPATH, trainingDataView.Schema); + SaveModel(mlContext, mlModel, Path.Combine(AppDomain.CurrentDomain.BaseDirectory, MODEL_FILEPATH), trainingDataView.Schema); } public static IEstimator BuildTrainingPipeline(MLContext mlContext) diff --git a/Mask.MLML.Model/ConsumeModel.cs b/Mask.MLML.Model/ConsumeModel.cs index f48f563..9498cdf 100644 --- a/Mask.MLML.Model/ConsumeModel.cs +++ b/Mask.MLML.Model/ConsumeModel.cs @@ -6,6 +6,7 @@ using System.Linq; using System.Text; using Microsoft.ML; using Mask_MLML.Model; +using System.IO; namespace Mask_MLML.Model { @@ -25,9 +26,8 @@ namespace Mask_MLML.Model { // Create new MLContext MLContext mlContext = new MLContext(); - // Load model & create prediction engine - string modelPath = @"C:\Users\HUAWEI\AppData\Local\Temp\MLVSTools\Mask.MLML\Mask.MLML.Model\MLModel.zip"; + string modelPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "MLModel.zip"); ITransformer mlModel = mlContext.Model.Load(modelPath, out var modelInputSchema); var predEngine = mlContext.Model.CreatePredictionEngine(mlModel);