forked from dotnet/machinelearning-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update samples to .NET 6 (dotnet#932)
* Update bike rental sample to .NET 6 * Update object detection sample to .NET 6
- Loading branch information
Showing
5 changed files
with
226 additions
and
255 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
242 changes: 113 additions & 129 deletions
242
...rp/getting-started/DeepLearning_ObjectDetection_Onnx/ObjectDetectionConsoleApp/Program.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,148 +1,132 @@ | ||
using System; | ||
using System.IO; | ||
using System.Collections.Generic; | ||
using System.Drawing; | ||
using System.Drawing; | ||
using System.Drawing.Drawing2D; | ||
using System.Linq; | ||
using Microsoft.ML; | ||
using ObjectDetection.YoloParser; | ||
using ObjectDetection.DataStructures; | ||
using ObjectDetection; | ||
using Microsoft.ML; | ||
|
||
var assetsRelativePath = @"../../../assets"; | ||
string assetsPath = GetAbsolutePath(assetsRelativePath); | ||
var modelFilePath = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx"); | ||
var imagesFolder = Path.Combine(assetsPath, "images"); | ||
var outputFolder = Path.Combine(assetsPath, "images", "output"); | ||
|
||
namespace ObjectDetection | ||
// Initialize MLContext | ||
MLContext mlContext = new MLContext(); | ||
|
||
try | ||
{ | ||
class Program | ||
// Load Data | ||
IEnumerable<ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder); | ||
IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images); | ||
|
||
// Create instance of model scorer | ||
var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext); | ||
|
||
// Use model to score data | ||
IEnumerable<float[]> probabilities = modelScorer.Score(imageDataView); | ||
|
||
// Post-process model output | ||
YoloOutputParser parser = new YoloOutputParser(); | ||
|
||
var boundingBoxes = | ||
probabilities | ||
.Select(probability => parser.ParseOutputs(probability)) | ||
.Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); | ||
|
||
// Draw bounding boxes for detected objects in each of the images | ||
for (var i = 0; i < images.Count(); i++) | ||
{ | ||
public static void Main() | ||
{ | ||
var assetsRelativePath = @"../../../assets"; | ||
string assetsPath = GetAbsolutePath(assetsRelativePath); | ||
var modelFilePath = Path.Combine(assetsPath, "Model", "TinyYolo2_model.onnx"); | ||
var imagesFolder = Path.Combine(assetsPath, "images"); | ||
var outputFolder = Path.Combine(assetsPath, "images", "output"); | ||
|
||
// Initialize MLContext | ||
MLContext mlContext = new MLContext(); | ||
|
||
try | ||
{ | ||
// Load Data | ||
IEnumerable<ImageNetData> images = ImageNetData.ReadFromFile(imagesFolder); | ||
IDataView imageDataView = mlContext.Data.LoadFromEnumerable(images); | ||
|
||
// Create instance of model scorer | ||
var modelScorer = new OnnxModelScorer(imagesFolder, modelFilePath, mlContext); | ||
|
||
// Use model to score data | ||
IEnumerable<float[]> probabilities = modelScorer.Score(imageDataView); | ||
|
||
// Post-process model output | ||
YoloOutputParser parser = new YoloOutputParser(); | ||
|
||
var boundingBoxes = | ||
probabilities | ||
.Select(probability => parser.ParseOutputs(probability)) | ||
.Select(boxes => parser.FilterBoundingBoxes(boxes, 5, .5F)); | ||
|
||
// Draw bounding boxes for detected objects in each of the images | ||
for (var i = 0; i < images.Count(); i++) | ||
{ | ||
string imageFileName = images.ElementAt(i).Label; | ||
IList<YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); | ||
|
||
DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects); | ||
|
||
LogDetectedObjects(imageFileName, detectedObjects); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.ToString()); | ||
} | ||
|
||
Console.WriteLine("========= End of Process..Hit any Key ========"); | ||
Console.ReadLine(); | ||
} | ||
string imageFileName = images.ElementAt(i).Label; | ||
IList<YoloBoundingBox> detectedObjects = boundingBoxes.ElementAt(i); | ||
|
||
public static string GetAbsolutePath(string relativePath) | ||
{ | ||
FileInfo _dataRoot = new FileInfo(typeof(Program).Assembly.Location); | ||
string assemblyFolderPath = _dataRoot.Directory.FullName; | ||
DrawBoundingBox(imagesFolder, outputFolder, imageFileName, detectedObjects); | ||
|
||
string fullPath = Path.Combine(assemblyFolderPath, relativePath); | ||
LogDetectedObjects(imageFileName, detectedObjects); | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex.ToString()); | ||
} | ||
|
||
return fullPath; | ||
} | ||
Console.WriteLine("========= End of Process..Hit any Key ========"); | ||
|
||
private static void DrawBoundingBox(string inputImageLocation, string outputImageLocation, string imageName, IList<YoloBoundingBox> filteredBoundingBoxes) | ||
{ | ||
Image image = Image.FromFile(Path.Combine(inputImageLocation, imageName)); | ||
|
||
var originalImageHeight = image.Height; | ||
var originalImageWidth = image.Width; | ||
|
||
foreach (var box in filteredBoundingBoxes) | ||
{ | ||
// Get Bounding Box Dimensions | ||
var x = (uint)Math.Max(box.Dimensions.X, 0); | ||
var y = (uint)Math.Max(box.Dimensions.Y, 0); | ||
var width = (uint)Math.Min(originalImageWidth - x, box.Dimensions.Width); | ||
var height = (uint)Math.Min(originalImageHeight - y, box.Dimensions.Height); | ||
|
||
// Resize To Image | ||
x = (uint)originalImageWidth * x / OnnxModelScorer.ImageNetSettings.imageWidth; | ||
y = (uint)originalImageHeight * y / OnnxModelScorer.ImageNetSettings.imageHeight; | ||
width = (uint)originalImageWidth * width / OnnxModelScorer.ImageNetSettings.imageWidth; | ||
height = (uint)originalImageHeight * height / OnnxModelScorer.ImageNetSettings.imageHeight; | ||
|
||
// Bounding Box Text | ||
string text = $"{box.Label} ({(box.Confidence * 100).ToString("0")}%)"; | ||
|
||
using (Graphics thumbnailGraphic = Graphics.FromImage(image)) | ||
{ | ||
thumbnailGraphic.CompositingQuality = CompositingQuality.HighQuality; | ||
thumbnailGraphic.SmoothingMode = SmoothingMode.HighQuality; | ||
thumbnailGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; | ||
|
||
// Define Text Options | ||
Font drawFont = new Font("Arial", 12, FontStyle.Bold); | ||
SizeF size = thumbnailGraphic.MeasureString(text, drawFont); | ||
SolidBrush fontBrush = new SolidBrush(Color.Black); | ||
Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1); | ||
|
||
// Define BoundingBox options | ||
Pen pen = new Pen(box.BoxColor, 3.2f); | ||
SolidBrush colorBrush = new SolidBrush(box.BoxColor); | ||
|
||
// Draw text on image | ||
thumbnailGraphic.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height); | ||
thumbnailGraphic.DrawString(text, drawFont, fontBrush, atPoint); | ||
|
||
// Draw bounding box on image | ||
thumbnailGraphic.DrawRectangle(pen, x, y, width, height); | ||
} | ||
} | ||
|
||
if (!Directory.Exists(outputImageLocation)) | ||
{ | ||
Directory.CreateDirectory(outputImageLocation); | ||
} | ||
|
||
image.Save(Path.Combine(outputImageLocation, imageName)); | ||
} | ||
string GetAbsolutePath(string relativePath) | ||
{ | ||
FileInfo _dataRoot = new FileInfo(typeof(Program).Assembly.Location); | ||
string assemblyFolderPath = _dataRoot.Directory.FullName; | ||
|
||
private static void LogDetectedObjects(string imageName, IList<YoloBoundingBox> boundingBoxes) | ||
{ | ||
Console.WriteLine($".....The objects in the image {imageName} are detected as below...."); | ||
string fullPath = Path.Combine(assemblyFolderPath, relativePath); | ||
|
||
foreach (var box in boundingBoxes) | ||
{ | ||
Console.WriteLine($"{box.Label} and its Confidence score: {box.Confidence}"); | ||
} | ||
return fullPath; | ||
} | ||
|
||
Console.WriteLine(""); | ||
void DrawBoundingBox(string inputImageLocation, string outputImageLocation, string imageName, IList<YoloBoundingBox> filteredBoundingBoxes) | ||
{ | ||
Image image = Image.FromFile(Path.Combine(inputImageLocation, imageName)); | ||
|
||
var originalImageHeight = image.Height; | ||
var originalImageWidth = image.Width; | ||
|
||
foreach (var box in filteredBoundingBoxes) | ||
{ | ||
// Get Bounding Box Dimensions | ||
var x = (uint)Math.Max(box.Dimensions.X, 0); | ||
var y = (uint)Math.Max(box.Dimensions.Y, 0); | ||
var width = (uint)Math.Min(originalImageWidth - x, box.Dimensions.Width); | ||
var height = (uint)Math.Min(originalImageHeight - y, box.Dimensions.Height); | ||
|
||
// Resize To Image | ||
x = (uint)originalImageWidth * x / OnnxModelScorer.ImageNetSettings.imageWidth; | ||
y = (uint)originalImageHeight * y / OnnxModelScorer.ImageNetSettings.imageHeight; | ||
width = (uint)originalImageWidth * width / OnnxModelScorer.ImageNetSettings.imageWidth; | ||
height = (uint)originalImageHeight * height / OnnxModelScorer.ImageNetSettings.imageHeight; | ||
|
||
// Bounding Box Text | ||
string text = $"{box.Label} ({(box.Confidence * 100).ToString("0")}%)"; | ||
|
||
using (Graphics thumbnailGraphic = Graphics.FromImage(image)) | ||
{ | ||
thumbnailGraphic.CompositingQuality = CompositingQuality.HighQuality; | ||
thumbnailGraphic.SmoothingMode = SmoothingMode.HighQuality; | ||
thumbnailGraphic.InterpolationMode = InterpolationMode.HighQualityBicubic; | ||
|
||
// Define Text Options | ||
Font drawFont = new Font("Arial", 12, FontStyle.Bold); | ||
SizeF size = thumbnailGraphic.MeasureString(text, drawFont); | ||
SolidBrush fontBrush = new SolidBrush(Color.Black); | ||
Point atPoint = new Point((int)x, (int)y - (int)size.Height - 1); | ||
|
||
// Define BoundingBox options | ||
Pen pen = new Pen(box.BoxColor, 3.2f); | ||
SolidBrush colorBrush = new SolidBrush(box.BoxColor); | ||
|
||
// Draw text on image | ||
thumbnailGraphic.FillRectangle(colorBrush, (int)x, (int)(y - size.Height - 1), (int)size.Width, (int)size.Height); | ||
thumbnailGraphic.DrawString(text, drawFont, fontBrush, atPoint); | ||
|
||
// Draw bounding box on image | ||
thumbnailGraphic.DrawRectangle(pen, x, y, width, height); | ||
} | ||
} | ||
|
||
if (!Directory.Exists(outputImageLocation)) | ||
{ | ||
Directory.CreateDirectory(outputImageLocation); | ||
} | ||
|
||
image.Save(Path.Combine(outputImageLocation, imageName)); | ||
} | ||
|
||
void LogDetectedObjects(string imageName, IList<YoloBoundingBox> boundingBoxes) | ||
{ | ||
Console.WriteLine($".....The objects in the image {imageName} are detected as below...."); | ||
|
||
foreach (var box in boundingBoxes) | ||
{ | ||
Console.WriteLine($"{box.Label} and its Confidence score: {box.Confidence}"); | ||
} | ||
|
||
Console.WriteLine(""); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+0 Bytes
(100%)
.../getting-started/Forecasting_BikeSharingDemand/BikeDemandForecasting/Data/DailyDemand.mdf
Binary file not shown.
Oops, something went wrong.