Image classification with Pytorch and C# ML - c#

I m trying to load an image classification model made with pytorch into a c# aplication. In order to acomplish this task i use an async task, but when i run the program it throws me a null reference exception because it seams it doesn't load the model.
using System;
using System.Threading.Tasks;
using Windows.AI.MachineLearning;
using Windows.Graphics.Imaging;
using Windows.Media;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Streams;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media.Imaging;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=402352&clcid=0x409
namespace AI_Test1
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class MainPage : Page
{
// All the required fields declaration
private Model modelGen;
private Input image = new Input();
private Output results;
private StorageFile selectedStorageFile;
private string label = "";
private float probability = 0;
private Helper helper = new Helper();
public enum Labels
{
Has_Lighter,
Has_No_Lighter,
}
public MainPage()
{
this.InitializeComponent();
_ = loadModel();
}
private async Task loadModel()
{
// Get an access the ONNX model and save it in memory.
StorageFile modelFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appx:///Assets/LTM-Classic_Model.onnx"));
// Instantiate the model.
modelGen = await Model.CreateFromStreamAsync(modelFile);
}
private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
{
if (!await getImage())
{
return;
}
// After the click event happened and an input selected, begin the model execution.
// Bind the model input
await imageBind();
// Model evaluation
await evaluate();
// Extract the results
ExtractResult();
// Display the results
displayResult();
}
private async Task<bool> getImage()
{
try
{
// Trigger file picker to select an image file
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
fileOpenPicker.FileTypeFilter.Add(".jpg");
fileOpenPicker.FileTypeFilter.Add(".png");
fileOpenPicker.ViewMode = PickerViewMode.Thumbnail;
selectedStorageFile = await fileOpenPicker.PickSingleFileAsync();
if (selectedStorageFile == null)
{
return false;
}
}
catch (Exception)
{
return false;
}
return true;
}
private async Task imageBind()
{
UIPreviewImage.Source = null;
try
{
SoftwareBitmap softwareBitmap;
using (IRandomAccessStream stream = await selectedStorageFile.OpenAsync(FileAccessMode.Read))
{
// Create the decoder from the stream
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(stream);
// Get the SoftwareBitmap representation of the file in BGRA8 format
softwareBitmap = await decoder.GetSoftwareBitmapAsync();
softwareBitmap = SoftwareBitmap.Convert(softwareBitmap, BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
}
// Display the image
SoftwareBitmapSource imageSource = new SoftwareBitmapSource();
await imageSource.SetBitmapAsync(softwareBitmap);
UIPreviewImage.Source = imageSource;
// Encapsulate the image within a VideoFrame to be bound and evaluated
VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
// Resize the image size to 32x32
inputImage = await helper.CropAndDisplayInputImageAsync(inputImage);
// Bind the model input with image
ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);
image.input = imageTensor;
// Encapsulate the image within a VideoFrame to be bound and evaluated
//VideoFrame inputImage = VideoFrame.CreateWithSoftwareBitmap(softwareBitmap);
// bind the input image
//ImageFeatureValue imageTensor = ImageFeatureValue.CreateFromVideoFrame(inputImage);
//image.modelInput = imageTensor;
}
catch (Exception )
{
}
}
private void ExtractResult()
{
// Retrieve the results of evaluation
var mResult = results.output as TensorFloat;
// convert the result to vector format
var resultVector = mResult.GetAsVectorView();
probability = 0;
int index = 0;
// find the maximum probability
for (int i = 0; i < resultVector.Count; i++)
{
var elementProbability = resultVector[i];
if (elementProbability > probability)
{
index = i;
}
}
label = ((Labels)index).ToString();
}
private void displayResult()
{
displayOutput.Text = label;
}
private async Task evaluate()
{
results = await modelGen.EvaluateAsync(image);
}
}
}

According to what I could understand from your code, the modelGen is not initialized before you have called the evaluate() method. Apparantly, you have created a method loadModel() for this purpose, so I am guessing that you need to call the loadmodel() before you call evaluate().
So edit this portion of the code
private async void OpenFileButton_Click(object sender, RoutedEventArgs e)
{
if (!await getImage())
{
return;
}
// After the click event happened and an input selected, begin the model execution.
// Bind the model input
await imageBind();
await loadModel(); // EDITED: Load Model here
// Model evaluation
await evaluate();
// Extract the results
ExtractResult();
// Display the results
displayResult();
}

Related

Is it possible to convert an Image in IFormFile

I'm trying to store a model informations in my Data Base, this informations contain an image, so I created a simple view with some entrys, with a media picker button to pick it and an image witch display the picked one.
This is the button method and the store method:
async void Button_Clicked(System.Object sender, System.EventArgs e)
{
var result = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Please pick a photo"
});
if (result != null)
{
var stream = await result.OpenReadAsync();
resultImage.Source = ImageSource.FromStream(() => stream);
}
}
public async Task SaveMachine()
{
var machine = new Machine
{
Machine_Name = nom.Text,
Machine_Qr = qr.Text,
Files = resultImage
};
await _rest.AddMachine(machine);
await Shell.Current.GoToAsync("..");
}
But I can't create Files = resultImage because the Files in the model is in IFormFile.

Load an Image to a Canvas Control from a File Picker

What I'm trying to do in a UWP app with Win2D:
User pressed a button to add an image and picks their file.
That file gets loaded as a resource for a Canvas Control.
The image then gets rendered to the current drawing session
When the button is clicked:
private async void btnAddPicture_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
overlayPictureFile = await picker.PickSingleFileAsync();
if (overlayPictureFile == null)
{
txbNotification.Text = "File Picking cancelled";
return;
}
else
{
txbNotification.Text = "Picture Loaded";
}
using (IRandomAccessStream stream = await overlayPictureFile.OpenAsync(FileAccessMode.Read))
{
var device = new CanvasDevice();
createdBitmap = await CanvasBitmap.LoadAsync(device, stream);
}
}
In the drawing function:
void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
if (createdBitmap != null)
{
args.DrawingSession.DrawImage(createdBitmap, Drawing.FindDefaultRect());
}
drawingCanvas.Invalidate();
}
Everything will compile but the moment I press the button to add an image it breaks here.
#if DEBUG && !DISABLE_XAML_GENERATED_BREAK_ON_UNHANDLED_EXCEPTION
UnhandledException += (sender, e) =>
{
if (global::System.Diagnostics.Debugger.IsAttached) global::System.Diagnostics.Debugger.Break();
};
#endif
I'm already loading some image in this, but those are all part of the program and are created before the canvas is created with these. Not sure how to do that with ones the user picks.
private void drawingCanvas_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
private async Task CreateResourcesAsync(CanvasControl sender)
{
logo = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Pictures/Logo_BlackBorders.png"));
}
Update:
Where I currently am drawing things. This is the canvas I'm trying to add the image to.
void CanvasControl_Draw(CanvasControl sender, CanvasDrawEventArgs args)
{
//Drawing a bunch of stuff
}
private void drawingCanvas_CreateResources(CanvasControl sender, Microsoft.Graphics.Canvas.UI.CanvasCreateResourcesEventArgs args)
{
args.TrackAsyncAction(CreateResourcesAsync(sender).AsAsyncAction());
}
private async Task CreateResourcesAsync(CanvasControl sender)
{
logo = await CanvasBitmap.LoadAsync(sender, new Uri("ms-appx:///Assets/Pictures/Logo.png"));
}
Load an Image to a Canvas Control from a File Picker
For your scenario, you could get CanvasDrawingSession with CreateDrawingSession method. And then use this drawingsession to draw picked image to current CanvasControl.
For example.
private async void btnAddPicture_Click(object sender, RoutedEventArgs e)
{
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.FileTypeFilter.Add(".png");
picker.FileTypeFilter.Add(".jpg");
picker.FileTypeFilter.Add(".jpeg");
var overlayPictureFile = await picker.PickSingleFileAsync();
if (overlayPictureFile == null)
{
return;
}
else
{
}
using (IRandomAccessStream stream = await overlayPictureFile.OpenAsync(FileAccessMode.Read))
{
//get canvascontrol's Device property.
CanvasDevice device = drawingCanvas.Device;
createdBitmap = await CanvasBitmap.LoadAsync(device, stream);
//use device property to make renderer
var renderer = new CanvasRenderTarget(device,
createdBitmap.SizeInPixels.Width,
createdBitmap.SizeInPixels.Height, createdBitmap.Dpi);
//make ds with above renderer.
using (var ds = renderer.CreateDrawingSession())
{
ds.DrawImage(createdBitmap, 0, 0);
}
}
}

Call on an Async Task from main activity to a fragment

I have an async task that needs to initilize on a button click inside my bottomsheet fragment. However when I call on the method from my bottomsheet fragment with
private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).Routing();
}
I get the error:
CS7036 There is no argument given that corresponds to the required formal parameter 'p' of 'MainActivity.Routing(MapPoint)' Maps.
The code below is my method for getting a route when clicking on a mappoint on the map in my mainactivity.
try
{
_textToSpeech = new TextToSpeech(this, this, "com.google.android.tts");
Stop stop0 = new Stop(_startPoint) { Name = "Starting Point" };
// Stop stop1 = new Stop(_endPoint) { Name = "EndPoint" };
if (_endPoint == null)
{
await _geocoder.ReverseGeocodeAsync(p);
_endPoint = p;
RouteTask routeTask = await RouteTask.CreateAsync(_routingUri);
RouteParameters routingParameters = await routeTask.CreateDefaultParametersAsync();
List<Stop> stops = new List<Stop> { new Stop(_startPoint), new Stop(_endPoint) };
routingParameters.SetStops(stops);
RouteResult result = await routeTask.SolveRouteAsync(routingParameters);
Route firstRoute = result.Routes.First();
_routeAheadGraphic = new Graphic(firstRoute.RouteGeometry) { Symbol = new SimpleLineSymbol(SimpleLineSymbolStyle.Solid, System.Drawing.Color.Blue, 4) };
SimpleLineSymbol(SimpleLineSymbolStyle.Dash, System.Drawing.Color.Red, 2) }; _myMapView.GraphicsOverlays[0].Graphics.Add(_routeAheadGraphic);
await _myMapView.SetViewpointGeometryAsync(firstRoute.RouteGeometry, 100);
return;
}
If the mappoint is not available in Fragment, you could define a public method in Main activity first , call Routing(mapPoint) inside that method .
Main Activity
public void MyMethod(){
Routing(mapPoint);
}
Fragment
private async void _navigationButton_Click(object sender, EventArgs e)
{
await ((MainActivity)Activity).MyMethod();
}

How to acess pictures Xamarin.Forms,SQLite, C#

i am creating mobile application where user can post adverts with pictures and text. I am using Media.Plugin by James Montemagno. That works very well. Also the user should be able to tap on the picture to review and possibly delete. I am not sure how to access these pictures, i dont know where they are stored so even if i would want to delete them i am not sure how to access each picture. COuld you please help me?
The user is able to take several images and then they are displayed on the main page. I would like the user to be able to review them once again and then if he continues with them to the second page he will upload some other data and on the third page it should all display together. How can i Display pictures on the third page?
public partial class AddingPage : ContentPage
{
AdLogEntry adLogEntry = new AdLogEntry();
//CameraService cameraService = new CameraService();
public byte[] imageAsBytes;
public string pathLabel;
private const int MaxColumns = 3;
private int _currentRow = 0;
private int _currentColumn = 0;
public AddingPage()
{
InitializeComponent();
}
protected override async void OnAppearing()
{
base.OnAppearing();
await MainProgressBar.ProgressTo(0, 250, Easing.Linear); ;
}
private async void NextStep_Clicked(object sender, EventArgs e)
{
await Navigation.PushAsync(new Informations());
}
private async void TakePicture_Clicked(object sender, EventArgs e)
{
await TakePicture();
}
private async Task TakePicture()
{
MediaFile file = null;
await CrossMedia.Current.Initialize();
if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsTakePhotoSupported)
{
await DisplayAlert("Nemáme přístup", "Nelze nalézt kameru", "OK");
return;
}
var imageSource = await DisplayActionSheet("Foto", "Cancel", null, new string[] { "Pořídit novou fotku", "Nahrát foto z galerie" });
var photoName = Guid.NewGuid().ToString() + ".jpg";
switch (imageSource)
{
case "Pořídit novou fotku":
file = await CrossMedia.Current.TakePhotoAsync(new StoreCameraMediaOptions
{
SaveToAlbum = true,
Directory = "Photos",
Name = photoName
});
//Get the public album path
var aPpath = file.AlbumPath;
//Get private path
var path = file.Path;
break;
case "Nahrát foto z galerie":
file = await CrossMedia.Current.PickPhotoAsync();
//Directory.GetFiles( "Photos");
break;
default:
break;
}
if (file == null)
return;
// Photo => Grid
_currentColumn++;
if (_currentColumn > MaxColumns - 0)
{
_currentColumn++;
_currentRow++;
// Add a new row definition by copying the first row.
ImageGridContainer.RowDefinitions.Add(ImageGridContainer.RowDefinitions[0]);
}
var newImage = new Image()
{
Source = ImageSource.FromFile(file.Path),
VerticalOptions = LayoutOptions.FillAndExpand,
HorizontalOptions = LayoutOptions.FillAndExpand,
Aspect = Aspect.AspectFill,
Scale = 0
};
ImageGridContainer.Children.Add(newImage, _currentColumn, _currentRow);
await Task.Delay(250);
await newImage.ScaleTo(1, 250, Easing.SpringOut);
}

Video recorder app in Windows Phone 8.1

I am going to write a Video Recorder app in Windows Phone 8.1 (RT).
I used the example provided in Windows Reference.
public sealed partial class CamcorderMainPage : Page
{
StatusBar StatusBarObject = Windows.UI.ViewManagement.StatusBar.GetForCurrentView();
string StatusBarHeader = "Firefly Moments";
MediaCaptureInitializationSettings _captureInitSettings;
List<Windows.Devices.Enumeration.DeviceInformation> _deviceList;
Windows.Media.MediaProperties.MediaEncodingProfile _profile;
Windows.Media.Capture.MediaCapture _mediaCapture;
bool _recording = false;
bool _previewing = false;
string NoCameraError = "No camera device is found ";
public CamcorderMainPage()
{
this.InitializeComponent();
this.Loaded += CamcorderMainPage_Loaded;
}
void CamcorderMainPage_Loaded(object sender, RoutedEventArgs e)
{
//throw new NotImplementedException();
EnumerateCameras();
}
private async void EnumerateCameras()
{
var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(
Windows.Devices.Enumeration.DeviceClass.VideoCapture);
_deviceList = new List<Windows.Devices.Enumeration.DeviceInformation>();
// Add the devices to deviceList
if (devices.Count > 0)
{
for (var i = 0; i < devices.Count; i++)
{
_deviceList.Add(devices[i]);
}
InitCaptureSettings();
InitMediaCapture();
// rootPage.NotifyUser("Initialization complete.", NotifyType.StatusMessage);
}
else
{
StatusBarObject.ProgressIndicator.Text = NoCameraError;
//rootPage.NotifyUser("No camera device is found ", NotifyType.ErrorMessage);
}
}
private void InitCaptureSettings()
{
_captureInitSettings = null;
_captureInitSettings = new Windows.Media.Capture.MediaCaptureInitializationSettings();
_captureInitSettings.AudioDeviceId = "";
_captureInitSettings.VideoDeviceId = "";
_captureInitSettings.StreamingCaptureMode = Windows.Media.Capture.StreamingCaptureMode.AudioAndVideo;
_captureInitSettings.PhotoCaptureSource = Windows.Media.Capture.PhotoCaptureSource.VideoPreview;
if (_deviceList.Count > 0)
_captureInitSettings.VideoDeviceId = _deviceList[0].Id;
}
// Create a profile.
private void CreateProfile()
{
_profile = Windows.Media.MediaProperties.MediaEncodingProfile.CreateMp4(
Windows.Media.MediaProperties.VideoEncodingQuality.Qvga);
}
// Create and initialze the MediaCapture object.
public async void InitMediaCapture()
{
_mediaCapture = null;
_mediaCapture = new Windows.Media.Capture.MediaCapture();
// Set the MediaCapture to a variable in App.xaml.cs to handle suspension.
(App.Current as App).MediaCapture = _mediaCapture;
await _mediaCapture.InitializeAsync(_captureInitSettings);
CreateProfile();
}
// Start the video capture.
private async void StartMediaCaptureSession()
{
var storageFile = await Windows.Storage.KnownFolders.VideosLibrary.CreateFileAsync(
"cameraCapture.mp4", Windows.Storage.CreationCollisionOption.GenerateUniqueName);
await _mediaCapture.StartRecordToStorageFileAsync(_profile, storageFile);
_recording = true;
}
// Stop the video capture.
private async void StopMediaCaptureSession()
{
await _mediaCapture.StopRecordAsync();
_recording = false;
(App.Current as App).IsRecording = false;
}
private async void ShowFireFlyStatusBar()
{
//this will show the Status Bar
Windows.UI.ViewManagement.ApplicationView.GetForCurrentView().SetDesiredBoundsMode(ApplicationViewBoundsMode.UseVisible);
StatusBarObject.ProgressIndicator.Text = StatusBarHeader;
StatusBarObject.ProgressIndicator.ProgressValue = 0;
StatusBarObject.ForegroundColor = Colors.MintCream;
StatusBarObject.BackgroundColor = Color.FromArgb(255, 166, 62, 59);
StatusBarObject.BackgroundOpacity = .6;
await StatusBarObject.ProgressIndicator.ShowAsync();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override void OnNavigatedTo(NavigationEventArgs e)
{
ShowFireFlyStatusBar();
}
private void Camcorder_StartCapture_Click(object sender, RoutedEventArgs e)
{
StartMediaCaptureSession();
}
private void Camcorder_StopCapture_Click(object sender, RoutedEventArgs e)
{
StopMediaCaptureSession();
}
}
Its working fine as I am getting the video file from Photos App .
Now how to enable live preview while recording in app? Which control to use for it?
You're looking for the CaptureElement. Once you add it to your XAML, connect it to your MediaCapture object and start preview like so:
PreviewControl.Source = _mediaCapture;
await _mediaCapture.StartPreviewAsync();
Have a look at the UniversalCameraSample posted on the Microsoft github page for more information. It targets Windows 10, but most of the patterns should work on 8/8.1 as well.

Categories