forked from AshveenBansal98/Attendo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlankPage1.xaml.cs
executable file
·196 lines (172 loc) · 8.58 KB
/
BlankPage1.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using Microsoft.ProjectOxford.Face;
using Microsoft.ProjectOxford.Face.Contract;
using Microsoft.ProjectOxford.Common.Contract;
using Windows.Media.Capture;
using Windows.Graphics.Display;
using Windows.Storage.Streams;
using Windows.Media.MediaProperties;
using Windows.UI.Xaml.Media.Imaging;
using System.Windows.Input;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Graphics.Imaging;
using Windows.Media.FaceAnalysis;
using Windows.UI.Xaml.Shapes;
using Microsoft.WindowsAzure.MobileServices;
// The Blank Page item template is documented at https://go.microsoft.com/fwlink/?LinkId=234238
namespace Attendo
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class BlankPage1 : Page
{
public static MobileServiceClient MobileService = new MobileServiceClient("https://attendoapp.azurewebsites.net");
FaceServiceClient faceServiceClient = new FaceServiceClient("51465113ce1a4f0ba199cc6292482c91", "https://southeastasia.api.cognitive.microsoft.com/face/v1.0");
public class TodoItem
{
public string Id { get; set; }
public string Date { get; set; }
public string Roll { get; set; }
}
public BlankPage1()
{
this.InitializeComponent();
}
private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
{
this.Frame.Navigate(typeof(MainPage));
}
private async void Button_Click(object sender, RoutedEventArgs e)
{
Roll.Text = "Roll no.(s) of Identified Persons: ";
VisualizationCanvas.Children.Clear();
FileOpenPicker photoPicker = new FileOpenPicker();
photoPicker.ViewMode = PickerViewMode.Thumbnail;
photoPicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
photoPicker.FileTypeFilter.Add(".jpg");
photoPicker.FileTypeFilter.Add(".jpeg");
photoPicker.FileTypeFilter.Add(".png");
photoPicker.FileTypeFilter.Add(".bmp");
StorageFile photoFile = await photoPicker.PickSingleFileAsync();
if (photoFile == null)
{
return;
}
string filePath = photoFile.Path;
using (var stream = await photoFile.OpenAsync(FileAccessMode.Read))
{
var faces = await faceServiceClient.DetectAsync(stream.AsStream());
var faceIds = faces.Select(face => face.FaceId).ToArray();
var results = await faceServiceClient.IdentifyAsync("student", faceIds);
foreach (var identifyResult in results)
{
//Console.WriteLine("Result of face: {0}", identifyResult.FaceId);
if (identifyResult.Candidates.Length == 0)
{
string s = Roll.Text;
Roll.Text = s + "Not identified, ";
}
else
{
// Get top 1 among all candidates returned
Person[] x = await faceServiceClient.ListPersonsAsync("student");
Candidate name = identifyResult.Candidates[0];
var candidateId = name.PersonId;
int p = 0;
for(int i = 0; i < x.Length; i++)
{
if (x[i].PersonId == candidateId)
{
p = 1;
break;
}
}
if (p == 0)
{
Roll.Text = Roll.Text + " Not identified, ";
continue;
}
var person = await faceServiceClient.GetPersonAsync("student", candidateId);
string s = Roll.Text;
Roll.Text = s + person.Name + ", ";
string day = DateTime.Today.Day.ToString();
string month = DateTime.Today.Month.ToString();
string year = DateTime.Today.Year.ToString();
string date = month + "/" + day + "/" + year;
TodoItem item = new TodoItem {
Roll = person.Name,
Date = date
};
await MobileService.GetTable<TodoItem>().InsertAsync(item);
}
}
BitmapImage bitmapSource = new BitmapImage();
IRandomAccessStream fileStream = await photoFile.OpenAsync(FileAccessMode.Read);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(fileStream);
BitmapTransform transform = new BitmapTransform();
const float sourceImageHeightLimit = 1280;
if (decoder.PixelHeight > sourceImageHeightLimit)
{
float scalingFactor = (float)sourceImageHeightLimit / (float)decoder.PixelHeight;
transform.ScaledWidth = (uint)Math.Floor(decoder.PixelWidth * scalingFactor);
transform.ScaledHeight = (uint)Math.Floor(decoder.PixelHeight * scalingFactor);
}
SoftwareBitmap sourceBitmap = await decoder.GetSoftwareBitmapAsync(decoder.BitmapPixelFormat, BitmapAlphaMode.Premultiplied, transform, ExifOrientationMode.IgnoreExifOrientation, ColorManagementMode.DoNotColorManage);
const BitmapPixelFormat faceDetectionPixelFormat = BitmapPixelFormat.Gray8;
SoftwareBitmap convertedBitmap;
if (sourceBitmap.BitmapPixelFormat != faceDetectionPixelFormat)
{
convertedBitmap = SoftwareBitmap.Convert(sourceBitmap, faceDetectionPixelFormat);
}
else
{
convertedBitmap = sourceBitmap;
}
SolidColorBrush lineBrush = new SolidColorBrush(Windows.UI.Colors.Yellow);
double lineThickness = 2.0;
SolidColorBrush fillBrush = new SolidColorBrush(Windows.UI.Colors.Transparent);
ImageBrush brush = new ImageBrush();
SoftwareBitmapSource bitmapsource = new SoftwareBitmapSource();
await bitmapsource.SetBitmapAsync(sourceBitmap);
brush.ImageSource = bitmapsource;
brush.Stretch = Stretch.Fill;
this.VisualizationCanvas.Background = brush;
double widthScale = sourceBitmap.PixelWidth / this.VisualizationCanvas.ActualWidth;
double heightScale = sourceBitmap.PixelHeight / this.VisualizationCanvas.ActualHeight;
foreach (var face in faces)
{
// Create a rectangle element for displaying the face box but since we're using a Canvas
// we must scale the rectangles according to the image’s actual size.
// The original FaceBox values are saved in the Rectangle's Tag field so we can update the
// boxes when the Canvas is resized.
Rectangle box = new Rectangle
{
Tag = face.FaceRectangle,
Width = (uint)(face.FaceRectangle.Width / widthScale),
Height = (uint)(face.FaceRectangle.Height / heightScale),
Fill = fillBrush,
Stroke = lineBrush,
StrokeThickness = lineThickness,
Margin = new Thickness((uint)(face.FaceRectangle.Left / widthScale), (uint)(face.FaceRectangle.Top / heightScale), 0, 0)
};
this.VisualizationCanvas.Children.Add(box);
}
}
}
}
}