private static async Task <string> Identify(string filePath) { using (var client = new HttpClient()) { client.BaseAddress = new Uri(ServiceUrl); client.DefaultRequestHeaders.Accept.Clear(); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var userToRecognize = new IdentificationRequest { Image = File.ReadAllBytes(filePath) }; var response = await client.PostAsync("api/user", new StringContent(JsonConvert.SerializeObject(userToRecognize), Encoding.UTF8, "application/json")); if (response.IsSuccessStatusCode) { var user = await response.Content.ReadAsStringAsync(); Console.WriteLine($"User identified : {user}"); return(user); } Console.WriteLine("Error while identifying the user !"); return(null); } }
public IdentificationResponse Identify(IdentificationRequest request) { Debug.WriteLine("Request!"); //var request = JsonConvert.DeserializeObject<IdentificationRequest>(requestJson); //decode Bitmap bmp = new Bitmap(request.Width, request.Height, PixelFormat.Format8bppIndexed); var rect = new Rectangle(0, 0, request.Width, request.Height); var bmpData = bmp.LockBits(rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); var ptr = bmpData.Scan0; for (var i = 0; i < request.Height; i++) { Marshal.Copy(request.Pixels, i * request.Width, ptr, request.Width); ptr += bmpData.Stride; } bmp.UnlockBits(bmpData); var objName = ml.IdentifyObject(bmp); Console.WriteLine("Identified object = '" + objName + "'"); var response = new IdentificationResponse() { ObjectName = objName }; return(response); }
public override void BuildTo(string path) { path = Directory.CreateDirectory(Path.Combine(path, "IDENTIFICATION", _provider)).FullName; RaiseInformation($"Начинаю конвертацию [{_file}]"); int count = _records.Count(); double parts = Math.Ceiling(Convert.ToDouble(count) / Convert.ToDouble(_size)); RaiseInformation($" Будет обработано записей: {count}"); RaiseInformation($" Будет создано частей: {parts}"); IdentificationRequest request; int part = 0; do { string fileName = $"IDENTIFICATION-REQ-012-{_provider}-{_fileNumber:00}-{(part + 1):000}.XML"; RaiseInformation($" Формируется файл [{fileName}]"); var items = _records.Skip(part * _size).Take(_size); count = items.Count(); RaiseInformation($" Будет добавлено записей {count}"); request = new IdentificationRequest(fileName, count); items.Do(r => { IdentificationItem item = request.AddNewItem(); item.ID = $"012-{_provider}-{r.Id}"; item.Name = r.Name; item.Surname = r.Surname; item.MiddleName = r.MiddleName; item.Birthday = r.Birthday.ToString("dd.MM.yyyy"); item.Gender = r.Gender; item.PlaceOfBirth.Country = r.Сountry; item.PlaceOfBirth.Region = r.Region; item.PlaceOfBirth.Area = r.Area; item.PlaceOfBirth.City = r.City; }); RaiseInformation($"Создан файл [{request.SaveTo(path)}]"); part++; } while (part < parts); RaiseComplete("Конвертация выполнена успешно"); }
public void SendRequest(Bitmap image) { IFormatter formatter = new BinaryFormatter(); IdentificationRequest request = new IdentificationRequest() { Image = image, Timestamp = DateTime.Now }; formatter.Serialize(stream, request); stream.Flush(); IdentificationResponse response = (IdentificationResponse)formatter.Deserialize(stream); Console.WriteLine(response.Object); }
public async Task <IHttpActionResult> Post([FromBody] IdentificationRequest request) { try { await _storage.LogAsunc("Identification request received", Level.Info); if (request.Image == null) { return(BadRequest()); } var faceClient = new FaceServiceClient(OxfordFaceKey); List <Face> faces; // Détection des visages sur la photo using (var stream = new MemoryStream(request.Image)) faces = (await faceClient.DetectAsync(stream)).ToList(); await _storage.LogAsunc($"{faces.Count} faces detected", Level.Info); if (faces.Count == 0) { return(NotFound()); } var result = (await faceClient.IdentifyAsync(XebienzaPersonGroup, faces.Select(o => o.FaceId).ToArray())).ToList(); await _storage.LogAsunc($"{result.Count} persons identified", Level.Info); if (result.Count == 0 || !result.Any(o => o.Candidates.Any())) { return(NotFound()); } // Une fois les personnes identifiées, on ne garde que la mieux reconnue var moreConfidentPerson = result.SelectMany(p => p.Candidates) .OrderByDescending(o => o.Confidence).First(); // On récupère les informations de la personne avec ses préférences await _storage.LogAsunc($"{moreConfidentPerson.PersonId} is the more confident person", Level.Info); var user = await _storage.GetUserFromTableStorageAsync(moreConfidentPerson.PersonId); await _storage.LogAsunc($"Result: {user.Name}", Level.Info); // On enrichit la base de connaissance d'Oxford await _storage.LogAsunc("Training requested for identified persons", Level.Info); foreach (var person in result.Where(o => o.Candidates.Any())) { await _storage.AddPersonFaceAsync(person.Candidates .OrderByDescending(o => o.Confidence).First().PersonId, request.Image); } return(Ok(user.Name)); } catch (Exception e) { await _storage.LogAsunc("Error while processing message", Level.Error, e); throw; } }