public async Task AddFaceAsync_WithCorrectInput_ReturnsOk()
        {
            var mockLogger            = Mock.Of <ILogger <MaintenanceController> >();
            var mockFaceClientService = new Mock <IFaceClientService>();

            mockFaceClientService
            .Setup(service => service.AddFaceAsync(It.IsNotNull <string>(), It.IsNotNull <string>()))
            .Returns(() => Task.FromResult(new Cloud.Models.Face {
                FaceId = $"{Guid.NewGuid()}"
            }));

            var dto = new FaceDto
            {
                FaceUrl = $"http://www.google.com"
            };

            var controller   = new MaintenanceController(mockFaceClientService.Object, mockLogger);
            var actionResult = await controller.AddFaceAsync($"{Guid.NewGuid()}", dto);

            Assert.NotNull(actionResult);

            var okObjectResult = Assert.IsAssignableFrom <OkObjectResult>(actionResult);
            var face           = Assert.IsAssignableFrom <Cloud.Models.Face>(okObjectResult.Value);

            face.Should().NotBeNull();
            face.FaceId.Should().NotBeNullOrWhiteSpace();
        }
Beispiel #2
0
        public async Task PutFace(FaceDto faceDto)
        {
            var face = await _context.Faces.FindAsync(faceDto.Id);

            face.PersonId = faceDto.PersonId;
            _context.Entry(face).State = EntityState.Modified;
            await _context.SaveChangesAsync();
        }
Beispiel #3
0
        public async Task <IHttpActionResult> UpdateDashboard(FaceDto faceDto)
        {
            var hubConnection = new HubConnection("https://face-dashboard-api.azurewebsites.net/signalr");
            var proxy         = hubConnection.CreateHubProxy("dashboard");

            await hubConnection.Start();

            await proxy.Invoke("UpdateDashboard", hubConnection.ConnectionId, faceDto);

            return(Ok());
        }
        public async Task UpdateFace(FaceDto face)
        {
            _getFaceRequest    = PersonGroupPersonFaceRequestGenerator.Get(PersonGroupId !, PersonId !, face.PersistedFaceId);
            _updateFaceRequest = PersonGroupPersonFaceRequestGenerator.Update(PersonGroupId !, PersonId !, face.PersistedFaceId, face.UserData);
            _deleteFaceRequest = PersonGroupPersonFaceRequestGenerator.Delete(PersonGroupId !, PersonId !, face.PersistedFaceId);
            UpdateRequestList();

            _ = await MakeRequest <string>(_updateFaceRequest).ConfigureAwait(false);

            // Refresh list.
            await GetPerson();
        }
        public async Task AddFace(FaceDto face, string imageUrl, string detectionModel)
        {
            _addFaceRequest = PersonGroupPersonFaceRequestGenerator.Add(
                PersonGroupId !,
                PersonId !,
                imageUrl,
                detectionModel,
                face.UserData);

            UpdateRequestList();

            _ = await MakeRequest <string>(_addFaceRequest).ConfigureAwait(false);

            // Refresh list.
            await GetPerson();
        }
        public async Task AddFaceAsync_WithFaceClientServiceFault_ThrowsException()
        {
            var mockLogger            = Mock.Of <ILogger <MaintenanceController> >();
            var mockFaceClientService = new Mock <IFaceClientService>();

            mockFaceClientService
            .Setup(service => service.AddFaceAsync(It.IsNotNull <string>(), It.IsNotNull <string>()))
            .Throws <NotImplementedException>();

            var dto = new FaceDto
            {
                FaceUrl = $"http://www.google.com"
            };

            var controller = new MaintenanceController(mockFaceClientService.Object, mockLogger);
            await Assert.ThrowsAsync <NotImplementedException>(async() =>
            {
                await controller.AddFaceAsync($"{Guid.NewGuid()}", dto);
            });
        }
Beispiel #7
0
        public async Task <IActionResult> PutFace(long personId, long faceId, FaceDto face)
        {
            if (faceId != face.Id || !_faceService.FaceExists(personId, faceId))
            {
                return(BadRequest());
            }

            try
            {
                await _faceService.PutFace(face);
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_faceService.FaceExists(personId, faceId))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }
            return(NoContent());
        }
        public async Task <IActionResult> AddFaceAsync(string personId, [FromBody] FaceDto dto)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(personId))
                {
                    return(BadRequest());
                }

                if (null == dto)
                {
                    return(BadRequest(dto));
                }

                var face = await _faceClientService.AddFaceAsync(personId, dto.FaceUrl);

                return(Ok(face));
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Failed to add face to cognitive servcices.");
                throw;
            }
        }
Beispiel #9
0
 public void UpdateDashboard(string name, FaceDto face)
 {
     Clients.All.updateDashboard(name, face);
 }
Beispiel #10
0
        public ContentResult Save(FaceDto face)
        {
            if (face == null || (face.Id == 0 && !(face.TagId > 0 || !string.IsNullOrWhiteSpace(face.Name))))
            {
                return(AjaxFailedResult("Must enter name for face."));
            }
            if (face.Width < 10)
            {
                Logger.Trace($"Invalid width for face.  x: {face.X}, y: {face.Y}, w: {face.Width}, h: {face.Height}");
                return(AjaxFailedResult("Width must be greater than 10."));
            }
            if (face.Height < 10)
            {
                Logger.Trace($"Invalid height for face.  x: {face.X}, y: {face.Y}, w: {face.Width}, h: {face.Height}");
                return(AjaxFailedResult("Height must be greater than 10."));
            }
            var thumbnail = Context.Thumbnails.FirstOrDefault(i => i.Id == face.ImageId);

            if (thumbnail == null)
            {
                return(AjaxFailedResult("Image not found."));
            }

            var  dbFace = Context.Faces.Include(f => f.Tag).FirstOrDefault(p => p.Id == face.Id);
            var  orig   = dbFace.ToFace();
            bool moved;

            if (dbFace == null)
            {
                dbFace = new Face
                {
                    IsValid         = true,
                    NeedsValidation = false,
                    Haar            = Constants.Manual,
                    Thumbnail       = thumbnail,
                    ThumbnailId     = face.ImageId
                };
                moved = true;
            }
            else
            {
                moved = !(
                    dbFace.RectX == face.X &&
                    dbFace.RectY == face.Y &&
                    dbFace.RectHeight == face.Height &&
                    dbFace.RectWidth == face.Width
                    );

                if (!(face.TagId > 0 || !string.IsNullOrWhiteSpace(face.Name)) && dbFace.TagId.HasValue)
                {
                    face.TagId = dbFace.TagId.Value;
                }
            }

            if (moved)
            {
                dbFace.RectX      = face.X;
                dbFace.RectY      = face.Y;
                dbFace.RectHeight = face.Height;
                dbFace.RectWidth  = face.Width;

                Logger.Trace($"Face Moved.  x: {face.X}, y: {face.Y}, w: {face.Width}, h: {face.Height}, iW: {dbFace.ToFace().ImageWidth}, iH: {dbFace.ToFace().ImageHeight}");

                using (var img = Image.Load(Helpers.GetFullPath(thumbnail)))
                {
                    Helpers.ExifRotate(img);
                    var clone = img.Clone(context => context
                                          .Crop(new Rectangle(dbFace.RectX, dbFace.RectY, dbFace.RectWidth, dbFace.RectHeight)));
                    dbFace.Bytes = Helpers.ImageToByteArray(clone);
                }
            }

            if (!string.IsNullOrWhiteSpace(face.Name) && face.TagId <= 0)
            {
                //check for existing tag
                var tag = Context.Tags.FirstOrDefault(t => t.Name == face.Name && t.TagTypeId == Constants.Tags.PersonTagTypeId);
                if (tag != null)
                {
                    face.TagId = tag.Id;
                    face.Name  = null;
                    Logger.Trace($"Tag already exists for name {tag.Name}.  Updating reference");
                }
            }
            if (dbFace.Id == 0 || (dbFace.TagId.HasValue && dbFace.TagId != face.TagId))
            {
                //tag doesn't exist currently or it's a different tag than before.
                if (dbFace.TagId.HasValue)
                {
                    RemoveTag(dbFace.TagId.Value, face.ImageId);
                }

                dbFace.Tag   = AddTag(face.Name, face.TagId, face.ImageId);
                dbFace.TagId = dbFace.Tag.Id;
            }

            dbFace.IsValid         = true;
            dbFace.NeedsValidation = false;
            var message = $"Created Face {face.Name} for {thumbnail.FileName}";

            if (dbFace.Id == 0)
            {
                Context.Faces.Add(dbFace);
                orig         = dbFace.ToFace();
                orig.IsValid = false;
            }
            else
            {
                message = $"Updated Face {face.Name} for {thumbnail.FileName}";
                Context.Entry(dbFace).State = EntityState.Modified;
            }
            Context.SaveChanges();

            return(AjaxResult(dbFace.ToFace(), message, orig, Url.Action("Save")));
        }
Beispiel #11
0
        public ContentResult Tag(FaceDto face)
        {
            if (face == null || !(face.TagId > 0 || !string.IsNullOrWhiteSpace(face.Name)))
            {
                return(AjaxFailedResult("Must enter name for face."));
            }

            var dbFace = Context.Faces
                         .Include(f => f.Tag)
                         .Include(f => f.Thumbnail)
                         .FirstOrDefault(p => p.Id == face.Id);

            if (dbFace == null)
            {
                return(AjaxFailedResult("Face not found."));
            }

            if (!string.IsNullOrWhiteSpace(face.Name) && face.TagId <= 0)
            {
                //check for existing tag
                var tag = Context.Tags.FirstOrDefault(t => t.Name == face.Name && t.TagTypeId == Constants.Tags.PersonTagTypeId);
                if (tag != null)
                {
                    face.TagId = tag.Id;
                    face.Name  = tag.Name;
                    Logger.Trace($"Tag already exists for name {tag.Name}.  Updating reference");
                }
            }
            else if (face.TagId > 0)
            {
                var tag = Context.Tags.FirstOrDefault(t => t.Id == face.TagId && t.TagTypeId == Constants.Tags.PersonTagTypeId);
                if (tag != null)
                {
                    face.Name = tag.Name;
                }
            }

            var thumbnail = dbFace.Thumbnail;
            var message   = $"Updated name for face {face.Name} for {thumbnail.FileName}";

            if (dbFace.TagId != face.TagId || face.TagId == 0)
            {
                //tag doesn't exist currently or it's a different tag than before.
                if (dbFace.TagId.HasValue)
                {
                    RemoveTag(dbFace.TagId.Value, face.ImageId);
                }

                dbFace.Tag   = AddTag(face.Name, face.TagId, face.ImageId);
                dbFace.TagId = dbFace.Tag.Id;

                dbFace.IsValid              = true;
                dbFace.NeedsValidation      = false;
                Context.Entry(dbFace).State = EntityState.Modified;
                Context.SaveChanges();
            }
            else if (!dbFace.IsValid || dbFace.NeedsValidation)
            {
                dbFace.IsValid              = true;
                dbFace.NeedsValidation      = false;
                Context.Entry(dbFace).State = EntityState.Modified;
                Context.SaveChanges();
            }

            return(AjaxResult(dbFace.ToFace(), message));
        }