Best Karate code snippet using com.intuit.karate.robot.Tesseract.Word
Source:Tesseract.java
...45 private static final Logger logger = LoggerFactory.getLogger(Tesseract.class);46 private final TessBaseAPI tess;47 private final Supplier<IntPointer> INT = () -> new IntPointer(new int[1]);48 private String allText;49 private List<Word> words;50 public String getAllText() {51 return allText;52 }53 public List<Word> getWords() {54 return words;55 }56 public static class Word {57 final String text;58 final int x;59 final int y;60 final int width;61 final int height;62 final float confidence;63 final Word prev;64 Word next;65 public Word(Word prev, String text, int x, int y, int width, int height, float confidence) {66 if (prev != null) {67 prev.next = this;68 }69 this.prev = prev;70 this.text = text;71 this.x = x;72 this.y = y;73 this.width = width;74 this.height = height;75 this.confidence = confidence;76 }77 @Override78 public String toString() {79 return text + " " + x + ":" + y + "(" + width + ":" + height + ") " + confidence;80 }81 }82 public Tesseract(File dataDir, String language) {83 tess = new TessBaseAPI();84 String dataPath = dataDir.getPath();85 if (tess.Init(dataPath, language) != 0) {86 throw new RuntimeException("tesseract init failed: " + dataDir.getAbsolutePath() + ", " + language);87 }88 }89 public static final Tesseract init(RobotBase robot, String lang, Region region, boolean negative) {90 File file = new File(robot.tessData);91 Tesseract tess = new Tesseract(file, lang);92 tess.process(region, negative);93 return tess;94 }95 public static Element find(RobotBase robot, String lang, Region sr, String text, boolean negative) {96 Tesseract tess = init(robot, lang, sr, negative);97 List<int[]> list = tess.find(false, text);98 if (list.isEmpty()) {99 return null;100 }101 int[] b = list.get(0);102 Region region = new Region(robot, sr.x + b[0], sr.y + b[1], b[2], b[3]);103 return new ImageElement(region);104 }105 public static List<Element> findAll(RobotBase robot, String lang, Region sr, String text, boolean negative) {106 Tesseract tess = init(robot, lang, sr, negative);107 List<int[]> list = tess.find(true, text);108 List<Element> found = new ArrayList(list.size());109 for (int[] b : list) {110 Region region = new Region(robot, sr.x + b[0], sr.y + b[1], b[2], b[3]);111 found.add(new ImageElement(region));112 }113 return found;114 }115 public void process(Region region, boolean negative) {116 BufferedImage bi = region.captureGreyScale();117 if (region.robot.highlight) {118 region.highlight(region.robot.highlightDuration);119 }120 Mat mat = OpenCvUtils.toMat(bi);121 process(mat, negative);122 }123 public void highlightWords(RobotBase robot, Region parent, int millis) {124 List<Element> elements = new ArrayList();125 for (Tesseract.Word word : words) {126 Region region = new Region(robot, parent.x + word.x, parent.y + word.y, word.width, word.height);127 Element e = new ImageElement(region, word.text);128 elements.add(e);129 }130 RobotUtils.highlightAll(parent, elements, millis, true);131 }132 public void process(Mat mat, boolean negative) {133 if (negative) {134 mat = OpenCvUtils.negative(mat);135 }136 int srcWidth = mat.size().width();137 int srcHeight = mat.size().height();138 int channels = mat.channels();139 int bytesPerLine = srcWidth * channels * (int) mat.elemSize1();140 tess.SetImage(mat.data().asBuffer(), srcWidth, srcHeight, channels, bytesPerLine);141 //======================================================================142 BytePointer textPtr = tess.GetUTF8Text();143 allText = textPtr.getString();144 textPtr.deallocate();145 //======================================================================146 ResultIterator ri = tess.GetIterator();147 int level = tesseract.RIL_WORD;148 words = new ArrayList();149 Word prev = null;150 do {151 float confidence = ri.Confidence(level);152 if (confidence < 50) {153 continue;154 }155 textPtr = ri.GetUTF8Text(level);156 String text = textPtr.getString().trim();157 textPtr.deallocate();158 IntPointer x1 = INT.get();159 IntPointer y1 = INT.get();160 IntPointer x2 = INT.get();161 IntPointer y2 = INT.get();162 boolean found = ri.BoundingBox(level, x1, y1, x2, y2);163 int x = x1.get();164 int y = y1.get();165 int width = x2.get() - x;166 int height = y2.get() - y;167 if (!found) {168 logger.warn("no such rectangle: {}:{}:{}:{}", x, y, width, height);169 continue;170 }171 Word word = new Word(prev, text, x, y, width, height, confidence);172 words.add(word);173 prev = word;174 } while (ri.Next(level));175 }176 public List<int[]> find(boolean findAll, String text) {177 StringTokenizer st = new StringTokenizer(text);178 String[] args = new String[st.countTokens()];179 for (int i = 0; st.hasMoreTokens(); i++) {180 args[i] = st.nextToken();181 }182 List<int[]> list = new ArrayList();183 for (Word w : words) {184 boolean found = false;185 int i = 0;186 Word current = w;187 Word prev = null;188 do {189 String s = args[i];190 found = s.contains(current.text);191 prev = current;192 current = current.next;193 } while (found && ++i < args.length && current != null);194 if (found && i == args.length) {195 Word first = w;196 Word last = prev;197 int x = first.x;198 int y = first.y;199 int width = last.x + last.width - first.x;200 int height = Math.max(first.height, last.height);201 int[] bounds = new int[]{x, y, width, height};202 if (!findAll) {203 return Collections.singletonList(bounds);204 }205 list.add(bounds);206 }207 }208 return list;209 }210}...
Word
Using AI Code Generation
1* def image = read('classpath:sample.png')2* def text = robot.word(image)3* def image = read('classpath:sample.png')4* def text = robot.line(image)5* def image = read('classpath:sample.png')6* def text = robot.paragraph(image)7* def image = read('classpath:sample.png')8* def text = robot.blockText(image)9* def image = read('classpath:sample.png')10* def text = robot.hocr(image)11* def image = read('classpath:sample.png')12* def text = robot.pdf(image)13* def image = read('classpath:sample.pdf')14* def text = robot.pdfToImage(image)15* def image = read('classpath:sample.pdf')16* def text = robot.pdfToText(image)17* def image = read('classpath:sample.pdf')18* def text = robot.pdfToTiff(image)19* def image = read('classpath:sample.png')20* def text = robot.tiff(image)21* def image = read('
Word
Using AI Code Generation
1* def text = tesseract.text(image, lang, options)2* def text = tesseract.tesseractText(image, lang, options)3* def text = tesseract.tesseract4Text(image, lang, options)4* def text = tesseract.text(image, lang, options)5* def text = tesseract.tesseractText(image, lang, options)6* def text = tesseract.tesseract4Text(image, lang, options)7* def text = tesseract.text(image, lang, options)8* def text = tesseract.tesseractText(image, lang, options)9* def text = tesseract.tesseract4Text(image, lang, options)10* def text = tesseract.text(image, lang, options)11* def text = tesseract.tesseractText(image, lang, options)12* def text = tesseract.tesseract4Text(image, lang, options)13* def text = tesseract.text(image, lang, options)14* def text = tesseract.tesseractText(image, lang, options)15* def text = tesseract.tesseract4Text(image, lang, options)
Word
Using AI Code Generation
1def text = tesseract.imageToText('image.png')2def text = tesseract.imageToText('image.png')3def text = tesseract.imageToText('image.png')4def text = tesseract.imageToText('image.png')5def text = tesseract.imageToText('image.png')6def text = tesseract.imageToText('image.png')7def text = tesseract.imageToText('image.png')8def text = tesseract.imageToText('image.png')9def text = tesseract.imageToText('image.png')10def text = tesseract.imageToText('image.png')11def text = tesseract.imageToText('image.png')12def text = tesseract.imageToText('image.png')
Word
Using AI Code Generation
1def tesseract = new com.intuit.karate.robot.Tesseract()2def text = tesseract.readTextFromImage('file:src/test/resources/ocr.png', 'eng')3def tesseract = new com.intuit.karate.robot.Tesseract()4def text = tesseract.Tesseract('file:src/test/resources/ocr.png', 'eng')5def tesseract = new com.intuit.karate.robot.Tesseract()6def text = tesseract.Tesseract('file:src/test/resources/ocr.png', 'eng', 'eng')7def tesseract = new com.intuit.karate.robot.Tesseract()8def text = tesseract.Tesseract('file:src/test/resources/ocr.png', 'eng', 'eng', 'eng')9def tesseract = new com.intuit.karate.robot.Tesseract()10def text = tesseract.Tesseract('file:src/test/resources/ocr.png', 'eng', 'eng', 'eng', 'eng')11def tesseract = new com.intuit.karate.robot.Tesseract()12def text = tesseract.Tesseract('file:src/test/resources/ocr.png', 'eng', 'eng', 'eng', 'eng', 'eng')
Word
Using AI Code Generation
1* def image = read('classpath:sample.png')2* def text = image.word(0,0,100,100)3* def image = read('classpath:sample.png')4* def text = image.words(0,0,100,100)5* def image = read('classpath:sample.png')6* def text = image.text(0,0,100,100)7* def image = read('classpath:sample.png')8* def text = image.text(0,0,100,100)9* def image = read('classpath:sample.png')10* def text = image.text(0,0,100,100)11* def image = read('classpath:sample.png')12* def text = image.text(0,0,100,100)13* def image = read('classpath:sample.png')14* def text = image.text(0,0,100,100)15* def image = read('classpath:sample.png')16* def text = image.text(0,0,100,100)
Word
Using AI Code Generation
1def t = new com.intuit.karate.robot.Tesseract()2def text = t.Word('image.png')3def t = new com.intuit.karate.robot.Tesseract()4def text = t.Word('image.png')5def t = new com.intuit.karate.robot.Tesseract()6def text = t.Word('image.png')7def t = new com.intuit.karate.robot.Tesseract()8def text = t.Word('image.png')9def t = new com.intuit.karate.robot.Tesseract()
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!