# Optical Character Recognition(OCR)-Tesseract

[OFFICIAL DOCUMENTATION](https://tesseract-ocr.github.io/tessdoc/)

| PLATFORM | HOW TO                                                                                  |
| -------- | --------------------------------------------------------------------------------------- |
| WINDOWS  | [download binaries for windows](https://tesseract-ocr.github.io/tessdoc/Downloads.html) |
| LINUX    | Tesseract is included in most Linux distributions. ("sudo apt install tesseract-ocr")   |

{% hint style="success" %}
FILE -> PROJECT INTERPRETER -> install pytesseract package
{% endhint %}

{% hint style="success" %}
import pytesseract
{% endhint %}

{% hint style="danger" %}
While you're working with pytesseract, your src image should be in RBG format not BGR.
{% endhint %}

## EACH CHARACTER

![Drawing Rectangle and Recognizing each Character](/files/-MAQieA41xqQw4-TtEv2)

```
import cv2
import pytesseract


img = cv2.imread("1.png")
imgRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
print(pytesseract.image_to_string(imgRGB))
cv2.imshow("Output", imgRGB)


cv2.waitKey(0) 
```

## EACH WORD

![](/files/-MAQii4wwL0WSjFcdkHR)

```
import cv2
import pytesseract

img = cv2.imread("1.png")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
hImg, wImg, _ = img.shape
listOfPoints = pytesseract.image_to_boxes(img)

for b in listOfPoints.splitlines():
    b = b.split(' ')
    print(b)
    x,y,w,h = int(b[1]), int(b[2]), int(b[3]), int(b[4])
    cv2.rectangle(img, (x, hImg-y), (w, hImg-h), (0,0,255), 3)
    cv2.putText(img, b[0], (x, hImg-y+25), cv2.FONT_HERSHEY_COMPLEX, 1, (50,50,255), 2)

cv2.imshow("Output", img)
cv2.waitKey(0)
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://capellac.gitbook.io/opencv/week1/opencv-basics/optical-character-recognition-ocr-tesseract.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
