private async Task ListAllPersonInGroup() { var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); string listallpersonIngrouprequest = uriFace + "/persongroups/group1/persons"; var response = await client.GetAsync(listallpersonIngrouprequest); string json = await response.Content.ReadAsStringAsync(); allperson = await Myjson.ToObjectAsync <List <PersonsInFaceGroup> >(json); if (allperson.Count > 0) { foreach (var item in allperson) { if (!FaceDictionary.ContainsKey(item.personId)) { FaceDictionary.Add(item.personId, item.name); } } } }
private async void DoorBell_Click(object sender, RoutedEventArgs e) { DoorBell.IsEnabled = false; ImageEncodingProperties imageFormat = ImageEncodingProperties.CreateJpeg(); StorageFile file = await ApplicationData.Current.LocalCacheFolder.CreateFileAsync($"{DateTime.Now.ToFileTime()}.jpg", CreationCollisionOption.GenerateUniqueName); await captureManager.CapturePhotoToStorageFileAsync(imageFormat, file); Stream stream = await file.OpenStreamForReadAsync(); BinaryReader binaryReader = new BinaryReader(stream); byte[] imagedata = binaryReader.ReadBytes((int)stream.Length); ByteArrayContent byteArrayContent = new ByteArrayContent(imagedata); var client = new HttpClient(); client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); string requestuir = uriFace + "detect?returnFaceId=true";// &returnFaceLandmarks=true&returnFaceAttributes=age,gender,headPose,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise"; byteArrayContent.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream"); var responseMessage = await client.PostAsync(requestuir, byteArrayContent); if (responseMessage.StatusCode.ToString() == "OK") { string detectjson = await responseMessage.Content.ReadAsStringAsync(); detectFaces = await Myjson.ToObjectAsync <List <MyDetectFace> >(detectjson); if (detectFaces.Count > 0) { BitmapImage image = new BitmapImage(new Uri(file.Path)); //visitors.Add(new Visitor { VisitDate = System.DateTime.Now.ToString(), VisitorImage = image }); var identifyclient = new HttpClient(); string identifyUri = uriFace + "identify"; identifyclient.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", subscriptionKey); string requestjson = "{\"personGroupId\":\"" + groupId + "\",\"faceIds\":[\"" + detectFaces[0].faceId + "\"],\"maxNumOfCandidatesReturned\": 1,\"confidenceThreshold\": 0.5}"; HttpContent httpContent = new StringContent(requestjson); httpContent.Headers.ContentType = new MediaTypeHeaderValue("application/json"); HttpResponseMessage httpResponseMessage = await client.PostAsync(identifyUri, httpContent); if (httpResponseMessage.StatusCode.ToString() == "OK") { string identifyjson = await httpResponseMessage.Content.ReadAsStringAsync(); identifyFaces = await Myjson.ToObjectAsync <List <MyIdentifyFace> >(identifyjson); if (identifyFaces[0].candidates.Count > 0) { //树莓派崩了的点 if (identifyFaces[0].candidates[0].confidence > confidenceThreshold) { DebugText.Text = identifyFaces[0].candidates[0].confidence.ToString(); if (FaceDictionary.ContainsKey(identifyFaces[0].candidates[0].personId)) { DebugText.Text = FaceDictionary.ContainsKey(identifyFaces[0].candidates[0].personId).ToString(); PersonsInFaceGroup person = allperson.Find(x => x.personId.Contains(identifyFaces[0].candidates[0].personId)); string words = $"欢迎回来,{person.userData}{FaceDictionary[identifyFaces[0].candidates[0].personId]}"; visitors.Add(new Visitor { VisitDate = System.DateTime.Now.ToString(), VisitorImage = image, Name = FaceDictionary[identifyFaces[0].candidates[0].personId] }); MySpeechHelper.SaySomething(words, myFaceDetectMediaElement); MyNotificationHelper.SendNotification(file.Path, FaceDictionary[identifyFaces[0].candidates[0].personId], person.userData); } } } else { string words = "对不起!我好像不认识你哦!"; visitors.Add(new Visitor { VisitDate = System.DateTime.Now.ToString(), VisitorImage = image, Name = "陌生人" }); MySpeechHelper.SaySomething(words, myFaceDetectMediaElement); MyNotificationHelper.SendNotification(file.Path); } } } else { string words = "你是不是逗我玩呢?能看着镜头吗?再试一次,我就不信了!"; MySpeechHelper.SaySomething(words, myFaceDetectMediaElement); } } DoorBell.IsEnabled = true; }