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); } }
static void Main(string[] args) { IImageServices imageServices = new ImageServices(); IVisionApiClient visionClient = new VisionApiClient(imageServices); IFaceApiClient faceClient = new FaceApiClient(imageServices); Console.WriteLine("Analyze an image:"); Console.Write("Enter the path to the image you wish to analyze: "); string imageFilePath = Console.ReadLine(); if (File.Exists(imageFilePath)) { Console.WriteLine("\nWait a moment for the results to appear.\n"); //visionClient.MakeAnalysisRequest(imageFilePath).Wait(); string response = faceClient.MakeDetectRequest(imageFilePath).GetAwaiter().GetResult(); Console.WriteLine($"\nResponse:\n\n{response}\n"); } else { Console.WriteLine("\nInvalid file path"); } Console.WriteLine("\nPress Enter to exit..."); Console.ReadLine(); }
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); } }
} // TODO refactor this be be a service impl. public PhotoProcessorService(FaceApiClient faceApiClient, RemoveBgClient removeBgClient) { _faceApiClient = faceApiClient; _backgroundRemovalApiClient = removeBgClient; }
public FaceApiController(IStorageClient storageClient, FaceApiClient faceApi, IMDbRepository repository) { this.storageClient = storageClient; this.faceApi = faceApi; this.repository = repository; }