Skip to content

Instantly share code, notes, and snippets.

@Crisfole
Created January 25, 2015 16:19
Show Gist options
  • Save Crisfole/bb636955891327f8b057 to your computer and use it in GitHub Desktop.
Save Crisfole/bb636955891327f8b057 to your computer and use it in GitHub Desktop.
Demo Unhelpful Error
<Query Kind="Program">
<Reference>&lt;RuntimeDirectory&gt;\System.Drawing.dll</Reference>
<NuGetReference>Accord.Imaging</NuGetReference>
<Namespace>Accord.Imaging</Namespace>
<Namespace>System.Drawing</Namespace>
<Namespace>System.Drawing.Imaging</Namespace>
<Namespace>Accord.MachineLearning</Namespace>
</Query>
void Main()
{
var trainingDirectory = @"PATH TO UNZIPPED `train` DIRECTORY";
var images = LabeledImages.FromDirectory(trainingDirectory).ToArray();
var binarySplit = new BinarySplit(32);
var bow = new BagOfVisualWords(binarySplit);
bow.Compute(images.Select(i => i.Item).Take(50).ToArray());
bow.Dump();
}
public interface ILabeledItem<T> {
string Category { get; }
T Item { get; }
}
public class LabeledImages : ILabeledItem<Bitmap> {
public LabeledImages(string category, string filename) {
Category = category;
Filename = filename;
}
public string Category { get; private set; }
public string Filename { get; private set; }
public Bitmap Item {
get {
using (var sourceImage = Image.FromFile(Filename)) {
var targetImage = new Bitmap(sourceImage.Width, sourceImage.Height, PixelFormat.Format32bppArgb);
using (var canvas = Graphics.FromImage(targetImage)) {
canvas.DrawImageUnscaled(sourceImage, 0, 0);
}
return targetImage;
}
}
}
public static IEnumerable<LabeledImages> FromDirectory(string path) {
return Directory.EnumerateDirectories(path)
.SelectMany(dir => Directory.EnumerateFiles(dir)
.Select(file => new LabeledImages(Path.GetFileName(dir), file)));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment