Enhancements.

This commit is contained in:
2024-08-01 17:25:17 -04:00
parent c4577b4374
commit 7f48834f23
2 changed files with 65 additions and 6 deletions

View File

@@ -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<String> Upload(String url,Stream stream)
{
try
@@ -78,6 +88,31 @@ namespace MarketData.CNNProcessing
return null;
}
}
/// <summary>
/// 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)
/// </summary>
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<String> Upload(String url)
{
try

View File

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