public void GivenNoRawResult_WhenCallingCreateSuccesResult_ThenArgumentNullExceptionIsThrown() { //Arrange Action act = () => AzureOcrResult.CreateSuccesResult(_timespan, _imageContent.Object, null); //Act //Assert act.ShouldThrow <ArgumentNullException>() .WithMessage("Value cannot be null.\r\nParameter name: rawAzureOcrResult"); }
public void GivenNoException_WhenCallingCreateErrorResult_ThenArgumentNullExceptionIsThrown() { //Arrange Action act = () => AzureOcrResult.CreateErrorResult(_timespan, null); //Act //Assert act.ShouldThrow <ArgumentNullException>() .WithMessage("Value cannot be null.\r\nParameter name: error"); }
public void GivenSuccessAzureOcrResult_WhenCallingTextFound_ThenReturnValueShouldBeSameAsImageContentTextFound(bool textFound, bool expected) { //Arrange _imageContent.Setup(c => c.TextFound()).Returns(textFound); var ocrResult = AzureOcrResult.CreateSuccesResult(_timespan, _imageContent.Object, _rawResult); //Act var result = ocrResult.TextFound; //Assert result.Should().Be(expected); }
public void GivenRawResultAndContent_WhenCallingCreateSuccesResult_ThenPropertiesShouldBeSetAsExpected() { //Arrange //Act var result = AzureOcrResult.CreateSuccesResult(_timespan, _imageContent.Object, _rawResult); //Assert result.RawResult.Should().Be(_rawResult); result.Content.Should().Be(_imageContent.Object); result.ProcessTime.Should().Be(_timespan); result.HasError.Should().BeFalse(); result.Error.Should().BeNull(); result.TextFound.Should().BeFalse(); }
public void GivenException_WhenCallingCreateErrorResult_ThenPropertiesShouldBeSetAsExpected() { //Arrange var exception = new Mock <Exception>().Object; //Act var result = AzureOcrResult.CreateErrorResult(_timespan, exception); //Assert result.RawResult.Should().BeNull(); result.Content.Should().BeNull(); result.ProcessTime.Should().Be(_timespan); result.HasError.Should().BeTrue(); result.Error.Should().Be(exception); result.TextFound.Should().BeFalse(); }
private async Task <AzureOcrResult> DoOcr(Stream imageAsStream, FileFormatEnum fileFormatEnum, DateTime start) { try { var preprocessedResult = _ocrPreProcessing.AjustOrientationAndSize(imageAsStream, fileFormatEnum); using (var stream = preprocessedResult.ImageFileStream) { ValidateImageProportions(preprocessedResult.NewImageHeight, preprocessedResult.NewImageWidth); var rawAzureOcrResult = await _azureVisionApi.Execute(stream); var content = _azureOcrParser.Execute(rawAzureOcrResult, preprocessedResult.NewImageHeight, preprocessedResult.NewImageWidth); return(AzureOcrResult.CreateSuccesResult(DateTime.Now.Subtract(start), content, rawAzureOcrResult)); } } catch (Exception e) { return(AzureOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e)); } }
/// <summary> /// Async method to load the image, call Azure's vision API and format the result into an easy to use format. /// Prior to calling the API, this method will compress the image if to large for the API service, and will rotate it according to the EXIF orientation. /// </summary> /// <param name="filePath">File path</param> /// <param name="fileFormatEnum">The image format. Needed for compression.</param> /// <returns></returns> public async Task <AzureOcrResult> OcrImage(string filePath, FileFormatEnum fileFormatEnum) { var start = DateTime.Now; try { if (!File.Exists(filePath)) { throw new FileNotFoundException("", filePath); } using (var stream = File.OpenRead(filePath)) { return(await DoOcr(stream, fileFormatEnum, start)); } } catch (Exception e) { return(AzureOcrResult.CreateErrorResult(DateTime.Now.Subtract(start), e)); } }