简体   繁体   中英

load image from PC as stream

I am trying to load a picture from my PC as a raw image in order to use it with the Microsoft cognitive services emotion (UWP). below is a piece of my code:

        //Chose Image from PC
    private async void chosefile_Click(object sender, RoutedEventArgs e)
    {


        //Open Dialog
        FileOpenPicker open = new FileOpenPicker();
        open.ViewMode = PickerViewMode.Thumbnail;
        open.SuggestedStartLocation = PickerLocationId.Desktop;
        open.FileTypeFilter.Add(".jpg");
        open.FileTypeFilter.Add(".jpeg");
        open.FileTypeFilter.Add(".gif");
        open.FileTypeFilter.Add(".png");
        file = await open.PickSingleFileAsync();


        if (file != null)
        {//imagestream is declared as IRandomAccessStream.

            imagestream = await file.OpenAsync(Windows.Storage.FileAccessMode.Read);
            var image = new BitmapImage();
            image.SetSource(imagestream);
            imageView.Source = image;
        }
        else
        {
            //  
        }
    }

The part above works fine, it selects a photo from the pc (dialog box) and displays it in Image box.

    private async void analyse_Click(object sender, RoutedEventArgs e)
    {
        try
        {
            emotionResult = await emotionServiceClient.RecognizeAsync(imagestream.AsStream());
        }
        catch
        {
            output.Text = "something is wrong in stream";
        }

        try { 
            if(emotionResult!= null)
            {
                Scores score = emotionResult[0].Scores;
                output.Text = "Your emotions are: \n" +
                    "Happiness: " + score.Happiness + "\n" +
                    "Sadness: " + score.Sadness;
            }
        }
        catch
        {
         output.Text = "Something went wrong";
        }
    }

I think the error is due to imagestream.AsStream() imagestream is declared as IRandomAccessStream.

Can someone please tell me how to fix that part and if the error is in fact due to not loading the image correctly?

EDIT: Also is there a better way to do this, instead of using stream to pass the emotionServiceClient a saved file instead of a stream?

Why not use their example, instead of trying to hold the file in memory, why don't you hold a path, and then use the path to read the stream at the time.

https://www.microsoft.com/cognitive-services/en-us/Emotion-api/documentation/GetStarted

In there example;

using (Stream imageFileStream = File.OpenRead(imageFilePath))
                {
                    //
                    // Detect the emotions in the URL
                    //
                    emotionResult = await emotionServiceClient.RecognizeAsync(imageFileStream);
                    return emotionResult;
                }

So you would be capturing imageFilePath as the result of the open file dialog.

Your problem is that you've advanced the stream position by virtue of creating the BitmapImage , so your read position is at the end by the time you call emotionServiceClient.RecognizeAsync . So you'll need to 'rewind':

var stream = imagestream.AsStreamForRead();
stream.Position = 0;
emotionResult = await emotionServiceClient.RecognizeAsync(stream);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM