-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathdraw-boxes.py
43 lines (34 loc) · 1.26 KB
/
draw-boxes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import pytesseract
import cv2
import sys
import matplotlib.pyplot as plt
from PIL import Image
# read the image using OpenCV
image = cv2.imread(sys.argv[1])
# make a copy of this image to draw in
image_copy = image.copy()
# the target word to search for
target_word = sys.argv[2]
# get all data from the image
data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
# get all occurences of the that word
word_occurences = [ i for i, word in enumerate(data["text"]) if word.lower() == target_word ]
for occ in word_occurences:
# extract the width, height, top and left position for that detected word
w = data["width"][occ]
h = data["height"][occ]
l = data["left"][occ]
t = data["top"][occ]
# define all the surrounding box points
p1 = (l, t)
p2 = (l + w, t)
p3 = (l + w, t + h)
p4 = (l, t + h)
# draw the 4 lines (rectangular)
image_copy = cv2.line(image_copy, p1, p2, color=(255, 0, 0), thickness=2)
image_copy = cv2.line(image_copy, p2, p3, color=(255, 0, 0), thickness=2)
image_copy = cv2.line(image_copy, p3, p4, color=(255, 0, 0), thickness=2)
image_copy = cv2.line(image_copy, p4, p1, color=(255, 0, 0), thickness=2)
plt.imsave("all_dog_words.png", image_copy)
plt.imshow(image_copy)
plt.show()