From cbd864a8e247b964668752e9b500c0f66e26b0e1 Mon Sep 17 00:00:00 2001 From: David Cox Date: Mon, 13 Apr 2020 21:58:00 +0200 Subject: [PATCH] support for multi-part-forms for uploading images --- .../WebApi/ObjectDetectionController.cs | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/Alturos.Yolo.WebService/Communication/WebApi/ObjectDetectionController.cs b/src/Alturos.Yolo.WebService/Communication/WebApi/ObjectDetectionController.cs index 6539b9a..1acfca1 100644 --- a/src/Alturos.Yolo.WebService/Communication/WebApi/ObjectDetectionController.cs +++ b/src/Alturos.Yolo.WebService/Communication/WebApi/ObjectDetectionController.cs @@ -1,6 +1,8 @@ using Alturos.Yolo.Model; using Alturos.Yolo.WebService.Contract; using System; +using System.Net.Http; +using System.Threading.Tasks; using System.Web.Http; using System.Web.Http.Description; @@ -37,6 +39,40 @@ public IHttpActionResult Detect(byte[] imageData) } } + /// + /// Upload image as multi-part from data + /// This example can be tested with pastman. http://localhost:8080//ObjectDetection//Upload + /// Select Body->form data + /// Then select "file" from the key box before typing in (any) key name. Then select your image file in the value. + /// + /// + [HttpPost] + [Route("Upload")] + [ResponseType(typeof(YoloItem[]))] + public async Task Detect() + { + // Get the HTTP request + //HttpRequestMessage httpRequest = this.Request; + var provider = new MultipartMemoryStreamProvider(); + await Request.Content.ReadAsMultipartAsync(provider); + + // Get the first value from the form + var file = provider.Contents[0]; + + // Read file as bytes + var imageData = await file.ReadAsByteArrayAsync(); + try + { + // Pass byte array to wrapper + var items = this._objectDetection.Detect(imageData); + return Ok(items); + } + catch (Exception exception) + { + return InternalServerError(exception); + } + } + /// /// Detect object positions ///