private async void addFaceAndTrainData(string imagePath, System.Guid personId) { try { using (Stream imageFileStream = File.OpenRead(imagePath)) { AddPersistedFaceResult faceRes = await faceServiceClient.AddPersonFaceAsync(this.personGroupId, personId, imageFileStream); Console.Out.WriteLine("Added face to Person with Id " + faceRes.PersistedFaceId); } await faceServiceClient.TrainPersonGroupAsync(this.personGroupId); TrainingStatus status = null; do { status = await faceServiceClient.GetPersonGroupTrainingStatusAsync(this.personGroupId); } while (status.Status.ToString() != "Succeeded"); } catch (FaceAPIException f) { MessageBox.Show(f.ErrorCode, f.ErrorMessage); } }
private async Task <Guid> CreatePerson(List <FaceSendInfo> facesInfo, Guid newPersonID, Face f) { AddPersistedFaceResult result = null; try { //No candidate we are going to create new person and set candidate to be same as newly created person with confidence of 100% var name = f.FaceAttributes.Gender + "-" + f.FaceAttributes.Age + "-" + newPersonID.ToString(); newPersonID = (await FaceServiceHelper.CreatePersonWithResultAsync(groupId, name)).PersonId; var fi = facesInfo.Where(fin => fin.faceId == f.FaceId.ToString()).FirstOrDefault(); fi.canid = newPersonID.ToString(); fi.canname = name; fi.canconf = 1; await FaceServiceHelper.AddPersonFaceAsync(groupId, newPersonID, await this.GetImageStreamCallback(), "", f.FaceRectangle); } catch (Exception e) { // Catch errors with individual groups so we can continue looping through all groups. Maybe an answer will come from // another one. ErrorTrackingHelper.TrackException(e, "Problem adding face to group"); if (this.ShowDialogOnFaceApiErrors) { await ErrorTrackingHelper.GenericApiCallExceptionHandler(e, "Problem adding face to group"); } } return(newPersonID); }
/// <summary> /// Function to add example faces to a given face list. Will loop through a folder and add all image files in that folder /// </summary> /// <param name="obj"></param> private async void AddExampleFacesToList(object obj) { string personGroupDirectory = Path.Combine(Environment.CurrentDirectory, "PersonGroup"); string[] images = GetImageFiles(personGroupDirectory); try { foreach (string image in images) { using (Stream fileStream = File.OpenRead(image)) { Face[] faces = await _faceServiceClient.DetectAsync(fileStream); FaceRectangle faceRectangle = faces[0].FaceRectangle; AddPersistedFaceResult addFacesResult = await _faceServiceClient.AddFaceToFaceListAsync( FaceListName.ToLower(), fileStream, null, faceRectangle); UpdateFaceGuidsAsync(); } } } catch (FaceAPIException ex) { Debug.WriteLine($"Failed to add faces to face list: {ex.ErrorMessage}"); } catch (Exception ex) { Debug.WriteLine(ex.Message); } }
private async void AddPersonFace(object obj) { try { var openDialog = new Microsoft.Win32.OpenFileDialog(); openDialog.Filter = "Image Files(*.jpg, *.gif, *.bmp, *.png)|*.jpg;*.jpeg;*.gif;*.bmp;*.png"; bool?result = openDialog.ShowDialog(); if (!(bool)result) { return; } string filePath = openDialog.FileName; using (Stream imageFile = File.OpenRead(filePath)) { AddPersistedFaceResult addFaceResult = await _faceServiceClient.AddPersonFaceAsync(SelectedPersonGroup.PersonGroupId, SelectedPerson.PersonId, imageFile); if (addFaceResult != null) { StatusText = $"Face added for {SelectedPerson.Name}. Remeber to train the person group!"; } } } catch (FaceAPIException ex) { StatusText = $"Failed to add person face: {ex.ErrorMessage}"; } catch (Exception ex) { StatusText = $"Failed to add person face: {ex.Message}"; } }
private async void AddFaceToPerson() { Object userid = settings.Values["userid"]; Object personid = settings.Values["personid"]; if (userid != null && personid != null) { enroll_progressbar.Visibility = Visibility.Visible; NextBtn.IsEnabled = false; if (file != null) { using (var fileStream = await file.OpenStreamForReadAsync()) { LoggingMsg("Add face for person to server..."); try { var faceServiceClient = new FaceServiceClient(subscriptionKey); AddPersistedFaceResult addPersistedFaceResult = await faceServiceClient.AddPersonFaceAsync(PersonGroupId, (Guid)personid, fileStream); Guid FaceId = addPersistedFaceResult.PersistedFaceId; if (FaceId != null) { settings.Values["faceid"] = FaceId; LoggingMsg("Sync face image with server success. FaceId = " + FaceId); NextBtn.IsEnabled = true; AddFaceSuccess = true; } //start training await faceServiceClient.TrainPersonGroupAsync(PersonGroupId); enroll_progressbar.Visibility = Visibility.Collapsed; } catch (FaceAPIException ex) { enroll_progressbar.Visibility = Visibility.Collapsed; NextBtn.IsEnabled = false; Debug.WriteLine("Response: {0}. {1}", ex.ErrorCode, ex.ErrorMessage); LoggingMsg("Sync face for person to server failed."); } catch { enroll_progressbar.Visibility = Visibility.Collapsed; NextBtn.IsEnabled = false; LoggingMsg("Sync face for person to server failed."); } } } else { // file missing LoggingMsg("Image file missing. Please choose image again."); NextBtn.IsEnabled = false; enroll_progressbar.Visibility = Visibility.Collapsed; } } }
public static async Task <AddPersistedFaceResult> AddPersonFaceAsync(string strPersonGroupId, Guid personId, Stream stream, string strUserData = null, FaceRectangle faceRectangle = null) { AddPersistedFaceResult addPersistedFaceResult = null; addPersistedFaceResult = await fsClient.AddPersonFaceAsync(strPersonGroupId, personId, stream, strUserData, faceRectangle); return(addPersistedFaceResult); }
/// <summary> /// 加入人臉的動作 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private async void btnAddFace_Click(object sender, EventArgs e) { byte[] data = File.ReadAllBytes(txtPerson.Text); MemoryStream stream = new MemoryStream(data); AddPersistedFaceResult result = await face.AddPersonFaceAsync(this.PersonGroupId, Guid.Parse(this.PersonId), stream); File.Copy(txtPerson.Text, @"Faces\" + result.PersistedFaceId.ToString(), true); txtPerson.Text = ""; this.BindFace(); }
/// <summary> /// /// </summary> /// <param name="FaceListID"></param> /// <param name="Image"></param> /// <param name="UserData"></param> /// <param name="FaceRectangle"></param> /// <returns></returns> public static async Task <Guid> AddFaceToFaceListByFaceListID(string FaceListID, Stream Image, string UserData = null, FaceRectangle FaceRectangle = null) { AddPersistedFaceResult FacePersistantID = null; FacePersistantID = await fsClient.AddFaceToFaceListAsync(FaceListID, Image, UserData, FaceRectangle); //FacePersistantID = AsyncHelpers.RunSync(() => // { // return fsClient.AddFaceToFaceListAsync(FaceListID, Image, UserData, FaceRectangle); // }); return(FacePersistantID.PersistedFaceId); }
/// <summary> /// Add face to both Cloud Face API and local whitelist /// </summary> /// <param name="personId"></param> /// <param name="faceId"></param> /// <param name="imagePath"></param> /// <returns></returns> private async Task AddFace(Guid personId, Guid faceId, string imagePath) { // prevent running synchronous call on UI thread await Task.Run(async() => { using (Stream imageStream = File.OpenRead(imagePath)) { AddPersistedFaceResult result = await _faceApiClient.AddPersonFaceAsync(WhitelistId, personId, imageStream); } _whitelist.AddFace(personId, faceId, imagePath); }); }
/// <summary> /// 上传图片到faceList /// </summary> /// <param name="imgPath"></param> /// <returns></returns> public async Task <AddPersistedFaceResult> ImgToFaceList(string imgPath) { if (!File.Exists(imgPath) || string.IsNullOrEmpty(imgPath)) { return(null); } using (var stream = File.OpenRead(imgPath)) { AddPersistedFaceResult ret = await faceServiceClient.AddFaceToFaceListAsync(faceListId, stream); return(ret); } }
public async Task AddFaceToPerson() { if (PersonResult == null) { await AddPersonTest(); } string TestPictureLocation = $@"W:\Example Pictures\imgNov82017.png"; using (Stream stream = File.OpenRead(TestPictureLocation)) { persistedFaceResult = await ProjectOxfordAPI.AddPersonFaceAsync(cstPersonGroupIDforTest, PersonResult.PersonId, stream); Assert.IsTrue(persistedFaceResult != null, "item == null"); } }
/// <summary> /// Add face to a person in FaceAPI from a stream /// </summary> /// <param name="faceClient"></param> /// <param name="facesListId"></param> /// <param name="personName"></param> /// <param name="faceStream"></param> /// <param name="filePath">Local file path</param> /// <returns></returns> public static async Task <List <TrainedFace> > UploadFaceAsync(FaceServiceClient faceClient, string facesListId, string personName, Stream faceStream, string filePath) { var persistedFaces = new List <TrainedFace>(); try { bool rateLimitExceeded; do { rateLimitExceeded = false; try { AddPersistedFaceResult uploadedFace = await faceClient.AddFaceToLargeFaceListAsync(facesListId, faceStream, personName); persistedFaces.Add(new TrainedFace( new Face { FaceId = uploadedFace.PersistedFaceId }, personName, filePath)); } catch (FaceAPIException e) { if (e.ErrorCode == "RateLimitExceeded") { rateLimitExceeded = true; await Task.Delay(1); } else if (e.ErrorCode != "InvalidURL" && e.ErrorCode != "InvalidImage") { throw; } // otherwise, just ignore this image } } while (rateLimitExceeded); return(persistedFaces); } catch (FileNotFoundException e) { Console.WriteLine(e); // just ignore this face return(null); } }
public async Task <bool> AddPerson(string GroupID, string eid, List <Stream> FaceStreamList) { bool success = false; PersonResultItem person = new PersonResultItem { EnterpriseID = eid, PersonGroupId = GroupID }; try { var personList = await faceServiceClient.ListPersonsAsync(ConstantsString.GroupId); var responsePerson = personList.FirstOrDefault(t => t.Name == eid); if (responsePerson != null) { person.PersonId = responsePerson.PersonId; } else { CreatePersonResult result = await faceServiceClient.CreatePersonAsync(person.PersonGroupId, person.EnterpriseID); person.PersonId = result.PersonId; } if (person.PersonId != Guid.Empty) { foreach (var faceStream in FaceStreamList) { await WaitCallLimitPerSecondAsync(); AddPersistedFaceResult result = await faceServiceClient.AddPersonFaceAsync(ConstantsString.GroupId, person.PersonId, faceStream); success = result.PersistedFaceId != Guid.Empty; } } else { success = false; } } catch (Exception ex) { throw ex; } return(success); }
private async void SubmitToAzureButton_ClickAsync(object sender, RoutedEventArgs e) { int imageCounter = 0; var items = await personFolder.GetFilesAsync(); List <StorageFile> imageFilesToUpload = new List <StorageFile>(); foreach (StorageFile item in items) { //Windows Cam default save type is jpg if (item.FileType == ".jpg") { imageCounter++; imageFilesToUpload.Add(item); } } if (imageCounter >= minPhotos) { imageCounter = 0; try { foreach (StorageFile imageFile in imageFilesToUpload) { imageCounter++; using (Stream s = await imageFile.OpenStreamForReadAsync()) { AddPersistedFaceResult addResult = await faceServiceClient.AddPersonFaceAsync(personGroupId, personId, s); Debug.WriteLine("Add result: " + addResult + addResult.PersistedFaceId); } SubmissionStatusTextBlock.Text = "Submitted Image n. " + imageCounter; } SubmissionStatusTextBlock.Text = "Total Images submitted: " + imageCounter; } catch (Exception ex) { Debug.WriteLine("Submission Exc: " + ex.Message); } } else { SubmissionStatusTextBlock.Text = $"Please add at least {minPhotos} face images to the person folder."; } }
static async Task addFace() { Console.WriteLine("Person:"); Console.WriteLine("1. Caleb"); Console.WriteLine("2. Adna"); Console.WriteLine("3. Jordan"); Console.WriteLine("4. Nicole"); Console.WriteLine("5. Nat"); choice = Convert.ToInt32(Console.ReadLine()) - 1; Console.WriteLine("Paste URL of picture:"); input = Console.ReadLine(); Console.WriteLine("Loading..."); Microsoft.ProjectOxford.Face.Contract.Face[] face = await client.DetectAsync(input); Microsoft.ProjectOxford.Face.Contract.FaceRectangle faceRect = face[0].FaceRectangle; AddPersistedFaceResult result = await client.AddPersonFaceAsync("rejected", ids[choice], input, null, faceRect); Console.WriteLine("Face added"); }
public async Task <IActionResult> Register(string login, [FromBody] byte[] photo, string userName = "") { var user = new { login = login, userName = userName, photo = photo }; // проверить по логину запись в базе данных // если такая запись есть, обломать // ... // ... // ... //если такой записи нет, регистрируем - получаем новый PersonnId try { using (Stream imageFileStream = new MemoryStream(photo)) { //await faceServiceClient.CreatePersonGroupAsync(groupName, groupName); // считаем, что у нас уже зарегистрироавна группа CreatePersonResult person = await faceServiceClient.CreatePersonAsync(groupName, userName); AddPersistedFaceResult faceResult = await faceServiceClient.AddPersonFaceAsync(groupName, person.PersonId, imageFileStream); var result = new { RegisterResult = true, PersonId = person.PersonId }; return(Json(result)); } } catch (Exception ex) { var result = new { RegisterResult = false, Message = "Ошибка при регистрации, возможно такой пользователь уже зарегистрирован", ErrorMessage = ex.Data["ErrorMessage"], }; return(Json(result)); } }
public async Task <AddPersistedFaceResult> AddFaceToFaceListAsync(string faceListId, string imageUrl, string userData = null, FaceRectangle targetFace = null) { object empty; object[] serviceHost = new object[] { this.ServiceHostCn, "facelists", faceListId, "persistedfaces", userData, null }; if (targetFace == null) { empty = string.Empty; } else { object[] left = new object[] { targetFace.Left, targetFace.Top, targetFace.Width, targetFace.Height }; empty = string.Format("{0},{1},{2},{3}", left); } serviceHost[5] = empty; string str = string.Format("{0}/{1}/{2}/{3}?userData={4}&targetFace={5}", serviceHost); AddPersistedFaceResult addPersistedFaceResult = await this.SendRequestAsync <object, AddPersistedFaceResult>(HttpMethod.Post, str, new { url = imageUrl }); return(addPersistedFaceResult); }
/// <summary> /// Asynchronously add face in oxford for a person and link it with a profile picture /// <see cref="PersonCreationFallBack(Guid?)"/> /// </summary> public async Task <bool> AddFaceToPersonInOxford(Person person, ProfilePicture picture, Face face, string temporaryImage) { Guid faceApiId = default(Guid); try { // if the person has reached the limit of 64 faces in oxford if (person.HasReachedMaxLimitOfFaces()) { await RemoveFaceFromOxford(person); } AddPersistedFaceResult result = await _faceServiceClient.AddPersonFaceAsync(Global.OxfordPersonGroupId, person.PersonApiId, UrlHelpers.Content(Global.Host, temporaryImage), targetFace : face.FaceRectangle); faceApiId = result.PersistedFaceId; // Link profile picture with face on oxford api picture.FaceApiId = faceApiId; return(true); } catch (FaceAPIException e) { LogManager.GetLogger(GetType()).Info("Error occured during adding od face to person", e); // If is a new person we rollback its creation to avoid person without face if (person.Id == 0) { await PersonCreationFallBack(person.PersonApiId); File.Delete(MapPath(temporaryImage)); return(false); } return(true); } }
public async System.Threading.Tasks.Task <IHttpActionResult> POSTAsync() { FaceServiceClient faceSC = new FaceServiceClient("9ed1e677b9bd402c83697c7c9e31e3e7", "https://eastasia.api.cognitive.microsoft.com/face/v1.0"); string ChannelAccessToken = Properties.Settings.Default.ChannelAccessToken; string groupID = "group1"; string replyToken = ""; //回覆訊息 string Message = "無法辨識的指令,請使用功能選項輸入關鍵字!"; try { //剖析JSON string postData = Request.Content.ReadAsStringAsync().Result; var ReceivedMessage = isRock.LineBot.Utility.Parsing(postData); replyToken = ReceivedMessage.events[0].replyToken; using (SqlConnection cnn1 = new SqlConnection(sqlcn)) { using (SqlCommand cmd1 = new SqlCommand()) { cmd1.Connection = cnn1; cmd1.CommandText = "SELECT A.memType, A.adminID, A.studentID, B.personID FROM dbo.members AS A, dbo.students AS B WHERE A.userID = @userId AND A.studentID = B.studentID"; cmd1.Parameters.Add("@userId", SqlDbType.NVarChar).Value = ReceivedMessage.events[0].source.userId; cnn1.Open(); SqlDataReader rr = cmd1.ExecuteReader(); IDExist = rr.HasRows; if (IDExist) { rr.Read(); memType = int.Parse(rr[0].ToString()); adminID = int.Parse(rr[1].ToString()); studentID = int.Parse(rr[2].ToString()); personID = rr[3].ToString(); } rr.Close(); cmd1.Dispose(); cnn1.Close(); } } using (SqlConnection cnn11 = new SqlConnection(sqlcn)) { using (SqlCommand cmd11 = new SqlCommand()) { cmd11.Connection = cnn11; cmd11.CommandText = "SELECT * FROM dbo.classes"; cnn11.Open(); SqlDataReader rr = cmd11.ExecuteReader(); if (rr.HasRows) { while (rr.Read()) { TimeSpan timeSpan = DateTime.Now.Subtract(DateTime.Parse(rr[1].ToString())); if (DateTime.Parse(rr[1].ToString()).ToShortDateString() == DateTime.Now.ToShortDateString()) { nowD = int.Parse(rr[0].ToString()); } else if (timeSpan.TotalDays > 0) { prevD = int.Parse(rr[0].ToString()); prevDT = DateTime.Parse(rr[1].ToString()).ToShortDateString(); } else { nextD = int.Parse(rr[0].ToString()); nextDT = DateTime.Parse(rr[1].ToString()).ToShortDateString(); break; } } } rr.Close(); cmd11.Dispose(); cnn11.Close(); } } //判斷初次加入好友 if (ReceivedMessage.events.FirstOrDefault().type == "follow") { if (!IDExist) { using (SqlConnection cnn2 = new SqlConnection(sqlcn)) { using (SqlCommand cmd2 = new SqlCommand()) { cmd2.Connection = cnn2; cmd2.CommandText = "INSERT INTO dbo.members(userID, memType) VALUES(@userId, 0)"; cmd2.Parameters.Add("@userId", SqlDbType.NVarChar).Value = ReceivedMessage.events[0].source.userId; cnn2.Open(); if (cmd2.ExecuteNonQuery() == 1) { Message = "登錄LineBot帳號成功!\n請輸入您的ID(學生輸入學號 如:1091234,老師輸入註冊代碼):"; } else { Message = "登錄LineBot帳號失敗!\n請刪除此LineBot後重新加入或找教授/助教確認個人資料。"; } cmd2.Dispose(); cnn2.Close(); } } } else { Message = "您的userID已被註冊過!\n請隨意傳送資料或找教授/助教確認個人資料。"; } isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } else if (ReceivedMessage.events.FirstOrDefault().type == "message") { if (ReceivedMessage.events.FirstOrDefault().message.type.Trim().ToLower() == "text") { if (memType == 0) { if (ReceivedMessage.events[0].message.text.Length != 7) { Message = "輸入的ID字數錯誤,ID為7個字的字串!"; } else { using (SqlConnection cnn3 = new SqlConnection(sqlcn)) { using (SqlCommand cmd3 = new SqlCommand()) { cmd3.Connection = cnn3; cmd3.CommandText = "SELECT studentID FROM dbo.students WHERE studentAct = @studentAct"; cmd3.Parameters.Add("@studentAct", SqlDbType.NVarChar).Value = ReceivedMessage.events[0].message.text; cnn3.Open(); SqlDataReader rr = cmd3.ExecuteReader(); if (rr.HasRows) { memType = 2; rr.Read(); studentID = int.Parse(rr[0].ToString()); } rr.Close(); cmd3.Dispose(); cnn3.Close(); } } if (memType == 0) { using (SqlConnection cnn3 = new SqlConnection(sqlcn)) { using (SqlCommand cmd3 = new SqlCommand()) { cmd3.Connection = cnn3; cmd3.CommandText = "SELECT adminID FROM dbo.admins WHERE adminAct = @adminAct"; cmd3.Parameters.Add("@adminAct", SqlDbType.NVarChar).Value = ReceivedMessage.events[0].message.text; cnn3.Open(); SqlDataReader rr = cmd3.ExecuteReader(); if (rr.HasRows) { memType = 1; rr.Read(); adminID = int.Parse(rr[0].ToString()); } rr.Close(); cmd3.Dispose(); cnn3.Close(); } } } if (memType > 0) { if (memType == 1) { using (SqlConnection cnn4 = new SqlConnection(sqlcn)) { using (SqlCommand cmd4 = new SqlCommand()) { cmd4.Connection = cnn4; CreatePersonResult pr = await faceSC.CreatePersonAsync(groupID, studentID.ToString()); cmd4.CommandText = "UPDATE dbo.admins SET adminExist = 1 WHERE adminID = @adminID"; cmd4.Parameters.Add("@adminID", SqlDbType.Int).Value = adminID; cnn4.Open(); cmd4.ExecuteNonQuery(); cmd4.Dispose(); cnn4.Close(); } } } else if (memType == 2) { using (SqlConnection cnn4 = new SqlConnection(sqlcn)) { using (SqlCommand cmd4 = new SqlCommand()) { cmd4.Connection = cnn4; CreatePersonResult pr = await faceSC.CreatePersonAsync(groupID, studentID.ToString()); cmd4.CommandText = "UPDATE dbo.students SET personID = @personID, studentExist = 1 WHERE studentID = @studentID"; cmd4.Parameters.Add("@personID", SqlDbType.NVarChar).Value = pr.PersonId.ToString(); cmd4.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; cnn4.Open(); cmd4.ExecuteNonQuery(); cmd4.Dispose(); cnn4.Close(); } } } using (SqlConnection cnn4 = new SqlConnection(sqlcn)) { using (SqlCommand cmd4 = new SqlCommand()) { cmd4.Connection = cnn4; cmd4.CommandText = "UPDATE dbo.members SET adminID = @adminID, studentID = @studentID, memType = @memType WHERE userID = @userId"; cmd4.Parameters.Add("@adminID", SqlDbType.Int).Value = adminID; cmd4.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; cmd4.Parameters.Add("@memType", SqlDbType.Int).Value = memType; cmd4.Parameters.Add("@userId", SqlDbType.NVarChar).Value = ReceivedMessage.events[0].source.userId; cnn4.Open(); if (cmd4.ExecuteNonQuery() == 1) { if (memType == 1) { Message = "個人資訊註冊完畢\n感謝您的填寫\n\n您可以開始傳送照片點名及使用功能選項中的查詢點名結果確認出席狀況"; } else if (memType == 2) { Message = "請傳送臉部可清晰辨識五官的個人照(還需三張):"; } } else { Message = "登入ID失敗!\n請重新輸入或找教授/助教確認個人資料。"; } cmd4.Dispose(); cnn4.Close(); } } } } } else if (ReceivedMessage.events[0].message.text == "[線上請假]") { if (nextD == -1) { Message = "本課程已經結束!"; isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } if (memType == 5) { using (SqlConnection cnn10 = new SqlConnection(sqlcn)) { using (SqlCommand cmd10 = new SqlCommand()) { cmd10.Connection = cnn10; cmd10.CommandText = "INSERT INTO dbo. rollCall(studentID, classID, RCState, picID, RCTime) VALUES(@studentID, @classID, 2, 1, @RCTime)"; cmd10.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; cmd10.Parameters.Add("@classID", SqlDbType.Int).Value = nextD; cmd10.Parameters.Add("@RCTime", SqlDbType.DateTime).Value = DateTime.Now; cnn10.Open(); if (cmd10.ExecuteNonQuery() != 1) { Message = "請假失敗!\n請重新傳送或找教授/助教確認個人資料。"; } else { Message = "下一堂課時間為 " + nextDT + "\n\n已請假成功!"; } cmd10.Dispose(); cnn10.Close(); } } } else if (memType == 1) { using (SqlConnection cnn8 = new SqlConnection(sqlcn)) { using (SqlCommand cmd8 = new SqlCommand()) { cmd8.Connection = cnn8; cmd8.CommandText = "SELECT DISTINCT A.studentAct FROM dbo.students AS A, dbo.rollCall AS B WHERE A.studentID = B.studentID AND B.RCState = 2 AND B.classID = @classID"; cmd8.Parameters.Add("@classID", SqlDbType.Int).Value = nextD; cnn8.Open(); SqlDataReader rr = cmd8.ExecuteReader(); if (!rr.HasRows) { Message = "下一堂課時間為 " + nextDT + "\n\n今天無人請假!"; } else { Message = "下一堂課時間為 " + nextDT + "\n\n請假學生:"; while (rr.Read()) { Message += "\n" + rr[0].ToString(); } } rr.Close(); cmd8.Dispose(); cnn8.Close(); } } } } else if (ReceivedMessage.events[0].message.text == "[查詢點名結果]") { if (prevD == -1 && nowD == -1) { Message = "本課程還沒開始!"; isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } if (memType == 5) { string temp = ""; using (SqlConnection cnn12 = new SqlConnection(sqlcn)) { using (SqlCommand cmd12 = new SqlCommand()) { cmd12.Connection = cnn12; cmd12.CommandText = "SELECT RCState, RCTime FROM dbo.rollCall WHERE studentID = @studentID AND classID = @classID ORDER BY RCID DESC"; cmd12.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; if (nowD == -1) { cmd12.Parameters.Add("@classID", SqlDbType.Int).Value = prevD; Message = "最近一次點名紀錄為\n" + prevDT; } else { cmd12.Parameters.Add("@classID", SqlDbType.Int).Value = nowD; Message = "最近一次點名紀錄為\n" + DateTime.Now.ToShortDateString(); } cnn12.Open(); SqlDataReader rr = cmd12.ExecuteReader(); if (!rr.HasRows) { temp = "缺席"; } else { rr.Read(); Message += " " + DateTime.Parse(rr[1].ToString()).ToShortTimeString(); if (rr[0].ToString() == "2") { temp = "請假"; } else if (rr[0].ToString() == "1") { temp = "出席"; } else { temp = "缺席"; } } Message += "\n\n您的點名紀錄為: " + temp; rr.Close(); cmd12.Dispose(); cnn12.Close(); } } } else if (memType == 1) { using (SqlConnection cnn8 = new SqlConnection(sqlcn)) { using (SqlCommand cmd8 = new SqlCommand()) { cmd8.Connection = cnn8; cmd8.CommandText = "SELECT DISTINCT A.studentAct, B.RCState FROM (SELECT * FROM dbo.students WHERE studentID > 1) AS A LEFT OUTER JOIN dbo.rollCall AS B ON A.studentID = B.studentID AND B.classID = @classID ORDER BY B.RCState DESC"; if (nowD == -1) { cmd8.Parameters.Add("@classID", SqlDbType.Int).Value = prevD; Message = "最近一次點名紀錄為\n" + prevDT; } else { cmd8.Parameters.Add("@classID", SqlDbType.Int).Value = nowD; Message = "最近一次點名紀錄為\n" + DateTime.Now.ToShortDateString(); } cnn8.Open(); SqlDataReader rr = cmd8.ExecuteReader(); bool check = true; Message += "\n\n請假:"; check = rr.Read(); while (check && rr[1].ToString() == "2") { Message += "\n" + rr[0].ToString(); check = rr.Read(); } Message += "\n\n出席:"; while (check && rr[1].ToString() == "1") { Message += "\n" + rr[0].ToString(); check = rr.Read(); } Message += "\n\n缺席:"; while (check) { Message += "\n" + rr[0].ToString(); check = rr.Read(); } rr.Close(); cmd8.Dispose(); cnn8.Close(); } } } } else if (ReceivedMessage.events[0].message.text == "[使用說明]") { Message = "●查詢點名結果:\n" + "學生可以看到自己最近一次上課出席狀況\n" + "老師可以看到最近一堂課學生出缺勤狀況\n\n" + "●線上請假:\n" + "學生可以為下一次的課程請假\n" + "老師可以看到下一次上課請假學生資訊\n\n" + "●管理網頁:\n" + "此功能學生不適用\n" + "老師點選可直接連結到點名後台,手動更改點名資訊\n\n" + "●智慧點名方式:\n" + "此功能學生不適用\n" + "老師拍攝並傳送教室學生照片,系統會透過人臉辨識完成點名"; } /*else if (ReceivedMessage.events[0].message.text == "[管理網站]") * { * Message = "http://140.138.155.175:8080/index.aspx"; * }*/ isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } else if (ReceivedMessage.events.FirstOrDefault().message.type.Trim().ToLower() == "image") { if (memType >= 1 && memType <= 4) { //取得contentid var LineContentID = ReceivedMessage.events.FirstOrDefault().message.id.ToString(); //取得bytedata var filebody = isRock.LineBot.Utility.GetUserUploadedContent(LineContentID, ChannelAccessToken); var ms = new MemoryStream(filebody); Face[] faces = await faceSC.DetectAsync(ms); string picPath = "/Temp/" + Guid.NewGuid() + ".jpg"; var path = System.Web.HttpContext.Current.Request.MapPath(picPath); //上傳圖片 File.WriteAllBytes(path, filebody); using (SqlConnection cnn15 = new SqlConnection(sqlcn)) { using (SqlCommand cmd15 = new SqlCommand()) { cmd15.Connection = cnn15; cmd15.CommandText = "INSERT INTO dbo.pictures(studentID, picPath, picTime) VALUES(@studentID, @picPath, @picTime)"; cmd15.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; cmd15.Parameters.Add("@picPath", SqlDbType.VarChar).Value = picPath; cmd15.Parameters.Add("@picTime", SqlDbType.DateTime).Value = DateTime.Now; cnn15.Open(); cmd15.ExecuteNonQuery(); cmd15.Dispose(); cnn15.Close(); } } if (memType != 1) { if (faces.Length != 1) { Message = "無法辨識,請重新傳送五官清晰的個人照!"; isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } //上傳圖片 memType++; using (SqlConnection cnn7 = new SqlConnection(sqlcn)) { using (SqlCommand cmd7 = new SqlCommand()) { cmd7.Connection = cnn7; cmd7.CommandText = "UPDATE dbo.members SET memType = @memType WHERE studentID = @studentID"; cmd7.Parameters.Add("@memType", SqlDbType.Int).Value = memType; cmd7.Parameters.Add("@studentID", SqlDbType.Int).Value = studentID; cnn7.Open(); if (cmd7.ExecuteNonQuery() != 1) { Message = "登入圖片失敗!\n請重新傳送或找教授/助教確認個人資料。"; } cmd7.Dispose(); cnn7.Close(); } } MemoryStream stream = new MemoryStream(filebody); AddPersistedFaceResult result = await faceSC.AddPersonFaceAsync(groupID, Guid.Parse(personID), stream); await faceSC.TrainPersonGroupAsync(groupID); if (memType == 3) { Message = "請傳送臉部可清晰辨識五官的個人照(還需兩張):"; } else if (memType == 4) { Message = "請傳送臉部可清晰辨識五官的個人照(還需一張):"; } else if (memType == 5) { Message = "個人資訊註冊完畢\n感謝您的填寫\n\n您可以開始使用功能選項中的查詢點名功能及線上請假功能"; } } else { if (nowD == -1) { Message = "今日沒有上課喔!"; isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } using (SqlConnection cnn16 = new SqlConnection(sqlcn)) { using (SqlCommand cmd16 = new SqlCommand()) { cmd16.Connection = cnn16; cmd16.CommandText = "SELECT picID FROM dbo.pictures WHERE picPath = @picPath"; cmd16.Parameters.Add("@picPath", SqlDbType.NVarChar).Value = picPath; cnn16.Open(); SqlDataReader rr = cmd16.ExecuteReader(); if (rr.HasRows) { rr.Read(); picID = int.Parse(rr[0].ToString()); } cmd16.Dispose(); cnn16.Close(); } } // 將照片中的臉,與指定的PersonGroup進行比對 if (faces != null) { Message = "已偵測到 " + faces.Length + " 人\n"; // 將這張照片回傳的人臉,取出每一張臉的FaceId並進行轉換成Guid Guid[] faceGuids = faces.Select(x => x.FaceId).ToArray(); int pCount = 0; if (faceGuids.Length > 0) { // 透過Identify,找出在這張照片中,所有辨識出的人臉,是否有包含在PersonGroup中的所有人員 IdentifyResult[] result = await faceSC.IdentifyAsync(groupID, faceGuids); // 取得照片中在PersonGroup裡的人 for (int i = 0; i < result.Length; i++) { for (int p = 0; p < result[i].Candidates.Length; p++) { Person person = await faceSC.GetPersonAsync(groupID, result[i].Candidates[p].PersonId); string strPersonId = person.Name; pCount++; using (SqlConnection cnn9 = new SqlConnection(sqlcn)) { using (SqlCommand cmd9 = new SqlCommand()) { cmd9.Connection = cnn9; cmd9.CommandText = "INSERT INTO dbo. rollCall(studentID, classID, RCState, picID, RCTime) VALUES(@studentID, @classID, 1, @picID, @RCTime)"; cmd9.Parameters.Add("@studentID", SqlDbType.Int).Value = int.Parse(strPersonId); cmd9.Parameters.Add("@classID", SqlDbType.Int).Value = nowD; cmd9.Parameters.Add("@picID", SqlDbType.Int).Value = picID; cmd9.Parameters.Add("@RCTime", SqlDbType.DateTime).Value = System.DateTime.Now; cnn9.Open(); if (cmd9.ExecuteNonQuery() != 1) { Message = "點名失敗,請再嘗試一次!"; } cmd9.Dispose(); cnn9.Close(); } } } } Message += "成功辨識 " + pCount + " 人"; } } } //ReplyMessage isRock.LineBot.Utility.ReplyMessage(replyToken, Message, ChannelAccessToken); return(Ok()); } else { isRock.LineBot.Utility.ReplyMessage(replyToken, "else", ChannelAccessToken); return(Ok()); } } } } catch (Exception ex) { isRock.LineBot.Utility.ReplyMessage(replyToken, "錯誤訊息: " + ex.Message + "\n請聯絡工程人員!", ChannelAccessToken); return(Ok()); } return(Ok()); }
public static async Task <SimilarPersistedFace> FindSimilarPersistedFaceAsync(Func <Task <Stream> > imageStreamCallback, Guid faceId, Face face) { if (faceLists == null) { await Initialize(); } Tuple <SimilarPersistedFace, string> bestMatch = null; bool foundMatch = false; foreach (var faceListId in faceLists.Keys) { try { SimilarPersistedFace similarFace = (await FaceServiceHelper.FindSimilarAsync(faceId, faceListId))?.FirstOrDefault(); if (similarFace == null) { continue; } foundMatch = true; if (bestMatch != null) { // We already found a match for this face in another list. Replace the previous one if the new confidence is higher. if (bestMatch.Item1.Confidence < similarFace.Confidence) { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } catch (Exception e) { // Catch errors with individual face lists so we can continue looping through all lists. Maybe an answer will come from // another one. ErrorTrackingHelper.TrackException(e, "Face API FindSimilarAsync error"); } } if (!foundMatch) { // If we are here we didnt' find a match, so let's add the face to the first FaceList that we can add it to. We // might create a new list if none exist, and if all lists are full we will delete the oldest face list (based on when we // last matched anything on it) so that we can add the new one. double maxAngle = 30; if (face.FaceAttributes.HeadPose != null && (Math.Abs(face.FaceAttributes.HeadPose.Yaw) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Pitch) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Roll) > maxAngle)) { // This isn't a good frontal shot, so let's not use it as the primary example face for this person return(null); } if (!faceLists.Any()) { // We don't have any FaceLists yet. Create one string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); } AddPersistedFaceResult addResult = null; bool failedToAddToNonFullList = false; foreach (var faceList in faceLists) { if (faceList.Value.IsFull) { continue; } try { addResult = await FaceServiceHelper.AddFaceToFaceListAsync(faceList.Key, imageStreamCallback, face.FaceRectangle); break; } catch (Exception ex) { if (ex is FaceAPIException && ((FaceAPIException)ex).ErrorCode == "403") { // FaceList is full. Continue so we can try again with the next FaceList faceList.Value.IsFull = true; continue; } else { failedToAddToNonFullList = true; break; } } } if (addResult == null && !failedToAddToNonFullList) { // We were not able to add the face to an existing list because they were all full. // If possible, let's create a new list now and add the new face to it. If we can't (e.g. we already maxed out on list count), // let's delete an old list, create a new one and add the new face to it. if (faceLists.Count == 64) { // delete oldest face list var oldestFaceList = faceLists.OrderBy(fl => fl.Value.LastMatchTimestamp).FirstOrDefault(); faceLists.Remove(oldestFaceList.Key); await FaceServiceHelper.DeleteFaceListAsync(oldestFaceList.Key); } // create new list string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); // Add face to new list addResult = await FaceServiceHelper.AddFaceToFaceListAsync(newFaceListId, imageStreamCallback, face.FaceRectangle); } if (addResult != null) { bestMatch = new Tuple <SimilarPersistedFace, string>(new SimilarPersistedFace { Confidence = 1, PersistedFaceId = addResult.PersistedFaceId }, null); } } return(bestMatch?.Item1); }
public static async Task <SimilarPersistedFace> FindSimilarPersistedFaceAsync(Func <Task <Stream> > imageStreamCallback, Guid faceId, Face face) { if (faceLists == null) { await Initialize(); } Tuple <SimilarPersistedFace, string> bestMatch = null; bool foundMatch = false; foreach (var faceListId in faceLists.Keys) { try { SimilarPersistedFace similarFace = (await FaceServiceHelper.FindSimilarAsync(faceId, faceListId))?.FirstOrDefault(); if (similarFace == null) { continue; } foundMatch = true; if (bestMatch != null) { if (bestMatch.Item1.Confidence < similarFace.Confidence) { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } catch (Exception e) { ErrorTrackingHelper.TrackException(e, "Face API FindSimilarAsync error"); } } if (!foundMatch) { double maxAngle = 30; if (face.FaceAttributes.HeadPose != null && (Math.Abs(face.FaceAttributes.HeadPose.Yaw) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Pitch) > maxAngle || Math.Abs(face.FaceAttributes.HeadPose.Roll) > maxAngle)) { return(null); } if (!faceLists.Any()) { string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); } AddPersistedFaceResult addResult = null; bool failedToAddToNonFullList = false; foreach (var faceList in faceLists) { if (faceList.Value.IsFull) { continue; } try { addResult = await FaceServiceHelper.AddFaceToFaceListAsync(faceList.Key, imageStreamCallback, face.FaceRectangle); break; } catch (Exception ex) { if (ex is FaceAPIException && ((FaceAPIException)ex).ErrorCode == "403") { faceList.Value.IsFull = true; continue; } else { failedToAddToNonFullList = true; break; } } } if (addResult == null && !failedToAddToNonFullList) { if (faceLists.Count == 64) { var oldestFaceList = faceLists.OrderBy(fl => fl.Value.LastMatchTimestamp).FirstOrDefault(); faceLists.Remove(oldestFaceList.Key); await FaceServiceHelper.DeleteFaceListAsync(oldestFaceList.Key); } string newFaceListId = Guid.NewGuid().ToString(); await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); addResult = await FaceServiceHelper.AddFaceToFaceListAsync(newFaceListId, imageStreamCallback, face.FaceRectangle); } if (addResult != null) { bestMatch = new Tuple <SimilarPersistedFace, string>(new SimilarPersistedFace { Confidence = 1, PersistedFaceId = addResult.PersistedFaceId }, null); } } return(bestMatch?.Item1); }
private void MemberService_Saving(IMemberService sender, SaveEventArgs <IMember> e) { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var faceServiceClient = new FaceServiceClient(_faceApiKey, _faceApiUrl); foreach (IMember member in e.SavedEntities) { var profileImage = member.GetValue <string>("profilePicture"); if (!string.IsNullOrWhiteSpace(profileImage)) { var profileImageUdi = Udi.Parse(profileImage); var profileImageMedia = umbracoHelper.TypedMedia(profileImageUdi); string fullPath = _fs.GetFullPath(profileImageMedia.Url); /* Stap 2 -> Face API: Delete the person if exists */ if (!string.IsNullOrWhiteSpace(member.GetValue <string>("personId"))) { try { var personId = Guid.Parse(member.GetValue <string>("personId")); AsyncHelpers.RunSync(() => faceServiceClient.DeletePersonAsync(_faceApiGroup, personId)); } catch { // ignored } } /* Stap 3 -> Face API: Detect face and attributes */ using (Stream imageFileStream = _fs.OpenFile(fullPath)) { Face[] detectface = AsyncHelpers.RunSync( () => faceServiceClient.DetectAsync(imageFileStream, false, false, new[] { FaceAttributeType.Age, FaceAttributeType.Gender, FaceAttributeType.Glasses, FaceAttributeType.Makeup, FaceAttributeType.Hair, })); // Getting values and setting the properties on the member string age = detectface.First().FaceAttributes.Age.ToString(); string gender = detectface.First().FaceAttributes.Gender; string glasses = detectface.First().FaceAttributes.Glasses.ToString(); bool eyeMakeup = detectface.First().FaceAttributes.Makeup.EyeMakeup; bool lipMakeup = detectface.First().FaceAttributes.Makeup.LipMakeup; member.SetValue("Age", age); member.SetValue("Gender", gender); member.SetValue("glasses", glasses); member.SetValue("eyeMakeup", eyeMakeup); member.SetValue("lipMakeup", lipMakeup); } // ==> Stap 4 -> Create a person in the persongroup CreatePersonResult person = AsyncHelpers.RunSync(() => faceServiceClient.CreatePersonAsync(_faceApiGroup, member.Name)); member.SetValue("personId", person.PersonId.ToString()); // ==> Stap 5 -> Add face to person and make persistent using (Stream imageFileStream = _fs.OpenFile(fullPath)) { AddPersistedFaceResult result = AsyncHelpers.RunSync(() => faceServiceClient.AddPersonFaceAsync(_faceApiGroup, person.PersonId, imageFileStream)); member.SetValue("faceId", result.PersistedFaceId.ToString()); } } } // ==> Stap 6 -> Train the facegroup AsyncHelpers.RunSync(() => faceServiceClient.TrainPersonGroupAsync(_faceApiGroup)); }
/// <summary> /// /// </summary> /// <param name="imageStream"></param> /// <param name="faceId"></param> /// <param name="faceRectangle"></param> /// <param name="complexScenario"> - Whether we are identifying face hard way - means whether we need to use db or not</param> /// <returns></returns> public static async Task <Tuple <SimilarPersistedFace, string> > FindSimilarPersistedFaceAsync(Stream imageStream, Guid faceId, FaceRectangle faceRectangle) { if (faceLists == null) { await Initialize(); } Tuple <SimilarPersistedFace, string> bestMatch = null; var faceListId = faceLists.FirstOrDefault().Key; //Delete for testing purposes //await FaceServiceHelper.DeleteFaceListAsync(faceListId); try { SimilarPersistedFace similarFace = null; if (faceListId != null) { try { similarFace = (await FaceServiceHelper.FindSimilarAsync(faceId, faceListId))?.FirstOrDefault(); } catch (Exception e) { if ((e as FaceAPIException).ErrorCode == "FaceListNotReady") { // do nothing, list is empty but we continue } } } if (similarFace != null) { if (bestMatch != null) { // We already found a match for this face in another list. Replace the previous one if the new confidence is higher. if (bestMatch.Item1.Confidence < similarFace.Confidence) { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { // If we are here we didnt' find a match, so let's add the face to the first FaceList that we can add it to. We // might create a new list if none exist, and if all lists are full we will delete the oldest face list (based on when we // last matched anything on it) so that we can add the new one. if (!faceLists.Any()) { // We don't have any FaceLists yet. Create one newFaceListId = Guid.NewGuid().ToString(); // await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); //We are not using filters await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList"); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); } AddPersistedFaceResult addResult = null; var faceList = faceLists.First(); if (faceList.Value.IsFull) { //It is here only for complex scenario where we use groups and lists mappings try { DBSimilarFace faceToDelete = null; using (var db = new KioskDBContext()) { faceToDelete = db.SimilarFaces.OrderByDescending(sf => sf.PersonId).First(); db.SimilarFaces.Remove(faceToDelete); await db.SaveChangesAsync(); } await FaceServiceHelper.DeleteFaceFromFaceListAsync(faceList.Key, new Guid(faceToDelete.FaceId)); } catch (Exception e) { Debug.WriteLine("No face to be deleted" + e.Message); } } addResult = await FaceServiceHelper.AddFaceToFaceListAsync(faceList.Key, imageStream, faceRectangle); if (addResult != null) { bestMatch = new Tuple <SimilarPersistedFace, string>(new SimilarPersistedFace { Confidence = 1, PersistedFaceId = addResult.PersistedFaceId }, null); } } } catch (Exception e) { // Catch errors with individual face lists so we can continue looping through all lists. Maybe an answer will come from // another one. ErrorTrackingHelper.TrackException(e, "Face API FindSimilarAsync error"); } return(bestMatch); }
private async void ExecuteFaceTask(List <int> mFaceIndices) { AddPersistedFaceResult result = null; bool mSucceed = true; mProgressDialog.Show(); try { var faceClient = new FaceClient(); UUID personId = UUID.FromString(mPersonId); using (MemoryStream pre_output = new MemoryStream()) { mBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, pre_output); using (ByteArrayInputStream inputStream = new ByteArrayInputStream(pre_output.ToArray())) { byte[] arr = new byte[inputStream.Available()]; inputStream.Read(arr); var output = new MemoryStream(arr); mProgressDialog.SetMessage("Adding face..."); SetInfo("Adding face..."); foreach (int index in mFaceIndices) { FaceRectangle faceRect = mFaceGridViewAdapter.faceRectList[index]; AddLog("Request: Adding face to person " + mPersonId); result = await faceClient.AddPersonFace(mPersonGroupId, personId, output, "User data", faceRect); mFaceGridViewAdapter.faceIdList[index] = result.PersistedFaceId; } } } } catch (Java.Lang.Exception e) { mSucceed = false; AddLog(e.Message); } RunOnUiThread(() => { mProgressDialog.Dismiss(); if (mSucceed) { String faceIds = ""; foreach (int index in mFaceIndices) { String faceId = mFaceGridViewAdapter.faceIdList[index].ToString(); faceIds += faceId + ", "; try { var file = System.IO.Path.Combine(Application.Context.FilesDir.Path, faceId); using (var fs = new FileStream(file, FileMode.OpenOrCreate)) { mFaceGridViewAdapter.faceThumbnails[index].Compress(Bitmap.CompressFormat.Jpeg, 100, fs); } Android.Net.Uri uri = Android.Net.Uri.Parse(file); StorageHelper.SetFaceUri(faceId, uri.ToString(), mPersonId, this); } catch (Java.IO.IOException e) { SetInfo(e.Message); } } AddLog("Response: Success. Face(s) " + faceIds + "added to person " + mPersonId); Finish(); } }); }
/// <summary> /// Used to take one person from one group and add them to another. (Does not delete person from first person group) /// </summary> /// <param name="strPersonGroupId"></param> /// <param name="uidPersonId"></param> /// <param name="imgStrImageStream"></param> /// <param name="strUserData"></param> /// <param name="frTargetFace"></param> /// <returns></returns> public static async Task <AddPersistedFaceResult> AddExsitingPersonToPersonGroup(string strPersonGroupId, Guid uidPersonId, Stream imgStrImageStream, string strUserData, FaceRectangle frTargetFace) { AddPersistedFaceResult item = await fsClient.AddPersonFaceAsync(strPersonGroupId, uidPersonId, imgStrImageStream, strUserData, frTargetFace); return(item); }
public async Task <string> GetPersonsAsync(string personGroupId, string personId, string imageUrl) { AddPersistedFaceResult res = await faceClient.AddPersonFaceAsync(personGroupId, new Guid(personId), imageUrl); return(res.PersistedFaceId.ToString()); }
/// <summary> /// 加入人臉 /// </summary> public async void AddFace(string fullImgPath) { byte[] data = File.ReadAllBytes(fullImgPath); MemoryStream stream = new MemoryStream(data); AddPersistedFaceResult result = await faceServiceClient.AddPersonFaceAsync(personGroupId, Guid.Parse("3C3F4783-F2B7-4FB6-96D4-01D53ECB608B"), stream); }
public static async Task <SimilarPersistedFace> FindSimilarPersistedFaceAsync(Stream imageStream, Guid faceId, FaceRectangle faceRectangle, string faceIdToDelete = "") { if (faceLists == null) { await Initialize(); } Tuple <SimilarPersistedFace, string> bestMatch = null; var faceListId = faceLists.FirstOrDefault().Key; try { SimilarPersistedFace similarFace = (await FaceServiceHelper.FindSimilarAsync(faceId, faceListId))?.FirstOrDefault(); if (similarFace != null) { if (bestMatch != null) { // We already found a match for this face in another list. Replace the previous one if the new confidence is higher. if (bestMatch.Item1.Confidence < similarFace.Confidence) { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { bestMatch = new Tuple <SimilarPersistedFace, string>(similarFace, faceListId); } } else { // If we are here we didnt' find a match, so let's add the face to the first FaceList that we can add it to. We // might create a new list if none exist, and if all lists are full we will delete the oldest face list (based on when we // last matched anything on it) so that we can add the new one. if (!faceLists.Any()) { // We don't have any FaceLists yet. Create one string newFaceListId = Guid.NewGuid().ToString(); // await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList", FaceListsUserDataFilter); //We are not using filters await FaceServiceHelper.CreateFaceListAsync(newFaceListId, "ManagedFaceList"); faceLists.Add(newFaceListId, new FaceListInfo { FaceListId = newFaceListId, LastMatchTimestamp = DateTime.Now }); } AddPersistedFaceResult addResult = null; var faceList = faceLists.First(); if (faceList.Value.IsFull) { await FaceServiceHelper.DeleteFaceFromFaceListAsync(faceList.Key, new Guid(faceIdToDelete)); addResult = await FaceServiceHelper.AddFaceToFaceListAsync(faceList.Key, imageStream, faceRectangle); } if (addResult != null) { bestMatch = new Tuple <SimilarPersistedFace, string>(new SimilarPersistedFace { Confidence = 1, PersistedFaceId = addResult.PersistedFaceId }, null); } } } catch (Exception e) { // Catch errors with individual face lists so we can continue looping through all lists. Maybe an answer will come from // another one. ErrorTrackingHelper.TrackException(e, "Face API FindSimilarAsync error"); } return(bestMatch?.Item1); }
/// <summary> /// Sends images to the Azure Face Resource /// </summary> /// <param name="sender">A sender object</param> /// <param name="e">RoutedEventArgs</param> /// <remarks> /// <para>Submits the folder created in <c>CreateFolderButton_ClickAsync</c>.</para> /// <para>Images should already be stored in the folder</para> /// </remarks> private async void SubmitToAzureButton_ClickAsync(object sender, RoutedEventArgs e) { //Clear Globals string successfullySubmitted = string.Empty; //Reset UI Globals TrainStatusTextBlock.Text = ""; //Reset UI Colors TrainStatusTextBlock.Foreground = new SolidColorBrush(Colors.Black); SubmissionStatusTextBlock.Foreground = new SolidColorBrush(Colors.Green); //Logic int imageCounter = 0; if (null != personFolder) { var items = await personFolder.GetFilesAsync(); if (items.Count > 0) { List <StorageFile> imageFilesToUpload = new List <StorageFile>(); foreach (StorageFile item in items) { //Windows Cam default save type is jpg if (item.FileType.ToLower() == ".jpg" || item.FileType.ToLower() == ".png") { imageCounter++; imageFilesToUpload.Add(item); } else { Debug.WriteLine(string.Format("Photo {0}, from {1}, is in the wrong format. Images must be jpg or png!", item.DisplayName, item.Path)); } } if (imageCounter >= minPhotos) { imageCounter = 0; try { foreach (StorageFile imageFile in imageFilesToUpload) { imageCounter++; using (Stream s = await imageFile.OpenStreamForReadAsync()) { await ApiCallAllowed(true); AddPersistedFaceResult addResult = await faceServiceClient.AddPersonFaceAsync(personGroupId, personId, s); Debug.WriteLine("Add result: " + addResult + addResult.PersistedFaceId); } SubmissionStatusTextBlock.Text = string.Format("Submission Status: {0}", imageCounter); } SubmissionStatusTextBlock.Text = "Submission Status: Total Images submitted: " + imageCounter; } catch (Exception ex) { Debug.WriteLine("Submission Exc: " + ex.Message); } } else { SubmissionStatusTextBlock.Text = $"Submission Status: Please add at least {minPhotos} face images to the person folder."; } } else { successfullySubmitted = "Submission Status: No Image Files Found."; } } else { successfullySubmitted = "Submission Status: No person folder found! Have you completed section five?"; } if (successfullySubmitted != string.Empty) { SubmissionStatusTextBlock.Foreground = new SolidColorBrush(Colors.Red); SubmissionStatusTextBlock.Text = successfullySubmitted; } else { SubmissionStatusTextBlock.Text = "Submission completed successfully! Now train your service!"; } }
static void Main(string[] args) { //Input oxfordKey Console.WriteLine("Please input the OxfordPrimaryKey: "); string oxfordProjectKey = Console.ReadLine(); FaceServiceClient faceClient = new FaceServiceClient(oxfordProjectKey); //Create PersonGroup string groupName = ""; string groupId = ""; Console.WriteLine("Create a new Person Group? [Y/N]"); string personGroupChoice = Console.ReadLine(); if (personGroupChoice == "Y") { Console.WriteLine("Please input the PersonGroup Name: "); groupName = Console.ReadLine(); groupId = Guid.NewGuid().ToString(); var runSync = Task.Factory.StartNew(new Func <Task>(async() => { await faceClient.CreatePersonGroupAsync(groupId, groupName); })).Unwrap(); runSync.Wait(); } else { Console.WriteLine("Please input the PersonGroup Id: "); groupId = Console.ReadLine(); } Console.WriteLine("Adding person and his photos......"); //Add Persons and Photos DirectoryInfo dirPrograms = new DirectoryInfo(Environment.CurrentDirectory); List <DirectoryInfo> dirs = new List <DirectoryInfo>(dirPrograms.EnumerateDirectories()); foreach (DirectoryInfo dirsplit in dirs) { string lastName = dirsplit.Name.Substring(dirsplit.Name.IndexOf("_") + 1, dirsplit.Name.Length - dirsplit.Name.IndexOf("_") - 1); string firstName = dirsplit.Name.Substring(0, dirsplit.Name.IndexOf("_")); //Create Person CreatePersonResult personResult = null; var runSync = Task.Factory.StartNew(new Func <Task>(async() => { personResult = await faceClient.CreatePersonAsync(groupId, firstName + " " + lastName); })).Unwrap(); runSync.Wait(); Console.WriteLine("Creating " + firstName + " " + lastName); //Add photos List <FileInfo> files = new List <FileInfo>(dirsplit.EnumerateFiles()); foreach (FileInfo filesplit in files) { FileStream fs0 = new FileStream(filesplit.Directory + "\\" + filesplit.Name, FileMode.Open); byte[] bytes = new byte[fs0.Length]; fs0.Read(bytes, 0, bytes.Length); fs0.Close(); Stream imageStream = new MemoryStream(bytes); AddPersistedFaceResult perFaceResult = null; runSync = Task.Factory.StartNew(new Func <Task>(async() => { perFaceResult = await faceClient.AddPersonFaceAsync(groupId, personResult.PersonId, imageStream); })).Unwrap(); runSync.Wait(); } } //Train and get training status faceClient.TrainPersonGroupAsync(groupId); TrainingStatus trStatus = null; do { Console.WriteLine("Waiting for training."); Thread.Sleep(3000); var runSync = Task.Factory.StartNew(new Func <Task>(async() => { trStatus = await faceClient.GetPersonGroupTrainingStatusAsync(groupId); })).Unwrap(); runSync.Wait(); } while (trStatus == null || trStatus.Status == Status.Running); Console.WriteLine("TrainingStatus: " + trStatus.Status.ToString()); //Write the info to txt file string data1 = "oxfordKey: " + oxfordProjectKey; string data2 = "PersonGroupId: " + groupId; StreamWriter sw = new StreamWriter("OxfordData.txt", false, Encoding.Default); sw.WriteLine(data1); sw.WriteLine(data2); sw.Close(); Console.ReadLine(); }