public async Task <bool> DetectIfPersonIsTrustedAsync(byte[] detectedPersonImageData) { var faceDetectResponse = (await this.faceDetectService.DetectFaceAsync(detectedPersonImageData))?.FirstOrDefault(); if (string.IsNullOrWhiteSpace(faceDetectResponse?.FaceId)) { return(false); } var detectedFaceId = faceDetectResponse.FaceId; var faceIdentifyRequest = new FaceIdentifyRequest { FaceIds = new List <string> { detectedFaceId }, PersonGroupId = personGroupId, ConfidenceThreshold = 0.5f, MaxNumOfCandidatesReturned = 1 }; var faceIdentifyResponse = (await this.faceIdentifyService.IdentifyFaceAsync(faceIdentifyRequest))?.FirstOrDefault(); return(faceIdentifyResponse?.Candidates?.Count > 0); }
public static ByteArrayContent GetIdentifyHttpContent(FaceIdentifyRequest request) { string strRequest = JsonConvert.SerializeObject(request); byte[] byteData = Encoding.UTF8.GetBytes(strRequest); var content = new ByteArrayContent(byteData); // Add application/octet-stream header for the content. content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return(content); }
public async Task <FaceIdentifyResponse> IdentifyFaceAsync(FaceIdentifyRequest faceIdentifyRequest) { FaceIdentifyResponse result = null; try { result = await PostAsync <FaceIdentifyResponse, FaceIdentifyRequest>(IdentityEndpoint, faceIdentifyRequest, (request) => this.ConfigureRequestWithSubscriptionHeader(request)); } catch { // TODO: Log error - note that this will fail if the model has not yet been trained } return(result); }
private async Task <FaceDetectResult[]> MakeFaceIdentifyRequest(FaceIdentifyRequest req, string apiKey, Guid requestId, PolicyWrap <HttpResponseMessage> policy) { string strResult = string.Empty; StringBuilder faceids = new StringBuilder(); req.FaceIds.ToList().ForEach(s => faceids.Append(s + " ")); _log.Info($"Making identify request faceids: {faceids} requestId: {requestId} apiKey:{apiKey} ticks: {DateTime.Now.Ticks}"); FaceDetectResult[] result = null; // Get the API URL and the API key from settings. var uri = ConfigurationManager.AppSettings["faceIdentifyApiUrl"]; // Configure the HttpClient request headers. _client.DefaultRequestHeaders.Clear(); _client.DefaultRequestHeaders.Accept.Clear(); _client.DefaultRequestHeaders.Add("Ocp-Apim-Subscription-Key", apiKey); //_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); try { // Execute the REST API call, implementing our resiliency strategy. HttpResponseMessage response = await policy.ExecuteAsync(() => _client.PostAsync(uri, FaceHelper.GetIdentifyHttpContent(req))); // Get the JSON response. strResult = await response.Content.ReadAsStringAsync(); result = await response.Content.ReadAsAsync <FaceDetectResult[]>(); _log.Info($"identify completed: {strResult} requestId: {requestId} apiKey:{apiKey} ticks: {DateTime.Now.Ticks}"); } catch (BrokenCircuitException bce) { _log.Error($"Could not contact the Face API service due to the following error: {bce.Message} requestId: {requestId} apiKey:{apiKey} ticks: {DateTime.Now.Ticks}", bce); } catch (Exception e) { _log.Error($"Critical error-MakeFaceIdentifyRequest: {e.Message} requestId: {requestId} apiKey:{apiKey} string result:{strResult} ticks: {DateTime.Now.Ticks}", e); } return(result); }
public static async Task <bool> RunDetect(Guid requestID, string apis, string name, string source, Stream incomingPicture, string sourceContainerName, string resultContainerName, IAsyncCollector <object> outputItem, TraceWriter log, string videoName = null) { string apikey = string.Empty; try { string[] apiArr = apis.Split(','); int randomApi = FaceHelper.Instance.Next(0, apiArr.Length); apikey = apiArr[randomApi]; log.Info($"RunDetect request id: {requestID} apiKey: {apikey} ticks: {DateTime.Now.Ticks}"); Tuple <HttpClient, PolicyWrap <HttpResponseMessage> > tuple = FaceHelper.HttpClientList.GetOrAdd(apikey, new Tuple <HttpClient, PolicyWrap <HttpResponseMessage> >( new HttpClient(), FaceHelper.DefineAndRetrieveResiliencyStrategy(log))); HttpClient client = tuple.Item1; PolicyWrap <HttpResponseMessage> policy = tuple.Item2; IDatabase cache = FaceHelper.Connection.GetDatabase(int.Parse(FaceHelper.Connection.GetDatabase(1).StringGet(apikey))); //the large group id it's based on the mac address we get - each MAC address can work with different face api group string largegroupid = ConfigurationManager.AppSettings[source]; if (videoName == null) { log.Info($"Detecting {name} requestId: {requestID} apiKey: {apikey} ticks: {DateTime.Now.Ticks}"); } else { log.Info($"Detecting thumbnail {name} from {videoName} requestId: {requestID} apiKey: {apikey} ticks: {DateTime.Now.Ticks}"); } byte[] pictureImage; // Convert the incoming image stream to a byte array. using (var br = new BinaryReader(incomingPicture)) { pictureImage = br.ReadBytes((int)incomingPicture.Length); } var detectionResult = await new FaceDetect(log, client).DetectFaces(pictureImage, apikey, requestID, policy); if ((detectionResult != null) && (detectionResult.Length > 0)) { //prepare identify request int maxCandidate = int.Parse(ConfigurationManager.AppSettings["maxNumOfCandidatesReturned"]); double threshold = double.Parse(ConfigurationManager.AppSettings["confidenceThreshold"]); var identifyResquest = new FaceIdentifyRequest() { ConfidenceThreshold = threshold, MaxNumOfCandidatesReturned = maxCandidate, LargePersonGroupId = largegroupid, FaceIds = detectionResult.Select(s => s.FaceId).ToArray() }; var identifyResult = await new FaceIdentify(log, client).IdentifyFaces(identifyResquest, apikey, requestID, policy); if ((identifyResult == null) || (identifyResult.Length == 0)) { log.Info($"No identification result requestId: {requestID}, apiKey:{apikey} ticks: {DateTime.Now.Ticks}"); } var personResult = new PersonDetails(log, client); //merging results and find person name for (int i = 0; i < detectionResult.Length; i++) { if (videoName == null) { detectionResult[i].FaceBlobName = string.Concat(detectionResult[i].FaceId, "_", name); } else { detectionResult[i].FaceBlobName = videoName + "/" + name; } if ((identifyResult != null) && (identifyResult.Length > 0)) { detectionResult[i].Candidates = identifyResult[i].Candidates; for (int j = 0; j < detectionResult[i].Candidates.Length; j++) { string personid = detectionResult[i].Candidates[j].PersonId.ToString(); string personName = cache.StringGet(largegroupid + "-" + personid); if (string.IsNullOrEmpty(personName) == true) { log.Info($"Missing cache requestId: {requestID} apiKey: {apikey} personId: {personid} ticks: {DateTime.Now.Ticks}"); var tPerson = await personResult.GetPersonName(personid, apikey, largegroupid, requestID, policy); personName = tPerson.Name; cache.StringSet(largegroupid + "-" + personid, personName, null, When.Always); } detectionResult[i].Candidates[j].PersonName = new InternalPersonDetails() { PersonId = detectionResult[i].Candidates[j].PersonId, Name = personName }; } } } } else { log.Info($"No dectection result requestId: {requestID}, apiKey:{apikey} ticks: {DateTime.Now.Ticks}"); //in case of video, we want to create a link to the face detected by AMS (Azure Media Services) although face api didn't recognize it if (videoName != null) { detectionResult = new FaceDetectResult[1] { new FaceDetectResult() { FaceBlobName = videoName + "/" + name } } } ; } string blobname = videoName ?? name; var actionResult = new FaceIdentifyResult() { BlobName = blobname, ContainerName = sourceContainerName, ResultContainerName = resultContainerName, BlobLength = incomingPicture.Length, CreatedDateTime = DateTime.UtcNow, RequestId = requestID, ApiKey = apikey, LargeGroupId = largegroupid, Source = source, DetectResultList = detectionResult }; string strResult = JsonConvert.SerializeObject(actionResult); await outputItem.AddAsync(strResult); } catch (Exception ex) { log.Error($"Exception Message: {ex.Message}, requestId: {requestID}, apiKey:{apikey} ticks: {DateTime.Now.Ticks}", ex); return(false); } return(true); }
public async Task <FaceDetectResult[]> IdentifyFaces(FaceIdentifyRequest req, string apiKey, Guid requestId, PolicyWrap <HttpResponseMessage> policy) { return(await MakeFaceIdentifyRequest(req, apiKey, requestId, policy)); }