app.MapPost overhaul
- Dominant language
- No language data
- Stars
- 4.8k
- Forks
- 6.1k
- Avg merge
- 19h 10m
- Merged PRs (30d)
- 268
Description
For the following ML Model Builder tutorial:
[Object Detection - Stop Signs](https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/object-detection-model-builder)
I could not get the API version to work out-of-the-box. Passing in the absolute path in Powershell would always result in the program having a null exception on "imagePath." Here is the original code that was generated by ML Model Builder and added to the project for the web API version:
`app.MapPost("/predict", async (PredictionEnginePool predictionEnginePool, string imagePath) => { /*does not work!...*/ /* var image = MLImage.CreateFromFile(imagePath); */ /* works, but ignores path from POST request... */ var image = MLImage.CreateFromFile(new String("C:/test.jpg")); var input = new StopSignDetection.ModelInput() { Image = image, }; return await Task.FromResult(predictionEnginePool.Predict(input)); });`
...and here is my re-write:
`app.MapPost("/add_data/{queryKey}", async delegate (HttpContext context, PredictionEnginePool predictionEnginePool, string imagePath) { using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8)) { string queryKey = context.Request.RouteValues["queryKey"].ToString(); string jsonstring = await reader.ReadToEndAsync(); dynamic model = JsonConvert.DeserializeObject(jsonstring); var n = model.ImageSource; var o = n.ToString(); var image = MLImage.CreateFromFile(o); var input = new StopSignDetection.ModelInput() { Image = image, }; return await Task.FromResult(predictionEnginePool.Predict(input)); } });`
I then had to modify the test POST request a bit. Here are shots from postman (replace 2008 with your port):


Now getting successful result from the API call:

...using..

---
#### Document Details
⚠ *Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.*
* ID: 63a4f3b7-a2a6-760a-534b-22788ba77b5c
* Version Independent ID: 1bee7901-164f-d9a7-9e7b-fd0ba55ad42e
* Content: [Tutorial: Detect objects in images with Model Builder - ML.NET](https://learn.microsoft.com/en-us/dotnet/machine-learning/tutorials/object-detection-model-builder)
* Content Source: [docs/machine-learning/tutorials/object-detection-model-builder.md](https://github.com/dotnet/docs/blob/main/docs/machine-learning/tutorials/object-detection-model-builder.md)
* Product: **dotnet-ml**
* GitHub Login: @luisquintanilla
* Microsoft Alias: **luquinta**
Contributor guide
Assessment
This issue has not been assessed yet.