-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTextdetector.py
40 lines (36 loc) · 1.41 KB
/
Textdetector.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
import cv2
import easyocr
import matplotlib.pyplot as plt
image_paths = ['i1.jpg' , 'i2.jpg']
image = []
# instancing text detector en for english detection
reader = easyocr.Reader(['en'],gpu = False)
for path in image_paths:
img = cv2.imread(path)
image.append(img)
# DISPLAYING THE IMAGE
i = 0
threshold = 0.25
for img in image:
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
text = reader.readtext(img)
for t in text:
bbox, text, score = t
text_position = (int(bbox[0][0]) + 20, int(bbox[0][1]) - 20) # Slightly offset from the top-left corner
if score > threshold:
cv2.putText(img, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2)
# Drawing bounding box around the text region
cv2.rectangle(img, (int(bbox[0][0]), int(bbox[0][1])), (int(bbox[2][0]), int(bbox[2][1])), (0, 255, 0), 2)
# Display the image
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
else:
text = " not so accurate"
# Put the text on the image
cv2.putText(img, text, text_position, cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 0, 255), 2)
# Display the image
plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.axis('off')
plt.show()
# i+=1