Achindra Bhatnagar
Published © MIT

Microsoft Azure Machine Learning and Face Detection in IoT

Applications of Future draw their intelligence from a wide variety of source (Web 3.0 connected databases).

IntermediateFull instructions provided16 hours8,536
Microsoft Azure Machine Learning and Face Detection in IoT

Things used in this project

Hardware components

Camera (generic)
×1

Software apps and online services

Microsoft Cognitive Services
Microsoft Azure
Microsoft Azure
Windows 10 IoT Core
Microsoft Windows 10 IoT Core
Visual Studio 2015
Microsoft Visual Studio 2015
Microsoft Cortana Intelligence Suite

Story

Read more

Code

PersonGroups Class

C#
public class PersonGroups
{
    public string personGroupId { get; set; }
    public string name { get; set; }
    public string userData { get; set; }
}

AddPersonGroups

C#
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 Class

C#
class Persons
{
    public string personId { get; set; }
    public string name { get; set; }
    public string userData { get; set; }
    public List persistedFaceIds { get; set; }
}

CreatePerson

C#
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 Class

C#
public class FaceData
{
    public string persistedFaceId { get; set; }
    public string userData { get; set; }
}

Add Face to Person

C#
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 Model

C#
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 Status

C#
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 Classes

C#
       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 Code

C#
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 Code

C#
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

Achindra Bhatnagar

Achindra Bhatnagar

20 projects • 161 followers
Windows Kernel Hacker, IoT Hobbyist, Enthusiast, Developer and Dreamer

Comments