update net6

This commit is contained in:
Lance 2024-08-05 17:12:21 +08:00
parent 5e08f0b7d5
commit 9c998f7614
6 changed files with 72 additions and 62 deletions

View File

@ -8,6 +8,7 @@ using System.Linq;
namespace Mask.ML.WebApi.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class DiscernController : ControllerBase
{
/// <summary>
@ -16,17 +17,11 @@ namespace Mask.ML.WebApi.Controllers
/// <param name="stream"></param>
/// <param name="fileName"></param>
[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 = "";
@ -36,7 +31,7 @@ namespace Mask.ML.WebApi.Controllers
r += random.Next(0, 9).ToString();
}
//文件路径
string FilePath = AppDomain.CurrentDomain.BaseDirectory+"/TempFiles/";
string FilePath = AppDomain.CurrentDomain.BaseDirectory + "/TempFiles/";
string name = file.FileName;
string FileName = DateTime.Now.ToString("yyyyMMddHHmmssfff") + r;
//获取文件类型
@ -69,8 +64,7 @@ namespace Mask.ML.WebApi.Controllers
var predictionResult = ConsumeModel.Predict(sampleData);
//System.IO.File.Delete(filefullname);
return Ok(predictionResult.Prediction);
}
return NoContent();
}
}

View File

@ -1,11 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk.Web">
<Project Sdk="Microsoft.NET.Sdk.Web">
<PropertyGroup>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Swashbuckle.AspNetCore" Version="5.6.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.7.0" />
</ItemGroup>
<ItemGroup>

View File

@ -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();

View File

@ -1,8 +1,8 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.ML" Version="1.5.0-preview2" />

View File

@ -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<ModelInput>(
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<ITransformer> BuildTrainingPipeline(MLContext mlContext)

View File

@ -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<ModelInput, ModelOutput>(mlModel);