<Capabilities> <Capability Name="internetClientServer" /> <DeviceCapability Name="webcam" /> </Capabilities>
<ScrollViewer> <StackPanel Orientation="Vertical" HorizontalAlignment="Center"> <StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Margin="0,10,0,10"> <Button x:Name="btnStartPreview" Background="Gray" Click="btnStartPreview_Click" Margin="0,0,0,0" Content="Start preview"/> <Button x:Name="btnTakePhoto" Background="Gray" Click="btnTakePhoto_Click" Margin="10,0,0,0" Content="Take a photo"/> <ProgressRing x:Name="progring" IsActive="False" Width="25" Height="25" Margin="10,0,0,0" /> </StackPanel> <Grid> <Image x:Name="captureImage" Width="400" Height="400" Visibility="Visible"/> <CaptureElement x:Name="previewElement" Width="400" Height="400" Visibility="Visible"/> </Grid> </StackPanel> </ScrollViewer>
using System.Threading.Tasks; using Windows.Media.Capture; using Windows.UI.Xaml.Media.Imaging; using Windows.Media.MediaProperties; using Windows.Storage.Streams; using Microsoft.ProjectOxford.Emotion; using Microsoft.ProjectOxford.Emotion.Contract;
MediaCapture mediaCapture; bool isPreviewing = false; Emotion[] emotionResult; public System.Collections.ObjectModel.ObservableCollection<Emotion> emo = new System.Collections.ObjectModel.ObservableCollection<Emotion>();
private async void Page_Loaded(object sender, RoutedEventArgs e) { MediaCaptureInitializationSettings set = new MediaCaptureInitializationSettings(); set.StreamingCaptureMode = StreamingCaptureMode.Video; mediaCapture = new MediaCapture(); await mediaCapture.InitializeAsync(set); }
private async void btnStartPreview_Click(object sender, RoutedEventArgs e) { if (isPreviewing == false) { previewElement.Source = mediaCapture; await mediaCapture.StartPreviewAsync(); isPreviewing = true; } previewElement.Visibility = Visibility.Visible; }
<Grid HorizontalAlignment="Center"> <Grid.RowDefinitions> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> <RowDefinition /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition /> <ColumnDefinition /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Happiness</TextBlock> <ProgressBar Grid.Row="0" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Happiness, Mode=OneWay}" /> <TextBlock Grid.Row="1" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Anger</TextBlock> <ProgressBar Grid.Row="1" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Anger, Mode=OneWay}" /> <TextBlock Grid.Row="2" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Contempt</TextBlock> <ProgressBar Grid.Row="2" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Contempt, Mode=OneWay}" /> <TextBlock Grid.Row="3" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Disgust</TextBlock> <ProgressBar Grid.Row="3" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Disgust, Mode=OneWay}" /> <TextBlock Grid.Row="4" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Fear</TextBlock> <ProgressBar Grid.Row="4" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Fear, Mode=OneWay}" /> <TextBlock Grid.Row="5" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Neutral</TextBlock> <ProgressBar Grid.Row="5" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Neutral, Mode=OneWay}" /> <TextBlock Grid.Row="6" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Sadness</TextBlock> <ProgressBar Grid.Row="6" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Sadness, Mode=OneWay}" /> <TextBlock Grid.Row="7" Grid.Column="0" Foreground="Black" FontSize="14" TextAlignment="Right" Margin="0,0,5,0">Surprise</TextBlock> <ProgressBar Grid.Row="7" Grid.Column="1" IsIndeterminate="False" Width="200" Maximum="1" SmallChange="0.0001" LargeChange="0.1" Value="{Binding Scores.Surprise, Mode=OneWay}" /> </Grid>
btnTakePhoto.IsEnabled = false; btnStartPreview.IsEnabled = false; InMemoryRandomAccessStream stream = new InMemoryRandomAccessStream(); await mediaCapture.CapturePhotoToStreamAsync(ImageEncodingProperties.CreateJpeg(), stream); stream.Seek(0); // 0 BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(stream); captureImage.Source = bitmap; stream.Seek(0); // 0 Stream st = stream.AsStream(); // InMemoryRandomAccessStream System.IO.Stream if (isPreviewing == true) await mediaCapture.StopPreviewAsync(); isPreviewing = false; previewElement.Visibility = Visibility.Collapsed;
progring.IsActive = true; try { EmotionServiceClient emotionServiceClient = new EmotionServiceClient("12345678901234567890123456789012"); emotionResult = await emotionServiceClient.RecognizeAsync(st); } catch { } progring.IsActive = false;
if ((emotionResult != null) && (emotionResult.Length > 0)) { emo.Clear(); emo.Add(emotionResult[0]); this.DataContext = emo.ElementAt(0); } btnStartPreview.IsEnabled = true; btnTakePhoto.IsEnabled = true;
// Windows.Storage.StorageFile photoFile = await Windows.Storage.KnownFolders.PicturesLibrary.CreateFileAsync( "lastphoto.jpeg", Windows.Storage.CreationCollisionOption.ReplaceExisting); await mediaCapture.CapturePhotoToStorageFileAsync(ImageEncodingProperties.CreateJpeg(), photoFile); IRandomAccessStream photoStream = await photoFile.OpenReadAsync(); BitmapImage bitmap = new BitmapImage(); bitmap.SetSource(photoStream); captureImage.Source = bitmap; photoStream.Seek(0); // 0 Stream st = photoStream.AsStream(); // IRandomAccessStream System.IO.Stream
Source: https://habr.com/ru/post/276411/
All Articles