dotnet / dotnet/docs

app.MapPost overhaul

Open
#36,545 0 comments 0 reactions 0 assignees View on GitHub
:watch: Not Triaged dotnet-ml/svc
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):
![image](https://github.com/dotnet/docs/assets/9452611/b0d702ad-4bc0-4454-911b-45374e55094d)
![image](https://github.com/dotnet/docs/assets/9452611/9bbe5a9c-211b-4f45-8fe2-493fca06d204)

Now getting successful result from the API call:
![image](https://github.com/dotnet/docs/assets/9452611/497b82fc-4e12-40db-9d95-46e7cf3ea4e7)

...using..

![test3](https://github.com/dotnet/docs/assets/9452611/25257838-65f4-4cd3-aa6c-b54377cae9b7)

---
#### 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

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.