# Image to text (Tess4J?)

**URL:** https://discourse.processing.org/t/image-to-text-tess4j/2957
**Category:** Libraries
**Created:** [August 25, 2018, 7:48pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957 "2018-08-25T19:48:57Z")
**Posts on this page:** 19
**Page:** 1

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 7:48pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/1 "2018-08-25T19:48:57Z")

</div>

Hey all,

So the past few hours i’ve been desperately trying to get some form of ‘image-to-text’ working in processing 3. I’ve browsed and read alot of things for java, and i think it should be possible to get something working. I just can’t seem to get it right though.

Background of the problem: I want to take screenshots frequently of a particular part of the screen which contains the Health Points of the boss in the game i play. I’ve got a lot of plans which i could do to make an interesting tool, but the big step “Screenshot/image -\> int HP variable” is where i need help with.

So i’ve tried to use the Tess4J library for this (see [http://tess4j.sourceforge.net/](http://tess4j.sourceforge.net/) ). I am not fully sure how to get things working though, and im down to this situation: [http://prntscr.com/kmua58](http://prntscr.com/kmua58) .

Main question here: how can i get Tess4J to work with processing? I’ve seem to get stuck alot. After hours of trying on my own im hoping someone here is able to help! Anyone able to get Tess4J image to text going in processing3?

Hope to get reactions -MTech

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 7:57pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/2 "2018-08-25T19:57:48Z")

</div>

What you’re looking for is called [optical character recognition](https://en.wikipedia.org/wiki/Optical_character_recognition), or OCR. Googling something like “java OCR library” will return a ton of results.

But to answer your question, the simplest way to use a Java library in Processing is to drag the `.jar` file onto the Processing editor. Then you need to add import statements to use the classes in the library `.jar` file.

Shameless self-promotion: here is a guide on using libraries in Processing:

> **[How to Use Processing Libraries](https://happycoding.io/tutorials/processing/libraries)**
>
> Learn how to use libraries to expand what Processing can do.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:07pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/3 "2018-08-25T20:07:58Z")

</div>

Thanks alot for replying, ye i found that out been reading articles like that. The thing is: in the Tess4J folder there is a ‘lib’ folder, which contains like 20 .jar files all with kinda vague names (atleast to me).

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:11pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/4 "2018-08-25T20:11:57Z")

</div>

The setup is going to be a little different for every library. You could try narrowing down which `.jar` file you actually need. Or you could just drag them all onto your editor.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:14pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/5 "2018-08-25T20:14:59Z")

</div>

The first .jar i even try comes up with an error, first asks me would you like to replace the current .jar? So i press yes…

 ![aExample](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/de9def3804ce8b2bc50f2321e014bcdb2df78d08.jpeg)

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:17pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/6 "2018-08-25T20:17:00Z")

</div>

Right, so it looks like you’ve already copied that `.jar` file.

Take a look at your sketch folder by going to `Sketch` -\> `Show Sketch Folder` to see what libraries files have already been copied.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:18pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/7 "2018-08-25T20:18:05Z")

</div>

You also might just want to use a different library.

There are a couple OpenCV libraries that work nicely with Processing. Those might be worth looking into. Check out the libraries page on the Processing website for more info.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:20pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/8 "2018-08-25T20:20:30Z")

</div>

So apperently when i drag all .jar files it creates a map ‘code’ in my sketchfolder… see image.

 ![codExample](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/0/063089679df91c5d79349dc3282b80b14c725cf4.jpeg)

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:22pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/9 "2018-08-25T20:22:15Z")

</div>

Well i’ve been looking around for ages now, for example [http://www.yunmai.com/en/home.html](http://www.yunmai.com/en/home.html) is one i just downloaded. I get a random .zip tho with no clear library folder. For some reason everything i try has some kind of complicated structure and i just can’t get things to work. Been looking youtube and what not aswell for concrete steps which i’d need to take for making it work, but can’t get there.

Do you know of a concrete library that you know is easy to get working

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:25pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/10 "2018-08-25T20:25:05Z")

</div>

I’ve also taken a look at this one: [https://github.com/bytedeco/javacpp-presets/tree/master/tesseract](https://github.com/bytedeco/javacpp-presets/tree/master/tesseract)

But again (i guess its me being a total noob) i just can’t seem to follow the steps. Creating a pom file? Never seen one before. Using maven 3? I just get confused quite fast.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:26pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/11 "2018-08-25T20:26:26Z")

</div>

> [@MTech](#):
>
> For some reason everything i try has some kind of complicated structure and i just can’t get things to work.

Unfortunately that’s the case for a lot of libraries. Many come in different structures and formats, and it’s a bit of guesswork to figure out exactly what you need.

I think you’re on the right track with what you already have to Tess4J. My guess is you’re just missing an import statement now.

> [@MTech](#):
>
> Do you know of a concrete library that you know is easy to get working

Yep like I mentioned, I’d try OpenCV. I don’t have a specific example, but it should at least be easier to import into Processing since it’s supported directly in the editor.

> [@MTech](#):
>
> Creating a pom file? Never seen one before. Using maven 3? I just get confused quite fast.

That’s talking about [Maven](https://maven.apache.org/), which is a way to handle libraries without manually coping over `.jar` files. You can’t really use Maven from the Processing editor though, you’d have to switch to a more advanced editor.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:29pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/12 "2018-08-25T20:29:52Z")

</div>

Yea i see what you are saying. I just hope things get easier as i get more experienced. I’ve taken a look at OpenCV ( [https://github.com/atduskgreg/opencv-processing](https://github.com/atduskgreg/opencv-processing) ) but all the examples showed some photos, not really text recognition. You’re saying it actually is able to recognize text from images?

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:32pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/13 "2018-08-25T20:32:16Z")

</div>

As for Tess4J: I’ve been trying the same stuff as in this example ( [http://tess4j.sourceforge.net/codesample.html](http://tess4j.sourceforge.net/codesample.html) ). import net.sourceforge.tess4j.\*, but this seems kinda odd since i now have a folder named ‘code’ in my sketchfolder after dragging the .jars in there. “The package net.sourceforge.tess4j does not exist. You might be missing a library”.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:36pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/14 "2018-08-25T20:36:38Z")

</div>

Like I mentioned I don’t have a specific OCR example for OpenCV in mind, but I know it handles a lot of similar problems. I’d recommend googling something like “OpenCV OCR” for a ton of results.

The `code` directory is expected. This is how Processing stores libraries used in a sketch. I’m not sure why the import statement isn’t working- maybe you’re missing a core library `.jar` file? In other words, it could be that the `.jar` files you’ve seen so far are the libraries used by Tess4J, and not the Tess4J library itself?

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:37pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/15 "2018-08-25T20:37:43Z")

</div>

Take a look in the `dist` folder inside the Tess4J directory. It contains the core library `.jar` file you need.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 8:39pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/16 "2018-08-25T20:39:38Z")

</div>

As you say this im just discovering the dist folder with a single tess.jar file myself lol. Program gets past this problem now, gonna check it out. Thanks alot! What your saying was probably right, i assumed the ‘lib’ folder was the library folder and i’d need those libraries.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 8:40pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/17 "2018-08-25T20:40:37Z")

</div>

> [@MTech](#):
>
> i assumed the ‘lib’ folder was the library folder and i’d need those libraries.

You probably do need those `.jar` files. You just also need the `tess.jar` file as well. Basically the `tess.jar` file contains code that requires the other `.jar` files.

---

<div class="post-metadata">

### Author: ![MTech](https://avatars.discourse-cdn.com/v4/letter/m/a88e57/32.png) [@MTech](https://discourse.processing.org/u/MTech)
#### Post date: [August 25, 2018, 9:03pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/18 "2018-08-25T21:03:01Z")

</div>

I see thanks. Well i guess one final question, the results seem quite bad. Written text on a white background gets recognized pretty good. But reading the numbers in the healthbar seems too hard for it? This is what he reads. If i set it to only recognize numbers it doesnt read anything at all.

To me as a human it seems the numbers are pretty easy to recognize from the background, is it normal a program like this finds it realy hard tho? Is that just a limit of computers? If so ill have to just give up on what i want i guess. ![aImgTester](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/5/5c3254bc0c1205b59bb7986722c718f9632affa1.jpeg)

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [August 25, 2018, 9:06pm UTC](https://discourse.processing.org/t/image-to-text-tess4j/2957/19 "2018-08-25T21:06:58Z")

</div>

One common approach is to do some pre-processing on the image first. You could try converting to black and white first, or removing the background, or increasing the contrast for example. These are just examples, but there are a ton of options you can do.

This is one reason I mentioned OpenCV: it comes with a bunch of features for cleaning up images before you actually process them.

But yes, in general OCR is not exactly a [trivial problem](https://en.wikipedia.org/wiki/CAPTCHA).
