Skip to content

Commit 4f94ee0

Browse files
committed
added ocr tutorial
1 parent 64d0b31 commit 4f94ee0

File tree

7 files changed

+149
-0
lines changed

7 files changed

+149
-0
lines changed
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [How to Recognize Optical Characters in Images in Python](https://www.thepythoncode.com/article/optical-character-recognition-pytesseract-python)
2+
To run this:
3+
- `pip3 install -r requirements.txt`
4+
- If you want to recognize optical characters of the image `test.png`:
5+
```
6+
python extracting_text.py test.png
7+
```
8+
**Output:**
9+
```
10+
This is a lot of 12 point text to test the
11+
ocr code and see if it works on all types
12+
of file format.
13+
14+
The quick brown dog jumped over the
15+
lazy fox. The quick brown dog jumped
16+
over the lazy fox. The quick brown dog
17+
jumped over the lazy fox. The quick
18+
brown dog jumped over the lazy fox.
19+
```
20+
- for drawing boxes of the image `test.png` for the word `"dog"`:
21+
```
22+
python draw-boxes.py test.png dog
23+
```
24+
**Output:**
25+
26+
<img src="detected-words-ocr.png" align="center">
27+
- For live camera, consider using `live_recognizer.py` script.
28+
Loading
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import pytesseract
2+
import cv2
3+
import sys
4+
import matplotlib.pyplot as plt
5+
from PIL import Image
6+
7+
# read the image using OpenCV
8+
image = cv2.imread(sys.argv[1])
9+
10+
# make a copy of this image to draw in
11+
image_copy = image.copy()
12+
13+
# the target word to search for
14+
target_word = sys.argv[2]
15+
16+
# get all data from the image
17+
data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
18+
19+
# get all occurences of the that word
20+
word_occurences = [ i for i, word in enumerate(data["text"]) if word.lower() == target_word ]
21+
22+
for occ in word_occurences:
23+
# extract the width, height, top and left position for that detected word
24+
w = data["width"][occ]
25+
h = data["height"][occ]
26+
l = data["left"][occ]
27+
t = data["top"][occ]
28+
# define all the surrounding box points
29+
p1 = (l, t)
30+
p2 = (l + w, t)
31+
p3 = (l + w, t + h)
32+
p4 = (l, t + h)
33+
# draw the 4 lines (rectangular)
34+
image_copy = cv2.line(image_copy, p1, p2, color=(255, 0, 0), thickness=2)
35+
image_copy = cv2.line(image_copy, p2, p3, color=(255, 0, 0), thickness=2)
36+
image_copy = cv2.line(image_copy, p3, p4, color=(255, 0, 0), thickness=2)
37+
image_copy = cv2.line(image_copy, p4, p1, color=(255, 0, 0), thickness=2)
38+
39+
plt.imsave("all_dog_words.png", image_copy)
40+
plt.imshow(image_copy)
41+
plt.show()
42+
43+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import pytesseract
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
import sys
5+
from PIL import Image
6+
7+
# read the image using OpenCV
8+
# from the command line first argument
9+
image = cv2.imread(sys.argv[1])
10+
# or you can use Pillow
11+
# image = Image.open(sys.argv[1])
12+
13+
# get the string
14+
string = pytesseract.image_to_string(image)
15+
# print it
16+
print(string)
17+
18+
# get all data
19+
# data = pytesseract.image_to_data(image)
20+
21+
# print(data)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import pytesseract
2+
import cv2
3+
import matplotlib.pyplot as plt
4+
from PIL import Image
5+
6+
# the target word to search for
7+
target_word = "your"
8+
9+
cap = cv2.VideoCapture(0)
10+
11+
while True:
12+
# read the image from the cam
13+
_, image = cap.read()
14+
15+
# make a copy of this image to draw in
16+
image_copy = image.copy()
17+
18+
# get all data from the image
19+
data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
20+
21+
# print the data
22+
print(data["text"])
23+
24+
# get all occurences of the that word
25+
word_occurences = [ i for i, word in enumerate(data["text"]) if word.lower() == target_word ]
26+
27+
for occ in word_occurences:
28+
# extract the width, height, top and left position for that detected word
29+
w = data["width"][occ]
30+
h = data["height"][occ]
31+
l = data["left"][occ]
32+
t = data["top"][occ]
33+
# define all the surrounding box points
34+
p1 = (l, t)
35+
p2 = (l + w, t)
36+
p3 = (l + w, t + h)
37+
p4 = (l, t + h)
38+
# draw the 4 lines (rectangular)
39+
image_copy = cv2.line(image_copy, p1, p2, color=(255, 0, 0), thickness=2)
40+
image_copy = cv2.line(image_copy, p2, p3, color=(255, 0, 0), thickness=2)
41+
image_copy = cv2.line(image_copy, p3, p4, color=(255, 0, 0), thickness=2)
42+
image_copy = cv2.line(image_copy, p4, p1, color=(255, 0, 0), thickness=2)
43+
44+
if cv2.waitKey(1) == ord("q"):
45+
break
46+
47+
cv2.imshow("image_copy", image_copy)
48+
49+
cap.release()
50+
cv2.destroyAllWindows()
51+
52+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pytesseract
2+
numpy
3+
matplotlib
4+
opencv-python
5+
pillow
11.2 KB
Loading

0 commit comments

Comments
 (0)