|
| 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 |
0 commit comments