Repository navigation
Expand file tree
/
Copy pathImageClassificationController.cs
More file actions
93 lines (77 loc) · 3.49 KB
/
Copy pathImageClassificationController.cs
File metadata and controls
93 lines (77 loc) · 3.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.ML;
using ImageClassification.WebApp;
using ImageClassification.WebApp.ImageHelpers;
using ImageClassification.WebApp.ML.DataModels;
using ImageClassification.DataModels;
using ImageClassification;
namespace TensorFlowImageClassification.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ImageClassificationController : ControllerBase
{
public IConfiguration Configuration { get; }
private readonly PredictionEnginePool<InMemoryImageData, ImagePrediction> _predictionEnginePool;
private readonly ILogger<ImageClassificationController> _logger;
public ImageClassificationController(PredictionEnginePool<InMemoryImageData, ImagePrediction> predictionEnginePool, IConfiguration configuration, ILogger<ImageClassificationController> logger) //When using DI/IoC
{
// Get the ML Model Engine injected, for scoring.
_predictionEnginePool = predictionEnginePool;
Configuration = configuration;
// Get other injected dependencies.
_logger = logger;
}
[HttpPost]
[ProducesResponseType(200)]
[ProducesResponseType(400)]
[Route("classifyimage")]
public async Task<IActionResult> ClassifyImage(IFormFile imageFile)
{
if (imageFile.Length == 0)
return BadRequest();
var imageMemoryStream = new MemoryStream();
await imageFile.CopyToAsync(imageMemoryStream);
// Check that the image is valid.
byte[] imageData = imageMemoryStream.ToArray();
if (!imageData.IsValidImage())
return StatusCode(StatusCodes.Status415UnsupportedMediaType);
_logger.LogInformation("Start processing image...");
// Measure execution time.
var watch = System.Diagnostics.Stopwatch.StartNew();
// Set the specific image data into the ImageInputData type used in the DataView.
var imageInputData = new InMemoryImageData(image: imageData, label: null, imageFileName: null);
// Predict code for provided image.
var prediction = _predictionEnginePool.Predict(imageInputData);
// Stop measuring time.
watch.Stop();
var elapsedMs = watch.ElapsedMilliseconds;
_logger.LogInformation($"Image processed in {elapsedMs} miliseconds");
// Predict the image's label (The one with highest probability).
var imageBestLabelPrediction =
new ImagePredictedLabelWithProbability
{
PredictedLabel = prediction.PredictedLabel,
Probability = prediction.Score.Max(),
PredictionExecutionTime = elapsedMs,
ImageId = imageFile.FileName,
};
return Ok(imageBestLabelPrediction);
}
public static string GetAbsolutePath(string relativePath)
=> FileUtils.GetAbsolutePath(typeof(Program).Assembly, relativePath);
// GET api/ImageClassification
[HttpGet]
public ActionResult<IEnumerable<string>> Get()
=> new string[] { "ACK Heart beat 1", "ACK Heart beat 2" };
}
}