private ClassificationResults UseClassifier(Image frame) { var classificationResults = frame != null?_classifier?.Classify(frame.CloneAs <Rgb24>()) : null; var results = classificationResults?.Prediction?.Confidence > Threshold ? classificationResults : null; return(results); }
public static Bitmap CustomRun(string path) { string classificationResult = imageClassifier.Classify(new Bitmap(path)); string fileName = Path.GetFileNameWithoutExtension(path); string[] nameParts = fileName.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); fileName = nameParts[0]; if (classificationResult.Equals(fileName, StringComparison.CurrentCulture)) { return(BinaryTransformer.BoolToBitmap(imageClassifier.LastPattern)); } return(new Bitmap(10, 10)); }
public static string Run(string DataDirectoryName) { var statistics = new Dictionary <string, int>(); IImageBinarizer binarizer = new ImageBinarizer(); var directoryInfo = new DirectoryInfo(Environment.CurrentDirectory).Parent.Parent.Parent; var path = Path.Combine(directoryInfo.FullName, DataDirectoryName); directoryInfo = new DirectoryInfo(path); imageClassifier = new ImageClassifier(binarizer, Size * Size); //Train foreach (DirectoryInfo directory in directoryInfo.EnumerateDirectories()) { foreach (var file in directory.EnumerateFiles("*.bmp")) { string fileName = Path.GetFileNameWithoutExtension(file.FullName); imageClassifier.Train(new ImageUnit { Id = fileName, Image = new Bitmap(file.FullName) }); } } foreach (DirectoryInfo directory in directoryInfo.EnumerateDirectories()) { foreach (var file in directory.EnumerateFiles("*.bmp")) { string fileName = Path.GetFileNameWithoutExtension(file.FullName); var patternDirectory = directory.EnumerateDirectories().FirstOrDefault(p => p.Name == ("Randomized_" + fileName)); foreach (var patternFile in patternDirectory.EnumerateFiles("*.bmp")) { string patternFileName = Path.GetFileNameWithoutExtension(patternFile.FullName); string[] nameParts = patternFileName.Split(new[] { "_" }, StringSplitOptions.RemoveEmptyEntries); string percentValue = nameParts[1]; if (statistics.ContainsKey(percentValue) == false) { statistics.Add(percentValue, 0); } string classificationResult = imageClassifier.Classify(new Bitmap(patternFile.FullName)); if (classificationResult.Equals(fileName, StringComparison.CurrentCulture)) { statistics[percentValue] += 1; } } } } return(statistics.Aggregate("Classification result: \n\n", (current, statistic) => current + String.Format("Noise percent: {0}\n Matched: {1}\n\n", statistic.Key, statistic.Value))); }
public static ClassificationResults Classify <TPixel>(this ImageClassifier classifier, Image <TPixel> image) where TPixel : unmanaged, IPixel <TPixel> { return(classifier.Classify(image.ToFlatArrayMatchingInputShape(classifier.Signature, "Image"))); }
static void Main(string[] args) { //LogEvents.Subscribe(i => //{ // i.Operation.Id = ""; // Console.WriteLine(i.ToLogString()); //}, new[] //{ // typeof(PiTop4Board).Assembly, // typeof(FoundationPlate).Assembly, // typeof(ExpansionPlate).Assembly, // typeof(RoverRobot).Assembly, // typeof(StreamingCamera).Assembly, // typeof(Program).Assembly, //}); ImageClassifier.Register("onnx", () => new OnnxImageClassifier()); ImageClassifier classifier = null; var js = new XBoxController(); // using ` mjpg_streamer -i "input_uvc.so -d /dev/video0" -o output_http.so` // ==> http://pi-top.local:8080/?action=stream PiTop4Board.Instance.UseCamera(); using var rover = new RoverRobot(PiTop4Board.Instance.GetOrCreateExpansionPlate(), PiTop4Board.Instance.GetOrCreateCamera <StreamingCamera>(0), RoverRobotConfiguration.Default); var camControl = rover.TiltController; var motorControl = rover.MotionComponent as SteeringMotorController; rover.AllLightsOn(); rover.BlinkAllLights(); Observable.Interval(TimeSpan.FromMilliseconds(10)) .Select(_ => (X: js.LeftStick.X, Y: js.LeftStick.Y)) .DistinctUntilChanged() .Subscribe(stick => { var left = stick.X.WithDeadZone(-.5, .5, .3); var forward = stick.Y; motorControl.SetPower((forward + left) / 1.5, (forward - left) / 1.5); }); js.Events.OfType <ButtonEvent>().Where(e => e.Button == Button.A) .Subscribe(e => { if (e.Pressed) { rover.AllLightsOn(); } else { rover.AllLightsOff(); } }); js.Events.OfType <ButtonEvent>().Where(e => e.Button == Button.B && e.Pressed) .Subscribe(e => { rover.Camera.GetFrame().Save("/home/pi/shot.jpg"); }); js.Events.OfType <ButtonEvent>().Where(e => e.Button == Button.Y && e.Pressed) .Subscribe(e => { classifier?.Dispose(); var signatureFile = new FileInfo("/home/pi/models/pics/signature.json"); classifier = ImageClassifier.CreateFromSignatureFile(signatureFile); Console.WriteLine($"Loaded model from {signatureFile.FullName}"); }); js.Events.OfType <ButtonEvent>().Where(e => e.Button == Button.X && e.Pressed) .Subscribe(e => { var frame = rover.Camera.GetFrame().Focus(); var result = classifier?.Classify(frame.CloneAs <Rgb24>()); if (result is { }) { Console.WriteLine($"{result.Prediction.Label}"); } });
public static ClassificationResults Classify(this ImageClassifier classifier, Mat image) { return(classifier.Classify(image.ToFlatArrayMatchingInputShape(classifier.Signature, "Image"))); }