public class FaceAPIHelper
{
private readonly IFaceServiceClient _faceClient =
new FaceServiceClient("key"
, "https://api.cognitive.azure.cn/face/v1.0");
private string guid = null;
private Guid personId;
private Person[] arr = null;
//画信息
//public async dynamic sendFaceInfo()
//{
// Face[] faces = await null;
//}
//监测人脸信息
public async Task<LiveCameraResult> FacesAnalysisFunction(MemoryStream stream, FaceInfo Model)
{
try
{
// Encode image.
// var jpg = frame.ToMemoryStream(".jpg", s_jpegParams);
// Submit image to API.
//var attrs = new List<FaceAttributeType> {
//FaceAttributeType.Age,
//FaceAttributeType.Gender,
//FaceAttributeType.HeadPose ,
//FaceAttributeType.Emotion,
//FaceAttributeType.Smile,
//FaceAttributeType.Blur,
//FaceAttributeType.Noise,
//FaceAttributeType.Makeup,
//FaceAttributeType.Hair,
//FaceAttributeType.Glasses};
// var faces = await _faceClient.DetectAsync(stream, returnFaceAttributes: attrs);
// await AddPersonGroup(Name);
PersonGroup[] arr=await GetPersonGroupsAsync();
if (arr.Length>0)
{
//存在
guid = "1cb5d278-2e43-49cd-aee3-bf8f48363f90"; //arr[1].PersonGroupId;
}
else
{
await AddPersonGroup(Model.Name);
}
CreatePersonResult info= await AddPersonGroupPerson(Model.Name);
AddPersistedFaceResult faceInfo = null;
if (info!=null)
{
personId = info.PersonId;
faceInfo=await AddPersonGroupPersonFace(stream,Model);
}
return null;
// new LiveCameraResult { Faces = faces };
}
// Catch and display Face API errors.
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
return null;
}
// Catch and display all other errors.
catch (Exception e)
{
LogHelper.Error(e.Message, LogSources.FaceAPI.ToString(), e);
return null;
}
}
public async Task<dynamic> FacesAnalysisFunctions(MemoryStream stream, FaceInfo Model,string groupId)
{
try
{
CreatePersonResult info = await AddPersonGroupPerson(Model.Name,groupId);
AddPersistedFaceResult faceInfo = null;
if (info != null)
{
personId = info.PersonId;
guid = groupId;
faceInfo = await AddPersonGroupPersonFace(stream, Model);
}
return faceInfo;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
return null;
}
// Catch and display all other errors.
catch (Exception e)
{
LogHelper.Error(e.Message, LogSources.FaceAPI.ToString(), e);
return null;
}
}
public async Task<string> FacesAnalysisFunctions(string Name, string groupId)
{
CreatePersonResult info = null;
try
{
// dynamic data= await GetPersonsAsync(groupId);
info = await AddPersonGroupPerson(Name, groupId);
if (info!=null)
{
return info.PersonId.ToString();
}
else
{
return null;
}
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
return null;
}
// Catch and display all other errors.
catch (Exception e)
{
LogHelper.Error(e.Message, LogSources.FaceAPI.ToString(), e);
return null;
}
}
//获取对应人脸
public async Task<LiveCameraResult> FacesAnalysisFunction(string url)
{
Face[] faces = null;
try
{
// Encode image.
// var jpg = frame.ToMemoryStream(".jpg", s_jpegParams);
// Submit image to API.
var faceAttributes = new List<FaceAttributeType> {
FaceAttributeType.Age,
FaceAttributeType.Gender,
FaceAttributeType.HeadPose ,
FaceAttributeType.Emotion,
FaceAttributeType.Smile,
FaceAttributeType.Blur,
FaceAttributeType.Noise,
FaceAttributeType.Makeup,
FaceAttributeType.Hair,
FaceAttributeType.Glasses};
// Call the Face API.
using (Stream imageFileStream = File.OpenRead(url))
{
faces = await _faceClient.DetectAsync(imageFileStream, returnFaceId: true, returnFaceLandmarks: false,returnFaceAttributes: faceAttributes);
return new LiveCameraResult { Faces = faces };
}
}
// Catch and display Face API errors.
catch (FaceAPIException f)
{
// MessageBox.Show(f.ErrorMessage, f.ErrorCode);
return new LiveCameraResult { Faces = faces };
}
// Catch and display all other errors.
catch (Exception e)
{
return new LiveCameraResult { Faces = faces };
}
}
//先加组
public async Task<bool> AddPersonGroup(string Name)
{
guid = System.Guid.NewGuid().ToString();
bool falg = false;
try
{
await _faceClient.CreatePersonGroupAsync(guid, Name);
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(),ex);
}
return falg;
}
//删除组
public async Task<bool> DeletePersonGroup(string personGroupId)
{
bool falg = false;
try
{
await _faceClient.DeletePersonGroupAsync(personGroupId);
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(), ex);
}
return falg;
}
//删除人
public async Task<bool> DeletePersonGroup(string personGroupId,string personId)
{
guid = System.Guid.NewGuid().ToString();
bool falg = false;
try
{
await _faceClient.DeletePersonAsync(personGroupId, new Guid(personId));
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(), ex);
}
return falg;
}
//更新组
public async Task<bool> UpdatePersonGroup(string personGroupId, string Name)
{
bool falg = false;
try
{
await _faceClient.UpdatePersonGroupAsync(personGroupId,Name);
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(), ex);
}
return falg;
}
//更新人
public async Task<bool> UpdatePerson(string personGroupId, string personId, string Name)
{
bool falg = false;
try
{
await _faceClient.UpdatePersonAsync(personGroupId, new Guid(personId),Name);
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(), ex);
}
return falg;
}
//先加组
public async Task<bool> AddPersonGroup(string id,string Name)
{
bool falg = false;
try
{
await _faceClient.CreatePersonGroupAsync(id, Name);
falg = true;
}
catch (FaceAPIException f)
{
LogHelper.Error(f.Message, LogSources.FaceAPI.ToString(), f);
}
catch (Exception ex)
{
LogHelper.Error(ex.Message, LogSources.FaceAPI.ToString(), ex);
}
return falg;
}
//再加人 返回personId
public async Task<CreatePersonResult> AddPersonGroupPerson(string Name)
{
CreatePersonResult info = null;
try
{
info = await _faceClient.CreatePersonInPersonGroupAsync(guid, Name);
return info;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_AddPersonGroupPerson", LogSources.FaceAPI, ex);
}
return info;
}
public async Task<CreatePersonResult> AddPersonGroupPerson(string Name,string groupId)
{
CreatePersonResult info = null;
try
{
info = await _faceClient.CreatePersonInPersonGroupAsync(groupId, Name);
return info;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_AddPersonGroupPerson", LogSources.FaceAPI, ex);
}
return info;
}
//最后加人脸
public async Task<AddPersistedFaceResult> AddPersonGroupPersonFace(MemoryStream stream,FaceInfo model)
{
AddPersistedFaceResult info = null;
try
{
info = await _faceClient.AddPersonFaceInPersonGroupAsync(guid, personId, stream, Newtonsoft.Json.JsonConvert.SerializeObject(model));
return info;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_AddPersonGroupPersonFace", LogSources.FaceAPI, ex);
}
return info;
}
//ALL
public async Task<PersonGroup[]> GetPersonGroupsAsync()
{
try
{
var lst = await _faceClient.ListPersonGroupsAsync();
return lst;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_GetPersonGroupsAsync", LogSources.FaceAPI, ex);
return null;
}
}
//获取组下面的人
public async Task<Person[]> GetPersonsAsync(string groupId)
{
try
{
return await _faceClient.ListPersonsInPersonGroupAsync(groupId);
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_GetPersonsAsync", LogSources.FaceAPI, ex);
return null;;
}
}
public async Task<LiveCameraResult> DetectAsync(Stream stream)
{
try
{
var attrs = new List<FaceAttributeType> {
FaceAttributeType.Age,
FaceAttributeType.Gender,
FaceAttributeType.HeadPose ,
FaceAttributeType.Emotion,
FaceAttributeType.Smile,
FaceAttributeType.Blur,
FaceAttributeType.Noise,
FaceAttributeType.Makeup,
FaceAttributeType.Hair,
FaceAttributeType.Glasses};
Face[] faces = await _faceClient.DetectAsync(stream, returnFaceAttributes: attrs);
Guid[] arrId = null;
if (faces.Length > 0)
{
foreach (var item in faces)
{
arrId[0] = item.FaceId;
}
}
await Identify(arrId);
return new LiveCameraResult { Faces = faces };
}
catch (FaceAPIException ex)
{
LogHelper.Error("FaceAPIHelper_DetectAsync", LogSources.FaceAPI, ex);
return null;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_DetectAsync", LogSources.FaceAPI, ex);
return null;
}
}
//单个
public async Task<Person> GetPersonGroup()
{
return await _faceClient.GetPersonInPersonGroupAsync(guid, personId);
}
//标识
public async Task<IdentifyResult[]> Identify(Guid[] faceId)
{
try
{
IdentifyResult[] info = await _faceClient.IdentifyAsync(faceId);
return info;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_GetPersonsAsync", LogSources.FaceAPI, ex);
}
return null;
}
//组训练状态
public async Task<TrainingStatus> GetPersonGroupTrainingStatusAsync(string personGroupId)
{
return await _faceClient.GetPersonGroupTrainingStatusAsync(personGroupId);
}
//删除人和脸
public async Task<bool> DeleteFace(string personGroupId,string PersonId)
{
bool falg = false;
try
{
if (!string.IsNullOrEmpty(personGroupId) &&!string.IsNullOrEmpty(PersonId))
{
await _faceClient.DeletePersonFromPersonGroupAsync(personGroupId, new Guid(PersonId));
falg = true;
}
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_DeleteFace", LogSources.FaceAPI, ex);
}
return falg;
}
public async Task<bool> DeletePerson(string personGroupId,string personId)
{
bool falg = false;
try
{
if (!string.IsNullOrEmpty(personGroupId)&& !string.IsNullOrEmpty(personId))
{
await _faceClient.DeletePersonFromPersonGroupAsync(personGroupId, new Guid(personId));
falg = true;
}
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_DeleteFace", LogSources.FaceAPI, ex);
}
return falg;
}
public async Task<bool> DeleteFace(string personGroupId, string personId,string faceId)
{
bool falg = false;
try
{
if (!string.IsNullOrEmpty(personGroupId) && !string.IsNullOrEmpty(personId)
&& !string.IsNullOrEmpty(faceId))
{
await _faceClient.DeletePersonFaceFromPersonGroupAsync(personGroupId, new Guid(personId),new Guid(faceId));
falg = true;
}
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_DeleteFace", LogSources.FaceAPI, ex);
}
return falg;
}
public async Task<AddPersistedFaceResult> AddFace(string groupId,Stream stream,FaceInfo Model)
{
bool falg = false;
AddPersistedFaceResult info = null;
try
{
arr = null;
arr = await GetPersonsAsync(groupId);
if (arr!=null&&arr.Length>0)
{
foreach (var item in arr)
{
if (Model.Name.Equals(item.Name))
{
personId = item.PersonId;
}
}
personId = new Guid("2BF361FD-87D4-4CD1-8B98-C6A5D5325116");
var lst = await _faceClient.GetPersonInPersonGroupAsync(groupId, personId);
info = await _faceClient.AddPersonFaceInPersonGroupAsync(groupId, personId, stream, Newtonsoft.Json.JsonConvert.SerializeObject(Model));
//falg = true;
}
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_UpdateFace", LogSources.FaceAPI, ex);
}
return info;
}
public async Task<string> AddFace(string groupId,string personId, Stream stream, string Name)
{
AddPersistedFaceResult info = null;
try
{
if (!string.IsNullOrEmpty(groupId) && !string.IsNullOrEmpty(personId))
{
info= await _faceClient.AddPersonFaceInPersonGroupAsync(groupId, new Guid(personId), stream, Name);
if (info!=null)
{
return info.PersistedFaceId.ToString();
}
}
}
catch (FaceAPIException ex)
{
LogHelper.Error("FaceAPIHelper_UpdateFace", LogSources.FaceAPI, ex);
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_UpdateFace", LogSources.FaceAPI, ex);
}
return null;
}
public async Task<dynamic> GetFace()
{
try
{
await _faceClient.TrainPersonGroupAsync(guid);
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_GetFace", LogSources.FaceAPI, ex);
}
return null;
}
public async Task< bool> Train(string groupId)
{
bool falg = false;
try
{
// var data = await _faceClient.GetPersonGroupTrainingStatusAsync(groupId);
// var status = data.Status;
await _faceClient.TrainPersonGroupAsync(groupId);
falg = true;
}
catch (Exception ex)
{
LogHelper.Error("FaceAPIHelper_Train", LogSources.FaceAPI, ex);
}
return falg;
}
}
//z照片画框
private string FaceDescription(Face face)
{
StringBuilder sb = new StringBuilder();
sb.Append("Face: ");
// Add the gender, age, and smile.
sb.Append(face.FaceAttributes.Gender);
sb.Append(", ");
sb.Append(face.FaceAttributes.Age);
sb.Append(", ");
sb.Append(String.Format("smile {0:F1}%, ", face.FaceAttributes.Smile * 100));
// // Add the emotions. Display all emotions over 10%.
sb.Append("Emotion: ");
Microsoft.ProjectOxford.Common.Contract.EmotionScores emotionScores = face.FaceAttributes.Emotion;
if (emotionScores.Anger >= 0.1f) sb.Append(String.Format("anger {0:F1}%, ", emotionScores.Anger * 100));
if (emotionScores.Contempt >= 0.1f) sb.Append(String.Format("contempt {0:F1}%, ", emotionScores.Contempt * 100));
if (emotionScores.Disgust >= 0.1f) sb.Append(String.Format("disgust {0:F1}%, ", emotionScores.Disgust * 100));
if (emotionScores.Fear >= 0.1f) sb.Append(String.Format("fear {0:F1}%, ", emotionScores.Fear * 100));
if (emotionScores.Happiness >= 0.1f) sb.Append(String.Format("happiness {0:F1}%, ", emotionScores.Happiness * 100));
if (emotionScores.Neutral >= 0.1f) sb.Append(String.Format("neutral {0:F1}%, ", emotionScores.Neutral * 100));
if (emotionScores.Sadness >= 0.1f) sb.Append(String.Format("sadness {0:F1}%, ", emotionScores.Sadness * 100));
if (emotionScores.Surprise >= 0.1f) sb.Append(String.Format("surprise {0:F1}%, ", emotionScores.Surprise * 100));
sb.Append(face.FaceAttributes.Glasses);
sb.Append(", ");
sb.Append("Hair: ");
if (face.FaceAttributes.Hair.Bald >= 0.01f)
sb.Append(String.Format("bald {0:F1}% ", face.FaceAttributes.Hair.Bald * 100));
HairColor[] hairColors = face.FaceAttributes.Hair.HairColor;
foreach (HairColor hairColor in hairColors)
{
if (hairColor.Confidence >= 0.1f)
{
sb.Append(hairColor.Color.ToString());
sb.Append(String.Format(" {0:F1}% ", hairColor.Confidence * 100));
}
}
Return the built string.
return sb.ToString();
}
public class LiveCameraResult
{
public Microsoft.ProjectOxford.Face.Contract.Face[] Faces { get; set; } = null;
public Microsoft.ProjectOxford.Common.Contract.EmotionScores[] EmotionScores { get; set; } = null;
public string[] CelebrityNames { get; set; } = null;
}
C# 微软 FaceAPI 人脸识别
最新推荐文章于 2021-03-15 19:50:08 发布