public async Task <IHttpActionResult> Post() { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } var provider = new MultipartMemoryStreamProvider();//For now I read the file in memory, but that is not needed, it could be saved directly to disk. await Request.Content.ReadAsMultipartAsync(provider); foreach (var file in provider.Contents) { var buffer = await file.ReadAsByteArrayAsync(); string tempFileName = Path.GetTempFileName(); File.WriteAllBytes(tempFileName, buffer); OcrExecutor ocrExecutor = new OcrExecutor();//TODO: Should be SINGLETON, created using DI Container var ocrResult = await ocrExecutor.GetOcrResultAsync(tempFileName); File.Delete(tempFileName); return(Ok(ocrResult)); } return(Ok()); }
private static void RunOcrForAllClocksInParallel() { var dir = @"C:\Users\mihai.petrutiu\Downloads\clocks\clocks\"; string[] files = Directory.GetFiles(dir).Where(f => f.EndsWith("png")).ToArray(); OcrExecutor ocrExecutor = new OcrExecutor(); Parallel.For(0, files.Length, async(i) => { var result = await ocrExecutor.GetOcrResultAsync(files[i]); Console.WriteLine(result.Text); Console.WriteLine(i); }); Console.ReadKey(); }
/// <summary> /// NOT WORKING FOR SOME STRANGE REASON /// </summary> /// <returns></returns> public HttpResponseMessage Post() { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count < 1) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var filePath = HttpContext.Current.Server.MapPath("~/" + postedFile.FileName); postedFile.SaveAs(filePath); OcrExecutor executor = new OcrExecutor(); var r = executor.GetOcrResult(filePath); } return(Request.CreateResponse(HttpStatusCode.Created)); }