How to use GoogleVisionOcrService class of com.galenframework.ocr package

Best Galen code snippet using com.galenframework.ocr.GoogleVisionOcrService

Source:GoogleVisionOcrService.java Github

copy

Full Screen

...46 * Cache the model, so if the same image is used, will send a single OCR request to the service.47 * @author guy arieli, Ivan Shubin48 *49 */50public class GoogleVisionOcrService implements OcrService {51 private final static String BASE_URL = "https://vision.googleapis.com/v1/images:annotate?key=";52 final static HttpClient httpClient = HttpClients.createDefault();53 final static ObjectMapper objectMapper = new ObjectMapper();54 @Override55 public OcrResult findOcrText(BufferedImage image, Rect rect) throws ValidationErrorException {56 if (rect.getRight() > image.getWidth() && rect.getBottom() > rect.getHeight()) {57 throw new ValidationErrorException("Could not extract element image. Looks like it is located outside of screenshot area");58 }59 try {60 BufferedImage croppedImage = image.getSubimage(rect.getLeft(), rect.getTop(), rect.getWidth(), rect.getHeight());61 GoogleModel model = getGoogleModel(croppedImage);62 if (model.responses != null && !model.responses.isEmpty()) {63 String resultedText = model.responses.get(0).fullTextAnnotation.text;64 if (resultedText == null) {...

Full Screen

Full Screen

Source:ValidationFactory.java Github

copy

Full Screen

...15******************************************************************************/16package com.galenframework.validation;17import java.util.HashMap;18import java.util.Map;19import com.galenframework.ocr.GoogleVisionOcrService;20import com.galenframework.specs.*;21import com.galenframework.validation.specs.*;22public class ValidationFactory {23 24 @SuppressWarnings("rawtypes")25 private Map<Class<? extends Spec>, SpecValidation> validations = new HashMap<>();26 27 private static ValidationFactory _instance = null;28 29 private ValidationFactory() {30 initValidations();31 }32 33 public synchronized static ValidationFactory get() {34 if (_instance == null) {35 _instance = new ValidationFactory();36 }37 return _instance;38 }39 40 private void initValidations() {41 validations.put(SpecContains.class, new SpecValidationContains());42 validations.put(SpecAbsent.class, new SpecValidationAbsent());43 validations.put(SpecVisible.class, new SpecValidationVisible());44 validations.put(SpecInside.class, new SpecValidationInside());45 validations.put(SpecNear.class, new SpecValidationNear());46 validations.put(SpecWidth.class, new SpecValidationWidth());47 validations.put(SpecHeight.class, new SpecValidationHeight());48 validations.put(SpecHorizontally.class, new SpecValidationHorizontally());49 validations.put(SpecVertically.class, new SpecValidationVertically());50 validations.put(SpecText.class, new SpecValidationText());51 validations.put(SpecAbove.class, new SpecValidationDirectionPosition(SpecValidationDirectionPosition.Direction.ABOVE));52 validations.put(SpecBelow.class, new SpecValidationDirectionPosition(SpecValidationDirectionPosition.Direction.BELOW));53 validations.put(SpecLeftOf.class, new SpecValidationDirectionPosition(SpecValidationDirectionPosition.Direction.LEFT_OF));54 validations.put(SpecRightOf.class, new SpecValidationDirectionPosition(SpecValidationDirectionPosition.Direction.RIGHT_OF));55 validations.put(SpecCentered.class, new SpecValidationCentered());56 validations.put(SpecOn.class, new SpecValidationOn());57 validations.put(SpecComponent.class, new SpecValidationComponent());58 validations.put(SpecColorScheme.class, new SpecValidationColorScheme());59 validations.put(SpecImage.class, new SpecValidationImage());60 validations.put(SpecCss.class, new SpecValidationCss());61 validations.put(SpecCount.class, new SpecValidationCount());62 validations.put(SpecOcr.class, new SpecValidationOcr(new GoogleVisionOcrService()));63 }64 @SuppressWarnings({ "unchecked", "rawtypes" })65 public static SpecValidation<? extends Spec> getValidation(Spec spec, PageValidation pageValidation) {66 SpecValidation specValidation = ValidationFactory.get().validations.get(spec.getClass());67 if (specValidation == null) {68 throw new RuntimeException("There is no known validation for spec " + spec.getClass());69 }70 else {71 return specValidation;72 }73 }74}...

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.GoogleVisionOcrService;2import org.openqa.selenium.*;3import org.openqa.selenium.chrome.ChromeDriver;4import org.openqa.selenium.chrome.ChromeOptions;5import org.openqa.selenium.support.ui.ExpectedConditions;6import org.openqa.selenium.support.ui.WebDriverWait;7import java.io.File;8import java.io.IOException;9import java.io.PrintWriter;10import java.util.concurrent.TimeUnit;11public class 1 {12 public static void main(String[] args) throws IOException {13 System.setProperty("webdriver.chrome.driver", "C:\\Users\\Downloads\\chromedriver.exe");14 ChromeOptions options = new ChromeOptions();15 options.addArguments("start-maximized");16 WebDriver driver = new ChromeDriver(options);17 driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);18 WebDriverWait wait = new WebDriverWait(driver, 10);19 wait.until(ExpectedConditions.presenceOfElementLocated(By.name("q")));20 driver.findElement(By.name("q")).sendKeys("galenframework");21 wait.until(ExpectedConditions.presenceOfElementLocated(By.name("btnK")));22 driver.findElement(By.name("btnK")).click();23 wait.until(ExpectedConditions.presenceOfElementLocated(By.id("resultStats")));24 File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);25 String text = new GoogleVisionOcrService().getText(scrFile);26 PrintWriter writer = new PrintWriter("C:\\Users\\Downloads\\text.txt", "UTF-8");27 writer.println(text);28 writer.close();29 driver.quit();30 }31}

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1package com.galenframework.ocr;2import java.io.IOException;3import com.google.api.gax.core.FixedCredentialsProvider;4import com.google.auth.oauth2.GoogleCredentials;5import com.google.auth.oauth2.ServiceAccountCredentials;6import com.google.cloud.vision.v1.AnnotateImageRequest;7import com.google.cloud.vision.v1.AnnotateImageResponse;8import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;9import com.google.cloud.vision.v1.Feature;10import com.google.cloud.vision.v1.Feature.Type;11import com.google.cloud.vision.v1.Image;12import com.google.cloud.vision.v1.ImageAnnotatorClient;13import com.google.cloud.vision.v1.ImageAnnotatorSettings;14import com.google.cloud.vision.v1.ImageSource;15import com.google.protobuf.ByteString;16public class GoogleVisionOcrService {17 private ImageAnnotatorClient visionClient;18 public GoogleVisionOcrService() throws IOException {19 GoogleCredentials credentials = ServiceAccountCredentials.getApplicationDefault();20 ImageAnnotatorSettings visionSettings = ImageAnnotatorSettings.newBuilder()21 .setCredentialsProvider(FixedCredentialsProvider.create(credentials))22 .build();23 visionClient = ImageAnnotatorClient.create(visionSettings);24 }25 public String getTextFromImage(String url) throws IOException {26 ImageSource source = ImageSource.newBuilder().setImageUri(url).build();27 Image image = Image.newBuilder().setSource(source).build();28 Feature feature = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();29 AnnotateImageRequest request = AnnotateImageRequest.newBuilder()30 .addFeatures(feature)31 .setImage(image)32 .build();33 BatchAnnotateImagesResponse response = visionClient.batchAnnotateImages(Collections.singletonList(request));34 AnnotateImageResponse imageResponse = response.getResponsesList().get(0);35 return imageResponse.getFullTextAnnotation().getText();36 }37}38package com.galenframework.ocr;39import java.io.IOException;40public class GoogleVisionOcrServiceTest {41 public static void main(String[] args) throws IOException {42 GoogleVisionOcrService service = new GoogleVisionOcrService();43 }44}45package com.galenframework.ocr;46import java.io.IOException;47public class GoogleVisionOcrServiceTest {

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1package com.galenframework.ocr;2import java.io.IOException;3import java.util.List;4import com.google.cloud.vision.v1.AnnotateImageResponse;5import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;6import com.google.cloud.vision.v1.Feature;7import com.google.cloud.vision.v1.Image;8import com.google.cloud.vision.v1.ImageAnnotatorClient;9import com.google.cloud.vision.v1.ImageSource;10import com.google.cloud.vision.v1.Page;11import com.google.cloud.vision.v1.Paragraph;12import com.google.cloud.vision.v1.TextAnnotation;13import com.google.cloud.vision.v1.Word;14import com.google.protobuf.ByteString;15public class GoogleVisionOcrService implements OcrService {16private static final String GOOGLE_VISION_API_KEY = "AIzaSyDdR1nWjQkQv5h5i5j0R0Z5Q2Y5Yq3X9x4";17public String getTextFromImage(String imageUrl) throws IOException {18ImageSource imageSource = ImageSource.newBuilder().setImageUri(imageUrl).build();19Image image = Image.newBuilder().setSource(imageSource).build();20Feature feature = Feature.newBuilder().setType(Feature.Type.DOCUMENT_TEXT_DETECTION).build();21BatchAnnotateImagesResponse response = ImageAnnotatorClient.create()22.batchAnnotateImages(List.of(image), List.of(feature));23return getTextFromResponse(response);24}25private String getTextFromResponse(BatchAnnotateImagesResponse response) {26AnnotateImageResponse annotateImageResponse = response.getResponsesList().get(0);27TextAnnotation textAnnotation = annotateImageResponse.getFullTextAnnotation();28return textAnnotation.getText();29}30}31package com.galenframework.ocr;32import java.io.IOException;33import org.testng.annotations.Test;34import com.galenframework.reports.GalenTestInfo;35import com.galenframework.reports.model.LayoutReport;36import com.galenframework.reports.model.LayoutReportBuilder;37import com.galenframework.reports.model.LayoutSection;38import com

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.GoogleVisionOcrService;2import com.galenframework.ocr.OcrService;3import com.galenframework.ocr.OcrText;4import com.galenframework.ocr.OcrWord;5import com.galenframework.reports.GalenTestInfo;6import com.galenframework.reports.HtmlReportBuilder;7import com.galenframework.reports.TestReport;8import com.galenframework.reports.model.LayoutReport;9import com.galenframework.reports.model.LayoutReportError;10import com.galenframework.reports.model.LayoutReportStatus;11import com.galenframework.reports.model.LayoutReportValidationError;12import com.galenframework.reports.model.LayoutReportValidationObject;13import com.galenframework.reports.model.LayoutReportValidationResult;14import com.galenframework.reports.model.LayoutReportValidationStatus;15import com.galenframework.reports.model.LayoutReportValidationWarning;16import com.galenframework.reports.model.LayoutReportValidationWarningObject;17import com.galenframework.reports.model.LayoutReportValidationWarningResult;18import com.galenframework.reports.model.LayoutReportValidationWarningStatus;19import com.galenframework.reports.model.LayoutReportWarning;20import com.galenframework.reports.model.LayoutReportWarningObject;21import com.galenframework.reports.model.LayoutReportWarningResult;22import com.galenframework.reports.model.LayoutReportWarningStatus;23import com.galenframework.specs.Spec;24import com.galenframework.specs.SpecExtractText;25import com.galenframework.specs.SpecText;26import com.galenframework.specs.page.Locator;27import com.galenframework.specs.page.PageSection;28import com.galenframework.specs.page.PageSectionFilter;29import com.galenframework.specs.page.PageSectionFilterType;30import com.galenframework.specs.page.PageSectionRange;31import com.galenframework.specs.page.PageSectionType;32import com.galenframework.specs.page.PageSpec;33import java.awt.image.BufferedImage;34import java.io.File;35import java.io.IOException;36import java.util.ArrayList;37import java.util.Arrays;38import java.util.List;39import javax.imageio.ImageIO;40import org.apache.commons.io.FileUtils;41import org.testng.annotations.Test;42public class GalenTest {43 public void galenTest() throws IOException, InterruptedException {44 PageSpec pageSpec = new PageSpec();45 PageSection pageSection = new PageSection();

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.google.GoogleVisionOcrService;2import com.galenframework.ocr.ocrservice.OcrService;3import com.galenframework.ocr.ocrservice.OcrServiceException;4import com.galenframework.ocr.ocrservice.OcrServiceFactory;5import com.galenframework.ocr.ocrservice.OcrServiceType;6import com.galenframework.ocr.ocrservice.OcrText;7import com.galenframework.ocr.ocrservice.OcrTextBlock;8import com.galenframework.ocr.ocrservice.OcrTextLine;9import com.galenframework.ocr.ocrservice.OcrTextWord;10import org.apache.commons.io.FileUtils;11import org.openqa.selenium.OutputType;12import org.openqa.selenium.TakesScreenshot;13import org.openqa.selenium.WebDriver;14import org.openqa.selenium.WebElement;15import java.awt.image.BufferedImage;16import java.io.File;17import java.io.IOException;18import java.util.List;19import javax.imageio.ImageIO;20public class GoogleVisionOcrServiceExample {21 public static void main(String[] args) throws IOException, OcrServiceException {22 WebDriver driver = null;23 WebElement element = null;24 File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);25 BufferedImage fullImg = ImageIO.read(screenshot);26 BufferedImage elementScreenshot = fullImg.getSubimage(element.getLocation().getX(), element.getLocation().getY(), element.getSize().getWidth(), element.getSize().getHeight());27 ImageIO.write(elementScreenshot, "png", screenshot);28 File croppedScreenshot = new File("C:\\Users\\user\\Desktop\\croppedScreenshot.png");29 FileUtils.copyFile(screenshot, croppedScreenshot);30 OcrService ocrService = OcrServiceFactory.getOcrService(OcrServiceType.GOOGLE_VISION);31 OcrText ocrText = ocrService.getText(croppedScreenshot);32 List<OcrTextBlock> blocks = ocrText.getBlocks();33 System.out.println("Blocks: " + blocks);34 List<OcrTextLine> lines = ocrText.getLines();35 System.out.println("Lines: " + lines);

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1package com.galenframework.ocr;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import org.apache.commons.io.FileUtils;7import org.openqa.selenium.WebDriver;8import org.openqa.selenium.chrome.ChromeDriver;9import org.openqa.selenium.chrome.ChromeOptions;10import org.openqa.selenium.remote.RemoteWebDriver;11import com.google.cloud.vision.v1.AnnotateImageRequest;12import com.google.cloud.vision.v1.AnnotateImageResponse;13import com.google.cloud.vision.v1.BatchAnnotateImagesResponse;14import com.google.cloud.vision.v1.EntityAnnotation;15import com.google.cloud.vision.v1.Feature;16import com.google.cloud.vision.v1.Feature.Type;17import com.google.cloud.vision.v1.Image;18import com.google.cloud.vision.v1.ImageAnnotatorClient;19import com.google.protobuf.ByteString;20public class GoogleVisionOcrService {21 public static void main(String[] args) throws IOException {22 String path = "C:\\Users\\mohit\\Desktop\\Maven\\Maven\\Screenshot\\";23 String fileName = "test.png";24 GoogleVisionOcrService ocr = new GoogleVisionOcrService();25 ocr.ocrUsingGoogleVisionAPI(path, fileName);26 }27 public String ocrUsingGoogleVisionAPI(String path, String fileName) throws IOException {28 String text = "";29 try (ImageAnnotatorClient vision = ImageAnnotatorClient.create()) {30 String filePath = path + fileName;31 byte[] data = FileUtils.readFileToByteArray(new File(filePath));32 ByteString imgBytes = ByteString.copyFrom(data);33 Image img = Image.newBuilder().setContent(imgBytes).build();34 Feature feat = Feature.newBuilder().setType(Type.TEXT_DETECTION).build();35 AnnotateImageRequest request = AnnotateImageRequest.newBuilder().addFeatures(feat).setImage(img).build();36 List<AnnotateImageRequest> requests = new ArrayList<>();37 requests.add(request);38 BatchAnnotateImagesResponse response = vision.batchAnnotateImages(requests);

Full Screen

Full Screen

GoogleVisionOcrService

Using AI Code Generation

copy

Full Screen

1import com.galenframework.ocr.GoogleVisionOcrService;2import com.galenframework.ocr.GoogleVisionOcrService.OcrResult;3import java.io.File;4import java.io.IOException;5public class 1 {6 public static void main(String[] args) throws IOException {7 GoogleVisionOcrService ocr = new GoogleVisionOcrService();8 File file = new File("image.png");9 OcrResult ocrResult = ocr.readText(file);10 String text = ocrResult.getText();11 System.out.println(text);12 }13}14import com.galenframework.ocr.GoogleVisionOcrService;15import com.galenframework.ocr.GoogleVisionOcrService.OcrResult;16import java.io.File;17import java.io.IOException;18public class 2 {19 public static void main(String[] args) throws IOException {20 GoogleVisionOcrService ocr = new GoogleVisionOcrService();21 File file = new File("image.png");22 OcrResult ocrResult = ocr.readText(file);23 String text = ocrResult.getText();24 System.out.println(text);25 }26}27import com.galenframework.ocr.GoogleVisionOcrService;28import com.galenframework.ocr.GoogleVisionOcrService.OcrResult;29import java.io.File;30import java.io.IOException;

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Galen automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Most used methods in GoogleVisionOcrService

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful