From 385b33d2344f640b8725026e043b5eeccf9f864d Mon Sep 17 00:00:00 2001 From: Citizen Date: Sat, 22 Jun 2024 18:17:01 +0200 Subject: [PATCH] Form name change and bugfix. --- src/AI.TensorFlow/AI.TensorFlow.csproj | 18 +++ src/AI.TensorFlow/Program.cs | 104 ++++++++++++++++++ src/ActuarialIntelligence.sln | 11 +- src/BasicTTS/BasicTTS.csproj | 21 +++- ...{Form1.Designer.cs => frmMain.Designer.cs} | 0 src/BasicTTS/{Form1.cs => frmMain.cs} | 0 src/BasicTTS/{Form1.resx => frmMain.resx} | 0 src/BasicTTS/packages.config | 3 + 8 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 src/AI.TensorFlow/AI.TensorFlow.csproj create mode 100644 src/AI.TensorFlow/Program.cs rename src/BasicTTS/{Form1.Designer.cs => frmMain.Designer.cs} (100%) rename src/BasicTTS/{Form1.cs => frmMain.cs} (100%) rename src/BasicTTS/{Form1.resx => frmMain.resx} (100%) diff --git a/src/AI.TensorFlow/AI.TensorFlow.csproj b/src/AI.TensorFlow/AI.TensorFlow.csproj new file mode 100644 index 0000000..7371318 --- /dev/null +++ b/src/AI.TensorFlow/AI.TensorFlow.csproj @@ -0,0 +1,18 @@ + + + + Exe + net6.0 + enable + enable + + + + + + + + + + + diff --git a/src/AI.TensorFlow/Program.cs b/src/AI.TensorFlow/Program.cs new file mode 100644 index 0000000..76fcb4b --- /dev/null +++ b/src/AI.TensorFlow/Program.cs @@ -0,0 +1,104 @@ +//using System; +//using System.IO; +//using System.Linq; +//using NumSharp; +//using OpenCvSharp; +//using Tensorflow; +//using Tensorflow.Hub; +//using static Tensorflow.Binding; + +//class Program +//{ +// static void Main(string[] args) +// { +// string modelPath = "ssd_mobilenet_v2"; +// string imagePath = "input_image.jpg"; +// string outputImagePath = "output_image.jpg"; + +// // Load the model from TensorFlow Hub +// var model = LoadModelFromHub("https://tfhub.dev/tensorflow/ssd_mobilenet_v2/2"); + +// // Preprocess the input image +// var inputTensor = PreprocessImage(imagePath); + +// // Run the model +// var outputs = model.Call(new Tensor[] { inputTensor }); + +// // Extract detection results +// var detectionBoxes = outputs[0]["detection_boxes"].numpy(); +// var detectionScores = outputs[0]["detection_scores"].numpy(); +// var detectionClasses = outputs[0]["detection_classes"].numpy(); + +// // Highlight detected objects +// HighlightObjects(imagePath, detectionBoxes, detectionScores, detectionClasses, outputImagePath); + +// Console.WriteLine($"Output image saved to {outputImagePath}"); +// } + +// static SavedModel LoadModelFromHub(string url) +// { +// var modulePath = Path.Combine(Path.GetTempPath(), "hub_module"); +// if (!Directory.Exists(modulePath)) +// { +// Directory.CreateDirectory(modulePath); +// Console.WriteLine($"Downloading model from {url} to {modulePath}"); +// // Manually download the model using HttpClient if tfhub.Module.download is unavailable +// using (var client = new System.Net.Http.HttpClient()) +// { +// var data = client.GetByteArrayAsync(url).Result; +// File.WriteAllBytes(modulePath, data); +// } +// } +// return SavedModel.Load(modulePath); +// } + +// static Tensor PreprocessImage(string imagePath) +// { +// var img = Cv2.ImRead(imagePath); +// Cv2.Resize(img, img, new Size(300, 300)); +// img.ConvertTo(img, MatType.CV_32F); +// img = img / 255.0f; // Normalize to [0,1] +// var imgArray = np.array(img.Reshape(new int[] { 1, 300, 300, 3 })); +// return ops.convert_to_tensor(imgArray); +// } + +// static void HighlightObjects(string imagePath, NDArray detectionBoxes, NDArray detectionScores, NDArray detectionClasses, string outputImagePath, float threshold = 0.5f) +// { +// var img = Cv2.ImRead(imagePath); +// int height = img.Height; +// int width = img.Width; + +// for (int i = 0; i < detectionScores.size; i++) +// { +// float score = detectionScores[i].ToSingle(); +// if (score < threshold) continue; + +// int classId = detectionClasses[i].ToInt32(); +// if (classId != 1 && classId != 43 && classId != 37) continue; // Filter only relevant classes + +// float[] box = detectionBoxes[i].ToArray(); + +// int yMin = (int)(box[0] * height); +// int xMin = (int)(box[1] * width); +// int yMax = (int)(box[2] * height); +// int xMax = (int)(box[3] * width); + +// Cv2.Rectangle(img, new Point(xMin, yMin), new Point(xMax, yMax), Scalar.Green, 2); +// string label = GetLabel(classId, score); +// Cv2.PutText(img, label, new Point(xMin, yMin - 10), HersheyFonts.HersheySimplex, 0.5, Scalar.Green, 2); +// } + +// Cv2.ImWrite(outputImagePath, img); +// } + +// static string GetLabel(int classId, float score) +// { +// return classId switch +// { +// 1 => $"Person: {score:P2}", +// 43 => $"Knife: {score:P2}", +// 37 => $"Gun: {score:P2}", +// _ => $"Unknown: {score:P2}" +// }; +// } +//} diff --git a/src/ActuarialIntelligence.sln b/src/ActuarialIntelligence.sln index c7a44e6..65f5304 100644 --- a/src/ActuarialIntelligence.sln +++ b/src/ActuarialIntelligence.sln @@ -77,10 +77,14 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AI.TTS.EXE", "AI.TTS.EXE\AI EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AI.TTS.Visual", "AI.TTS.Visual\AI.TTS.Visual.csproj", "{26F4EDC1-05BA-41FE-BE71-71DB0E3C5C8D}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicTTSAi", "BasicTTSAi\BasicTTSAi.csproj", "{34AD0CD5-C5D9-43B5-8290-C331434A343A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "BasicTTSAi", "BasicTTSAi\BasicTTSAi.csproj", "{34AD0CD5-C5D9-43B5-8290-C331434A343A}" EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "BasicTTS", "BasicTTS\BasicTTS.csproj", "{585E5FD4-CA9B-435E-9A8F-A5F6B2F5E144}" EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "TensorFlowRedis", "TensorFlowRedis", "{FDF73B9B-D72A-4563-8CAD-DE84FE4EB8CE}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AI.TensorFlow", "AI.TensorFlow\AI.TensorFlow.csproj", "{138BC271-053B-4C53-B6DF-9AEF8753CB85}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -189,6 +193,10 @@ Global {585E5FD4-CA9B-435E-9A8F-A5F6B2F5E144}.Debug|Any CPU.Build.0 = Debug|Any CPU {585E5FD4-CA9B-435E-9A8F-A5F6B2F5E144}.Release|Any CPU.ActiveCfg = Release|Any CPU {585E5FD4-CA9B-435E-9A8F-A5F6B2F5E144}.Release|Any CPU.Build.0 = Release|Any CPU + {138BC271-053B-4C53-B6DF-9AEF8753CB85}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {138BC271-053B-4C53-B6DF-9AEF8753CB85}.Debug|Any CPU.Build.0 = Debug|Any CPU + {138BC271-053B-4C53-B6DF-9AEF8753CB85}.Release|Any CPU.ActiveCfg = Release|Any CPU + {138BC271-053B-4C53-B6DF-9AEF8753CB85}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE @@ -218,6 +226,7 @@ Global {26F4EDC1-05BA-41FE-BE71-71DB0E3C5C8D} = {846214C2-E1CC-4C1E-8F09-86BB5582072B} {34AD0CD5-C5D9-43B5-8290-C331434A343A} = {846214C2-E1CC-4C1E-8F09-86BB5582072B} {585E5FD4-CA9B-435E-9A8F-A5F6B2F5E144} = {846214C2-E1CC-4C1E-8F09-86BB5582072B} + {138BC271-053B-4C53-B6DF-9AEF8753CB85} = {FDF73B9B-D72A-4563-8CAD-DE84FE4EB8CE} EndGlobalSection GlobalSection(ExtensibilityGlobals) = postSolution SolutionGuid = {7AFF1FB0-27A8-4765-900D-F53F52605824} diff --git a/src/BasicTTS/BasicTTS.csproj b/src/BasicTTS/BasicTTS.csproj index c09d5dd..c0e2bd3 100644 --- a/src/BasicTTS/BasicTTS.csproj +++ b/src/BasicTTS/BasicTTS.csproj @@ -18,7 +18,7 @@ - AnyCPU + x64 true full false @@ -109,21 +109,30 @@ + + ..\packages\Vlc.DotNet.Core.3.1.0\lib\net45\Vlc.DotNet.Core.dll + + + ..\packages\Vlc.DotNet.Core.Interops.3.1.0\lib\net45\Vlc.DotNet.Core.Interops.dll + + + ..\packages\Vlc.DotNet.Forms.3.1.0\lib\net45\Vlc.DotNet.Forms.dll + - + Form - - Form1.cs + + frmMain.cs - - Form1.cs + + frmMain.cs ResXFileCodeGenerator diff --git a/src/BasicTTS/Form1.Designer.cs b/src/BasicTTS/frmMain.Designer.cs similarity index 100% rename from src/BasicTTS/Form1.Designer.cs rename to src/BasicTTS/frmMain.Designer.cs diff --git a/src/BasicTTS/Form1.cs b/src/BasicTTS/frmMain.cs similarity index 100% rename from src/BasicTTS/Form1.cs rename to src/BasicTTS/frmMain.cs diff --git a/src/BasicTTS/Form1.resx b/src/BasicTTS/frmMain.resx similarity index 100% rename from src/BasicTTS/Form1.resx rename to src/BasicTTS/frmMain.resx diff --git a/src/BasicTTS/packages.config b/src/BasicTTS/packages.config index 6823006..23bc896 100644 --- a/src/BasicTTS/packages.config +++ b/src/BasicTTS/packages.config @@ -15,4 +15,7 @@ + + + \ No newline at end of file