update net6
This commit is contained in:
parent
5e08f0b7d5
commit
9c998f7614
@ -8,6 +8,7 @@ using System.Linq;
|
||||
namespace Mask.ML.WebApi.Controllers
|
||||
{
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class DiscernController : ControllerBase
|
||||
{
|
||||
/// <summary>
|
||||
@ -16,61 +17,54 @@ 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 = "";
|
||||
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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -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>
|
||||
|
@ -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();
|
||||
|
@ -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" />
|
||||
|
@ -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)
|
||||
|
@ -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);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user