Skip to content

Commit 2ae29e1

Browse files
committed
first commit
0 parents  commit 2ae29e1

29 files changed

Lines changed: 1409 additions & 0 deletions

File tree

Conceptual_Guide/Part_1-model_deployment/README.md

Lines changed: 293 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import numpy as np
2+
import tritonclient.http as httpclient
3+
from tritonclient.utils import triton_to_np_dtype
4+
5+
import math
6+
import cv2
7+
8+
def fourPointsTransform(frame, vertices):
9+
vertices = np.asarray(vertices)
10+
outputSize = (100, 32)
11+
targetVertices = np.array([
12+
[0, outputSize[1] - 1],
13+
[0, 0],
14+
[outputSize[0] - 1, 0],
15+
[outputSize[0] - 1, outputSize[1] - 1]], dtype="float32")
16+
17+
rotationMatrix = cv2.getPerspectiveTransform(vertices, targetVertices)
18+
result = cv2.warpPerspective(frame, rotationMatrix, outputSize)
19+
return result
20+
21+
def decodeBoundingBoxes(scores, geometry, scoreThresh):
22+
detections = []
23+
confidences = []
24+
25+
############ CHECK DIMENSIONS AND SHAPES OF geometry AND scores ############
26+
assert len(scores.shape) == 4, "Incorrect dimensions of scores"
27+
assert len(geometry.shape) == 4, "Incorrect dimensions of geometry"
28+
assert scores.shape[0] == 1, "Invalid dimensions of scores"
29+
assert geometry.shape[0] == 1, "Invalid dimensions of geometry"
30+
assert scores.shape[1] == 1, "Invalid dimensions of scores"
31+
assert geometry.shape[1] == 5, "Invalid dimensions of geometry"
32+
assert scores.shape[2] == geometry.shape[2], "Invalid dimensions of scores and geometry"
33+
assert scores.shape[3] == geometry.shape[3], "Invalid dimensions of scores and geometry"
34+
height = scores.shape[2]
35+
width = scores.shape[3]
36+
for y in range(0, height):
37+
# Extract data from scores
38+
scoresData = scores[0][0][y]
39+
x0_data = geometry[0][0][y]
40+
x1_data = geometry[0][1][y]
41+
x2_data = geometry[0][2][y]
42+
x3_data = geometry[0][3][y]
43+
anglesData = geometry[0][4][y]
44+
for x in range(0, width):
45+
score = scoresData[x]
46+
# If score is lower than threshold score, move to next x
47+
if (score < scoreThresh):
48+
continue
49+
# Calculate offset
50+
offsetX = x * 4.0
51+
offsetY = y * 4.0
52+
angle = anglesData[x]
53+
54+
# Calculate cos and sin of angle
55+
cosA = math.cos(angle)
56+
sinA = math.sin(angle)
57+
h = x0_data[x] + x2_data[x]
58+
w = x1_data[x] + x3_data[x]
59+
60+
# Calculate offset
61+
offset = ([offsetX + cosA * x1_data[x] + sinA * x2_data[x], offsetY - sinA * x1_data[x] + cosA * x2_data[x]])
62+
63+
# Find points for rectangle
64+
p1 = (-sinA * h + offset[0], -cosA * h + offset[1])
65+
p3 = (-cosA * w + offset[0], sinA * w + offset[1])
66+
center = (0.5 * (p1[0] + p3[0]), 0.5 * (p1[1] + p3[1]))
67+
detections.append((center, (w, h), -1 * angle * 180.0 / math.pi))
68+
confidences.append(float(score))
69+
70+
# Return detections and confidences
71+
return [detections, confidences]
72+
73+
# read image
74+
img = cv2.imread("./img1.jpg")
75+
height_ = img.shape[0]
76+
width_ = img.shape[1]
77+
confThreshold = 0.5
78+
nmsThreshold = 0.4
79+
inpWidth = 640
80+
inpHeight = 480
81+
82+
rW = width_ / float(inpWidth)
83+
rH = height_ / float(inpHeight)
84+
85+
# pre-process image
86+
blob = cv2.dnn.blobFromImage(img, 1.0, (inpWidth, inpHeight), (123.68, 116.78, 103.94), True, False)
87+
blob = np.transpose(blob, (0, 2,3,1))
88+
89+
# Setting up client
90+
client = httpclient.InferenceServerClient(url="localhost:8000")
91+
92+
input_image = httpclient.InferInput("input_images:0", blob.shape, datatype="FP32")
93+
input_image.set_data_from_numpy(blob, binary_data=True)
94+
95+
scores = httpclient.InferRequestedOutput("feature_fusion/Conv_7/Sigmoid:0", binary_data=True)
96+
geometry = httpclient.InferRequestedOutput("feature_fusion/concat_3:0", binary_data=True)
97+
98+
# Querying the server
99+
query = client.infer(model_name="text_detection", inputs=[input_image], outputs=[scores, geometry])
100+
101+
scores_ = np.transpose(query.as_numpy('feature_fusion/Conv_7/Sigmoid:0'), (0,3,1,2))
102+
geometry_ = np.transpose(query.as_numpy('feature_fusion/concat_3:0'), (0,3,1,2))
103+
print(scores_.shape)
104+
105+
#print(scores_.shape)
106+
[boxes, confidences] = decodeBoundingBoxes(scores_, geometry_, confThreshold)
107+
indices = cv2.dnn.NMSBoxesRotated(boxes, confidences, confThreshold, nmsThreshold)
108+
ctr = 0
109+
for i in indices:
110+
vertices = cv2.boxPoints(boxes[i])
111+
for j in range(4):
112+
vertices[j][0] *= rW
113+
vertices[j][1] *= rH
114+
115+
cropped = fourPointsTransform(img, vertices)
116+
cv2.imwrite(str(ctr)+".jpg",cropped)
117+
ctr+=1
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import numpy as np
2+
import tritonclient.http as httpclient
3+
from tritonclient.utils import triton_to_np_dtype
4+
5+
import math
6+
import cv2
7+
8+
9+
def decodeText(scores):
10+
text = ""
11+
alphabet = "0123456789abcdefghijklmnopqrstuvwxyz"
12+
for i in range(scores.shape[0]):
13+
c = np.argmax(scores[i][0])
14+
if c != 0:
15+
text += alphabet[c - 1]
16+
else:
17+
text += '-'
18+
# adjacent same letters as well as background text must be removed to get the final output
19+
char_list = []
20+
for i in range(len(text)):
21+
if text[i] != '-' and (not (i > 0 and text[i] == text[i - 1])):
22+
char_list.append(text[i])
23+
return ''.join(char_list)
24+
25+
# read image
26+
img = cv2.imread("./0.jpg")
27+
28+
# pre-process image
29+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
30+
blob = cv2.dnn.blobFromImage(gray, size=(100,32), mean=127.5, scalefactor=1 / 127.5)
31+
print(blob.shape)
32+
33+
#blob = np.transpose(blob, (0, 2,3,1))
34+
35+
# Setting up client
36+
client = httpclient.InferenceServerClient(url="localhost:8000")
37+
38+
input_image = httpclient.InferInput("input.1", blob.shape, datatype="FP32")
39+
input_image.set_data_from_numpy(blob, binary_data=True)
40+
41+
text_matrix = httpclient.InferRequestedOutput("308", binary_data=True)
42+
43+
# Querying the server
44+
query = client.infer(model_name="text_recognition", inputs=[input_image], outputs=[text_matrix])
45+
text = decodeText(np.transpose(query.as_numpy('308'), (1,0,2)))
904 KB
Loading
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: "text_detection"
2+
backend: "onnxruntime"
3+
max_batch_size : 0
4+
input [
5+
{
6+
name: "input_images:0"
7+
data_type: TYPE_FP32
8+
dims: [ -1, -1, -1, 3 ]
9+
}
10+
]
11+
output [
12+
{
13+
name: "feature_fusion/Conv_7/Sigmoid:0"
14+
data_type: TYPE_FP32
15+
dims: [ -1, -1, -1, 1 ]
16+
}
17+
]
18+
output [
19+
{
20+
name: "feature_fusion/concat_3:0"
21+
data_type: TYPE_FP32
22+
dims: [ -1, -1, -1, 5 ]
23+
}
24+
]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: "text_recognition"
2+
backend: "onnxruntime"
3+
max_batch_size : 0
4+
input [
5+
{
6+
name: "input.1"
7+
data_type: TYPE_FP32
8+
dims: [ 1, 1, 32, 100 ]
9+
}
10+
]
11+
output [
12+
{
13+
name: "308"
14+
data_type: TYPE_FP32
15+
dims: [ 1, 26, 37 ]
16+
}
17+
]

0 commit comments

Comments
 (0)