From 7f48834f23c5a709baa336d2059586cbc336bf77 Mon Sep 17 00:00:00 2001 From: Sean Date: Thu, 1 Aug 2024 17:25:17 -0400 Subject: [PATCH] Enhancements. --- MarketDataLib/CNNProcessing/CNNClient.cs | 37 +++++++++++++++++++++- MarketDataLib/CNNProcessing/ImageHelper.cs | 34 +++++++++++++++++--- 2 files changed, 65 insertions(+), 6 deletions(-) diff --git a/MarketDataLib/CNNProcessing/CNNClient.cs b/MarketDataLib/CNNProcessing/CNNClient.cs index 8e7f379..de64093 100644 --- a/MarketDataLib/CNNProcessing/CNNClient.cs +++ b/MarketDataLib/CNNProcessing/CNNClient.cs @@ -11,7 +11,7 @@ namespace MarketData.CNNProcessing { public class CNNClient { - public enum Model{resnet50,inception,vgg16,lenet5,ping}; + public enum Model{resnet50,resnet50B,inception,vgg16,lenet5,ping}; private static readonly string Alive="Alive"; private readonly HttpClient client = new HttpClient(); private string baseUrl; @@ -42,6 +42,15 @@ namespace MarketData.CNNProcessing } } +// This method is used to process an image through PIL. All images that are being used in training should ultimately be processed through +// PIL (Python Image Library) so that images can be normalized by python prior to training. We do this because we use PIL during the +// prediction process and we want all images used in training/validation and all images used for prediction to be touched by and processed +// by PIL + public Stream ProcessImage(Stream stream) + { + return UploadImage(baseUrl+"/process_image", stream); + } + public bool Ping() { try @@ -56,6 +65,7 @@ namespace MarketData.CNNProcessing return false; } } + private async Task Upload(String url,Stream stream) { try @@ -78,6 +88,31 @@ namespace MarketData.CNNProcessing return null; } } + /// + /// This method uploads an image stream to the specified url and receives the processed image back. + /// The processed image simply processes the image through PIL (Python Image Library) + /// + private Stream UploadImage(String url,Stream stream) + { + try + { + using (HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, url)) + { + int streamEnd = Convert.ToInt32(stream.Length); + byte[] byteArray = new byte[streamEnd]; + stream.Read(byteArray, 0, streamEnd); + request.Content=new ByteArrayContent(byteArray); + HttpResponseMessage response = client.SendAsync(request).GetAwaiter().GetResult(); + return response.Content.ReadAsStreamAsync().GetAwaiter().GetResult(); + } + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,String.Format("Exception encountered: {0}",exception.ToString())); + return null; + } + } + private async Task Upload(String url) { try diff --git a/MarketDataLib/CNNProcessing/ImageHelper.cs b/MarketDataLib/CNNProcessing/ImageHelper.cs index 01b7da1..b8e86d7 100644 --- a/MarketDataLib/CNNProcessing/ImageHelper.cs +++ b/MarketDataLib/CNNProcessing/ImageHelper.cs @@ -27,6 +27,11 @@ namespace MarketData.CNNProcessing } public void Dispose() + { + DisposeAll(); + } + + private void DisposeAll() { if(null!=bitmap) { @@ -55,20 +60,38 @@ namespace MarketData.CNNProcessing return copy; } + public bool LoadImage(Stream stream) + { + try + { + DisposeAll(); + Image image=Image.FromStream(stream); + bitmap = new Bitmap(image); + height = bitmap.Height; + width = bitmap.Width; + return true; + } + catch(Exception exception) + { + MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString()); + return false; + } + } + public bool LoadImage(string pathFileName) { Stream bitmapStream = null; try { - bitmapStream = File.Open(pathFileName,FileMode.Open); - Image image = Image.FromStream(bitmapStream); - bitmap=new Bitmap(image); + DisposeAll(); + bitmap = new Bitmap(pathFileName); width=bitmap.Width; height=bitmap.Height; return true; } - catch(Exception) + catch(Exception exception) { + MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString()); return false; } finally @@ -125,8 +148,9 @@ namespace MarketData.CNNProcessing graphics.Dispose(); return true; } - catch(Exception) + catch(Exception exception) { + MDTrace.WriteLine(LogLevel.DEBUG,exception.ToString()); return false; } finally