-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Image analytics documentation, samples, internalization (#2372)
* samples and documentation * addressing issue #1798 for the Image Analytics project.
- Loading branch information
Showing
21 changed files
with
1,052 additions
and
537 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
docs/samples/Microsoft.ML.Samples/Dynamic/ImageAnalytics/ConvertToGrayScale.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 |
---|---|---|
@@ -0,0 +1,57 @@ | ||
using System.IO; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class ConvertToGrayscaleExample | ||
{ | ||
// Sample that loads images from the file system, and converts them to grayscale. | ||
public static void ConvertToGrayscale() | ||
{ | ||
var mlContext = new MLContext(); | ||
|
||
// Downloading a few images, and an images.tsv file, which contains a list of the files from the dotnet/machinelearning/test/data/images/. | ||
// If you inspect the fileSystem, after running this line, an "images" folder will be created, containing 4 images, and a .tsv file | ||
// enumerating the images. | ||
var imagesDataFile = SamplesUtils.DatasetUtils.DownloadImages(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// | ||
// imagePath imageType | ||
// tomato.bmp tomato | ||
// banana.jpg banana | ||
// hotdog.jpg hotdog | ||
// tomato.jpg tomato | ||
|
||
var data = mlContext.Data.CreateTextLoader(new TextLoader.Arguments() | ||
{ | ||
Columns = new[] | ||
{ | ||
new TextLoader.Column("ImagePath", DataKind.TX, 0), | ||
new TextLoader.Column("Name", DataKind.TX, 1), | ||
} | ||
}).Read(imagesDataFile); | ||
|
||
var imagesFolder = Path.GetDirectoryName(imagesDataFile); | ||
// Image loading pipeline. | ||
var pipeline = mlContext.Transforms.LoadImages(imagesFolder, ("ImageObject", "ImagePath")) | ||
.Append(mlContext.Transforms.ConvertToGrayscale(("Grayscale", "ImageObject"))); | ||
|
||
var transformedData = pipeline.Fit(data).Transform(data); | ||
|
||
// The transformedData IDataView contains the loaded images column, and the grayscaled column. | ||
// Preview of the transformedData | ||
var transformedDataPreview = transformedData.Preview(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// The actual images, in the Grayscale column are of type System.Drawing.Bitmap. | ||
// | ||
// ImagePath Name ImageObject Grayscale | ||
// tomato.bmp tomato {System.Drawing.Bitmap} {System.Drawing.Bitmap} | ||
// banana.jpg banana {System.Drawing.Bitmap} {System.Drawing.Bitmap} | ||
// hotdog.jpg hotdog {System.Drawing.Bitmap} {System.Drawing.Bitmap} | ||
// tomato.jpg tomato {System.Drawing.Bitmap} {System.Drawing.Bitmap} | ||
|
||
} | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
docs/samples/Microsoft.ML.Samples/Dynamic/ImageAnalytics/ExtractPixels.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 |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.IO; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class ExtractPixelsExample | ||
{ | ||
// Sample that loads the images from the file system, resizes them (ExtractPixels requires a resizing operation), and extracts the | ||
// values of the pixels as a vector. | ||
public static void ExtractPixels() | ||
{ | ||
var mlContext = new MLContext(); | ||
|
||
// Downloading a few images, and an images.tsv file, which contains a list of the files from the dotnet/machinelearning/test/data/images/. | ||
// If you inspect the fileSystem, after running this line, an "images" folder will be created, containing 4 images, and a .tsv file | ||
// enumerating the images. | ||
var imagesDataFile = SamplesUtils.DatasetUtils.DownloadImages(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// | ||
// imagePath imageType | ||
// tomato.bmp tomato | ||
// banana.jpg banana | ||
// hotdog.jpg hotdog | ||
// tomato.jpg tomato | ||
|
||
var data = mlContext.Data.CreateTextLoader(new TextLoader.Arguments() | ||
{ | ||
Columns = new[] | ||
{ | ||
new TextLoader.Column("ImagePath", DataKind.TX, 0), | ||
new TextLoader.Column("Name", DataKind.TX, 1), | ||
} | ||
}).Read(imagesDataFile); | ||
|
||
var imagesFolder = Path.GetDirectoryName(imagesDataFile); | ||
// Image loading pipeline. | ||
var pipeline = mlContext.Transforms.LoadImages(imagesFolder, ("ImageObject", "ImagePath")) | ||
.Append(mlContext.Transforms.Resize("ImageObject",imageWidth: 100 , imageHeight: 100 )) | ||
.Append(mlContext.Transforms.ExtractPixels("Pixels", "ImageObject")); | ||
|
||
|
||
var transformedData = pipeline.Fit(data).Transform(data); | ||
|
||
// The transformedData IDataView contains the loaded images now | ||
//Preview of the transformedData | ||
var transformedDataPreview = transformedData.Preview(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// | ||
// ImagePath Name ImageObject "Pixels" | ||
// tomato.bmp tomato {System.Drawing.Bitmap} [ 255, 255, 255, ..... 232, 243, 226, ... | ||
// banana.jpg banana {System.Drawing.Bitmap} [ 255, 255, 255, ..... 90, 54, 43, ... | ||
// hotdog.jpg hotdog {System.Drawing.Bitmap} [ 255, 255, 255, ..... 132, 143, 126, ... | ||
// tomato.jpg tomato {System.Drawing.Bitmap} [ 255, 255, 255, ..... 16, 21, 23, ... | ||
|
||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
docs/samples/Microsoft.ML.Samples/Dynamic/ImageAnalytics/LoadImage.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System.IO; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class LoadImageExample | ||
{ | ||
// Loads the images of the imagesFolder into an IDataView. | ||
public static void LoadImage() | ||
{ | ||
var mlContext = new MLContext(); | ||
|
||
// Downloading a few images, and an images.tsv file, which contains a list of the files from the dotnet/machinelearning/test/data/images/. | ||
// If you inspect the fileSystem, after running this line, an "images" folder will be created, containing 4 images, and a .tsv file | ||
// enumerating the images. | ||
var imagesDataFile = SamplesUtils.DatasetUtils.DownloadImages(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// | ||
// imagePath imageType | ||
// tomato.bmp tomato | ||
// banana.jpg banana | ||
// hotdog.jpg hotdog | ||
// tomato.jpg tomato | ||
|
||
var data = mlContext.Data.CreateTextLoader(new TextLoader.Arguments() | ||
{ | ||
Columns = new[] | ||
{ | ||
new TextLoader.Column("ImagePath", DataKind.TX, 0), | ||
new TextLoader.Column("Name", DataKind.TX, 1), | ||
} | ||
}).Read(imagesDataFile); | ||
|
||
var imagesFolder = Path.GetDirectoryName(imagesDataFile); | ||
// Image loading pipeline. | ||
var pipeline = mlContext.Transforms.LoadImages(imagesFolder, ("ImageReal", "ImagePath")); | ||
var transformedData = pipeline.Fit(data).Transform(data); | ||
|
||
// The transformedData IDataView contains the loaded images now | ||
//Preview of the transformedData | ||
var transformedDataPreview = transformedData.Preview(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// The actual images, in the ImageReal column are of type System.Drawing.Bitmap. | ||
// | ||
// ImagePath Name ImageReal | ||
// tomato.bmp tomato {System.Drawing.Bitmap} | ||
// banana.jpg banana {System.Drawing.Bitmap} | ||
// hotdog.jpg hotdog {System.Drawing.Bitmap} | ||
// tomato.jpg tomato {System.Drawing.Bitmap} | ||
|
||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
docs/samples/Microsoft.ML.Samples/Dynamic/ImageAnalytics/ResizeImage.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 |
---|---|---|
@@ -0,0 +1,58 @@ | ||
using System.IO; | ||
using Microsoft.ML.Data; | ||
|
||
namespace Microsoft.ML.Samples.Dynamic | ||
{ | ||
public class ResizeImageExample | ||
{ | ||
// Example on how to load the images from the file system, and resize them. | ||
public static void ResizeImage() | ||
{ | ||
var mlContext = new MLContext(); | ||
|
||
// Downloading a few images, and an images.tsv file, which contains a list of the files from the dotnet/machinelearning/test/data/images/. | ||
// If you inspect the fileSystem, after running this line, an "images" folder will be created, containing 4 images, and a .tsv file | ||
// enumerating the images. | ||
var imagesDataFile = SamplesUtils.DatasetUtils.DownloadImages(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// | ||
// imagePath imageType | ||
// tomato.bmp tomato | ||
// banana.jpg banana | ||
// hotdog.jpg hotdog | ||
// tomato.jpg tomato | ||
|
||
var data = mlContext.Data.CreateTextLoader(new TextLoader.Arguments() | ||
{ | ||
Columns = new[] | ||
{ | ||
new TextLoader.Column("ImagePath", DataKind.TX, 0), | ||
new TextLoader.Column("Name", DataKind.TX, 1), | ||
} | ||
}).Read(imagesDataFile); | ||
|
||
var imagesFolder = Path.GetDirectoryName(imagesDataFile); | ||
// Image loading pipeline. | ||
var pipeline = mlContext.Transforms.LoadImages(imagesFolder, ("ImageReal", "ImagePath")) | ||
.Append(mlContext.Transforms.Resize("ImageReal", imageWidth: 100, imageHeight: 100)); | ||
|
||
|
||
var transformedData = pipeline.Fit(data).Transform(data); | ||
|
||
// The transformedData IDataView contains the loaded images now | ||
//Preview of the transformedData | ||
var transformedDataPreview = transformedData.Preview(); | ||
|
||
// Preview of the content of the images.tsv file | ||
// The actual images, in the ImageReal column are of type System.Drawing.Bitmap. | ||
// | ||
// ImagePath Name ImageReal | ||
// tomato.bmp tomato {System.Drawing.Bitmap} | ||
// banana.jpg banana {System.Drawing.Bitmap} | ||
// hotdog.jpg hotdog {System.Drawing.Bitmap} | ||
// tomato.jpg tomato {System.Drawing.Bitmap} | ||
|
||
} | ||
} | ||
} |
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
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
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
Oops, something went wrong.