-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathText_detection_using_East_and_Recognition_using_Tesseract.py
141 lines (101 loc) · 3.48 KB
/
Text_detection_using_East_and_Recognition_using_Tesseract.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
from imutils.object_detection import non_max_suppression
import numpy as np
import time
import cv2
import pytesseract
net = cv2.dnn.readNet("frozen_east_text_detection.pb")
def text_detector(image):
orig = image
(H, W) = image.shape[:2]
(newW, newH) = (320, 320)
rW = W / float(newW)
rH = H / float(newH)
image = cv2.resize(image, (newW, newH))
(H, W) = image.shape[:2]
layerNames = [
"feature_fusion/Conv_7/Sigmoid",
"feature_fusion/concat_3"]
blob = cv2.dnn.blobFromImage(image, 1.0, (W, H),
(123.68, 116.78, 103.94), swapRB=True, crop=False)
net.setInput(blob)
(scores, geometry) = net.forward(layerNames)
(numRows, numCols) = scores.shape[2:4]
rects = []
confidences = []
for y in range(0, numRows):
scoresData = scores[0, 0, y]
xData0 = geometry[0, 0, y]
xData1 = geometry[0, 1, y]
xData2 = geometry[0, 2, y]
xData3 = geometry[0, 3, y]
anglesData = geometry[0, 4, y]
# loop over the number of columns
for x in range(0, numCols):
# if our score does not have sufficient probability, ignore it
if scoresData[x] < 0.5:
continue
# compute the offset factor as our resulting feature maps will
# be 4x smaller than the input image
(offsetX, offsetY) = (x * 4.0, y * 4.0)
# extract the rotation angle for the prediction and then
# compute the sin and cosine
angle = anglesData[x]
cos = np.cos(angle)
sin = np.sin(angle)
# use the geometry volume to derive the width and height of
# the bounding box
h = xData0[x] + xData2[x]
w = xData1[x] + xData3[x]
# compute both the starting and ending (x, y)-coordinates for
# the text prediction bounding box
endX = int(offsetX + (cos * xData1[x]) + (sin * xData2[x]))
endY = int(offsetY - (sin * xData1[x]) + (cos * xData2[x]))
startX = int(endX - w)
startY = int(endY - h)
# add the bounding box coordinates and probability score to
# our respective lists
rects.append((startX, startY, endX, endY))
confidences.append(scoresData[x])
boxes = non_max_suppression(np.array(rects), probs=confidences)
for (startX, startY, endX, endY) in boxes:
startX = int(startX * rW)
startY = int(startY * rH)
endX = int(endX * rW)
endY = int(endY * rH)
boundary = 2
text = orig[startY-boundary:endY+boundary, startX - boundary:endX + boundary]
text = cv2.cvtColor(text.astype(np.uint8), cv2.COLOR_BGR2GRAY)
textRecongized = pytesseract.image_to_string(text)
cv2.rectangle(orig, (startX, startY), (endX, endY), (0, 255, 0), 3)
# orig = cv2.putText(orig, textRecongized, (endX,endY+5), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,255), 2, cv2.LINE_AA)
return orig
# # Image
# # for i in range(0,2):
# # for img in array:
# img = cv2.imread(r'img6.png')
# imageO = cv2.resize(img, (640,320), interpolation = cv2.INTER_AREA)
# orig = cv2.resize(img, (640,320), interpolation = cv2.INTER_AREA)
# textDetected = text_detector(imageO)
# cv2.imshow("Orig Image",orig)
# cv2.imshow("Text Detection", textDetected)
# # time.sleep(2)
# # k = cv2.waitKey(30)
# # if k == 27:
# # break
# # cv2.imshow("Result",img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
# Web cam
cap = cv2.VideoCapture(0)
cap.set(3,640)
cap.set(4,320)
while True:
sucess, img = cap.read()
imageO = cv2.resize(img, (640,320), interpolation = cv2.INTER_AREA)
orig = cv2.resize(img, (640,320), interpolation = cv2.INTER_AREA)
textDetected = text_detector(imageO)
cv2.imshow("Orig Image",orig)
cv2.imshow("Text Detection", textDetected)
# cv2.imshow("Video",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break