Things used in this project

Hardware components:
Camera (generic)
×1
Software apps and online services:
Microsoft Cognitive Services
W9gt7hzo
Microsoft Azure
10
Microsoft Windows 10 IoT Core
Vs2015logo
Microsoft Visual Studio 2015
Microsoft Cortana Intelligence Suite

Code

PersonGroups ClassC#
public class PersonGroups
{
    public string personGroupId { get; set; }
    public string name { get; set; }
    public string userData { get; set; }
}
AddPersonGroupsC#
public static async Task<string> AddPersonGroups(string personGroupId, string name, string userData)
{
    string uri = HttpHandler.BaseUri + "/" + personGroupId;
    string jsonString = "{\"name\":\"" + name + "\", \"userData\":\"" + userData + "\"}";
    HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
    
    HttpResponseMessage response = await HttpHandler.client.PutAsync(uri, content);
    response.EnsureSuccessStatusCode();
    
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}
Persons ClassC#
class Persons
{
    public string personId { get; set; }
    public string name { get; set; }
    public string userData { get; set; }
    public List persistedFaceIds { get; set; }
}
CreatePersonC#
public static async Task<string> CreatePerson(string personGroupId, string name, string userData)
{
    string uri = HttpHandler.BaseUri + "/" + personGroupId + "/persons";
    string jsonString = "{\"name\":\"" + name + "\", \"userData\":\"" + userData + "\"}";
    HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
    
    HttpResponseMessage response = await HttpHandler.client.PostAsync(uri, content);
    response.EnsureSuccessStatusCode();
    
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}
FaceData ClassC#
public class FaceData
{
    public string persistedFaceId { get; set; }
    public string userData { get; set; }
}
Add Face to PersonC#
public static async Task<string> AddPersonFace(string personGroupId, string personId, string userData)
{
    string uri = HttpHandler.BaseUri + "/" + personGroupId + "/persons/" + personId + "/persistedFaces?userData=" + userData + "&userData=" + userData;
    string jsonString = "{\"url\":\"" + HttpHandler.storagePath + "originals/" + userData + "\"}";
    HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
    
    HttpResponseMessage response = await HttpHandler.client.PostAsync(uri, content);
    response.EnsureSuccessStatusCode();
    
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}
Train Face ML ModelC#
public static async Task<string> TrainPersonGroups(string personGroupId)
{
    string uri = HttpHandler.BaseUri + "/" + personGroupId + "/train";
    
    HttpResponseMessage response = await HttpHandler.client.PostAsync(uri, null);
    response.EnsureSuccessStatusCode();
    
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}
Check Training StatusC#
public static async Task<string> StatusPersonGroups(string personGroupId)
{
    string uri = HttpHandler.BaseUri + "/" + personGroupId + "/training";
    
    HttpResponseMessage response = await HttpHandler.client.GetAsync(uri);
    response.EnsureSuccessStatusCode();
    
    string responseBody = await response.Content.ReadAsStringAsync();
    return responseBody;
}
Face Detection Related ClassesC#
       public class FaceRectangle
        {
            public int top { get; set; }
            public int left { get; set; }
            public int width { get; set; }
            public int height { get; set; }
        }

        public class Visitors
        {
            public string faceId { get; set; }
            public FaceRectangle faceRectangle { get; set; }
        }

        public class FaceQueryPayload
        {
            public string personGroupId { get; set; }
            public List<string> faceIds { get; set; }
            public int maxNumOfCandidatesReturned { get; set; }
            public double confidenceThreshold { get; set; }
        }


        public class Candidate
        {
            public string personId { get; set; }
            public double confidence { get; set; }
        }

        public class CandidateObject
        {
            public string faceId { get; set; }
            public List<Candidate> candidates { get; set; }
        }
Upload Image to Blob and Call Face Identity CodeC#
private async void btnTestPic_Click(object sender, RoutedEventArgs e)
{
    btnTestPic.IsEnabled = false;
    FileOpenPicker filePicker = new FileOpenPicker();
    filePicker.SuggestedStartLocation = PickerLocationId.PicturesLibrary;
    filePicker.ViewMode = PickerViewMode.Thumbnail;

    filePicker.FileTypeFilter.Clear();
    filePicker.FileTypeFilter.Add(".jpeg"); filePicker.FileTypeFilter.Add(".jpg");
    filePicker.FileTypeFilter.Add(".png"); filePicker.FileTypeFilter.Add(".gif");

    StorageFile file = await filePicker.PickSingleFileAsync();
            
    CloudBlockBlob blob = null;
    string blobFileName = null;
    if (null != file)
    {
        BitmapImage bitmapImage = new BitmapImage();
        IRandomAccessStream fileStream = await file.OpenAsync(FileAccessMode.Read);
        bitmapImage.SetSource(fileStream);
        CapturedPhoto.Source = bitmapImage;
        CapturedPhoto.Tag = file.Path;

        blobFileName = System.Guid.NewGuid() + "." + file.Name.Split('.').Last();

        await HttpHandler.tempContainer.CreateIfNotExistsAsync();
        BlobContainerPermissions permissions = new BlobContainerPermissions();
        permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
        await HttpHandler.tempContainer.SetPermissionsAsync(permissions);
        blob = HttpHandler.tempContainer.GetBlockBlobReference(blobFileName);
        await blob.DeleteIfExistsAsync();
        await blob.UploadFromFileAsync(file);

        string uri = "https://api.projectoxford.ai/face/v1.0/detect?returnFaceId=true";
        string jsonString = "{\"url\":\"" + HttpHandler.storagePath + "visitors/" + blobFileName + "\"}";
        HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
        
        HttpResponseMessage response = await HttpHandler.client.PostAsync(uri, content);
        response.EnsureSuccessStatusCode();
        
        string responseBody = await response.Content.ReadAsStringAsync();

        List names = await VisitorCmds.CheckFace(responseBody, ((PersonGroups)cmbPersonGroup.SelectedItem).personGroupId);
        txtResponse.Text = string.Join(",", names.ToArray());
    }
    btnTestPic.IsEnabled = true;
}
CheckFace CodeC#
public static async Task<List<string>> CheckFace(string responseString, string personGroupId)
{
    List<Visitors> visitors = JsonConvert.DeserializeObject<List<Visitors>>(responseString);
    List<string> faceIds = new List<string>() ;
    foreach (Visitors visitor in visitors)
    {
        faceIds.Add(visitor.faceId);
    }

    FaceQueryPayload jsonPayLoad = new PhotoBooth.VisitorCmds.FaceQueryPayload();
    jsonPayLoad.personGroupId = personGroupId;
    jsonPayLoad.faceIds = faceIds;
    jsonPayLoad.maxNumOfCandidatesReturned = 1;
    jsonPayLoad.confidenceThreshold = 0.5;


    string uri = "https://api.projectoxford.ai/face/v1.0/identify";
    string jsonString = JsonConvert.SerializeObject(jsonPayLoad);
    HttpContent content = new StringContent(jsonString, Encoding.UTF8, "application/json");
    
    HttpResponseMessage response = await HttpHandler.client.PostAsync(uri, content);
    response.EnsureSuccessStatusCode();

    string responseBody = await response.Content.ReadAsStringAsync();
    List<CandidateObject> candidates = JsonConvert.DeserializeObject<List<CandidateObject>>(responseBody);

    List<string> names = new List<string>();
    foreach (CandidateObject candidate in candidates)
    {
        //get person from personId
        if (candidate.candidates.Count != 0)
        {
            string name = await PersonCmds.GetPerson(personGroupId, candidate.candidates[0].personId);
            names.Add(name);
        }
    }
    return names;
}

Credits

418553
Achindra Bhatnagar
18 projects • 83 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer
Contact

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Sign up / LoginProjectsPlatformsTopicsContestsLiveAppsBetaBlog