Пример #1
0
        protected override async Task OnMessageActivityAsync(ITurnContext <IMessageActivity> turnContext, CancellationToken cancellationToken)
        {
            foreach (var attachment in turnContext.Activity.Attachments)
            {
                if (attachment.ContentType != "image/jpeg" && attachment.ContentType != "image/png")
                {
                    continue;
                }

                await turnContext.SendActivityAsync("Thank you! Please standby, this only takes a moment.", cancellationToken : cancellationToken);

                var sourcePhotoBytes = await _httpClient.GetByteArrayAsync(attachment.ContentUrl);

                var sourcePhoto = new ReadOnlyMemory <byte>(sourcePhotoBytes);

                var processedFaces = await _photoProcessorService.ProcessPhoto(sourcePhoto);

                foreach (var processedFace in processedFaces)
                {
                    var url = await _azureBlobStorageService.SaveProcessedFaceAsync(processedFace, cancellationToken);

                    await turnContext.SendActivityAsync(MessageFactory.Attachment(new Attachment("image/png", url)), cancellationToken);
                }
            }
        }
Пример #2
0
        static async Task Main(string[] args)
        {
            var configuration = new ConfigurationBuilder()
                                .AddJsonFile("appsettings.json")
                                .AddUserSecrets("8A5141D1-33BC-4B1E-8051-F57FB87FC5C5")
                                .Build();

            var settings = new ProfileImageServiceSettings();

            configuration.Bind(settings);

            var removeBgClient        = new RemoveBgClient(new HttpClient(), settings);
            var faceApiClient         = new FaceApiClient(new HttpClient(), settings);
            var photoProcessorService = new PhotoProcessorService(faceApiClient, removeBgClient)
            {
                Validate = face =>
                {
                    Console.WriteLine($"Validating face '{face.FaceId}'...");
                    return(true);
                }
            };

            var sourcePhoto    = new ReadOnlyMemory <byte>(File.ReadAllBytes("assets/adult-1868750_1280.jpg"));
            var processedFaces = await photoProcessorService.ProcessPhoto(sourcePhoto);

            foreach (var processedFace in processedFaces)
            {
                await using var rawFile = File.Create("output/raw.png");
                await rawFile.WriteAsync(processedFace.TransparentPhoto);

                await using var profileImageFile = File.Create("output/profileImage.png");
                await profileImageFile.WriteAsync(processedFace.ProfileImage);
            }
        }
        public async Task PhotoProcessorService_Should_ProcessPhoto()
        {
            // Arrange
            var(removeBgHttpClient, _) = CreateMockHttpClient(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StreamContent(new MemoryStream(File.ReadAllBytes("assets/removebg-response.png"))),
            });

            var(faceApiHttpClient, _) = CreateMockHttpClient(new HttpResponseMessage
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(File.ReadAllText("assets/face-api-response.json"), Encoding.UTF8, "application/json"),
            });

            var removeBgClient        = new RemoveBgClient(removeBgHttpClient, _testFixture.Settings);
            var faceApiClient         = new FaceApiClient(faceApiHttpClient, _testFixture.Settings);
            var photoProcessorService = new PhotoProcessorService(faceApiClient, removeBgClient);

            var sourcePhoto = new ReadOnlyMemory <byte>(File.ReadAllBytes("assets/adult-1868750_1280.jpg"));

            // Act
            var processedFaces = await photoProcessorService.ProcessPhoto(sourcePhoto);

            // Assert
            foreach (var processedFace in processedFaces)
            {
                //using var rawFile = File.Create("test-output/actual-profile-image.png");
                //await processedFace.PhotoOfFaceWithoutBackgroundStream.CopyToAsync(rawFile);

                await using var debugImageFile = File.Create("test-output/debug-image.png");
                await debugImageFile.WriteAsync(processedFace.DebugImage);

                await using var croppedPhotoFile = File.Create("test-output/actual-cropped-photo.png");
                await croppedPhotoFile.WriteAsync(processedFace.CroppedPhoto);

                await using var profileImageFile = File.Create("test-output/actual-profile-image.png");
                await profileImageFile.WriteAsync(processedFace.ProfileImage);

                await using var rawProfileImageFile = File.Create("test-output/actual-raw-profile-image.png");
                await rawProfileImageFile.WriteAsync(processedFace.TransparentPhoto);

                var expected = new Memory <byte>(File.ReadAllBytes("assets/expected-profile-image.png"));

                processedFace.TransparentPhoto.Length.Should().BeGreaterThan(10000);
                processedFace.ProfileImage.Length.Should().BeGreaterThan(10000);

                // TODO do some tolerant image comparison
                //processedFace.ProfileImage.Should().Be(expected);
            }
        }