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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy
|
| 4 |
+
|
| 5 |
+
sys.dont_write_bytecode = True
|
| 6 |
+
|
| 7 |
+
# Source
|
| 8 |
+
from helper import onnxSessionBuild
|
| 9 |
+
|
| 10 |
+
pathModel = "./TableFormer/"
|
| 11 |
+
|
| 12 |
+
imageSizePage = 1024
|
| 13 |
+
imageSizeModel = 448
|
| 14 |
+
|
| 15 |
+
meanList = numpy.array([0.94247851, 0.94254675, 0.94292611], dtype=numpy.float32)
|
| 16 |
+
standardList = numpy.array([0.17910956, 0.17940403, 0.17931663], dtype=numpy.float32)
|
| 17 |
+
|
| 18 |
+
countStep = 1024
|
| 19 |
+
countLayer = 6
|
| 20 |
+
countBatch = 32
|
| 21 |
+
sizeHidden = 512
|
| 22 |
+
|
| 23 |
+
tagList = ["<pad>", "<unk>", "<start>", "<end>", "ecel", "fcel", "lcel", "ucel", "xcel", "nl", "ched", "rhed", "srow"]
|
| 24 |
+
tagSkipList = ["nl", "ucel", "xcel"]
|
| 25 |
+
|
| 26 |
+
onnxSessionEncoder = onnxSessionBuild(f"{pathModel}onnx/tableFormer_encoder.onnx")
|
| 27 |
+
onnxSessionDecoder = onnxSessionBuild(f"{pathModel}onnx/tableFormer_decoder.onnx")
|
| 28 |
+
onnxSessionBox = onnxSessionBuild(f"{pathModel}onnx/tableFormer_box.onnx")
|
| 29 |
+
|
| 30 |
+
def imageResize(imageRgb):
|
| 31 |
+
imageHeight, imageWidth = imageRgb.shape[0:2]
|
| 32 |
+
|
| 33 |
+
ratio = imageSizePage / float(imageHeight)
|
| 34 |
+
|
| 35 |
+
imageResized = cv2.resize(imageRgb, (int(imageWidth * ratio), imageSizePage), interpolation=cv2.INTER_AREA)
|
| 36 |
+
|
| 37 |
+
tensor = (imageResized.astype(numpy.float32) - 255.0 * meanList) / standardList
|
| 38 |
+
tensor = cv2.resize(tensor, (imageSizeModel, imageSizeModel), interpolation=cv2.INTER_LINEAR)
|
| 39 |
+
|
| 40 |
+
return numpy.expand_dims(tensor.transpose((2, 1, 0)) / 255.0, axis=0).astype(numpy.float32)
|
| 41 |
+
|
| 42 |
+
def tagDecode(memory):
|
| 43 |
+
indexList = [tagList.index("<start>")]
|
| 44 |
+
hiddenList = []
|
| 45 |
+
mergeObject = {}
|
| 46 |
+
|
| 47 |
+
buffer = numpy.zeros((countLayer, countStep, 1, sizeHidden), dtype=numpy.float32)
|
| 48 |
+
|
| 49 |
+
isSkip = True
|
| 50 |
+
isSpan = False
|
| 51 |
+
|
| 52 |
+
indexSpan = -1
|
| 53 |
+
|
| 54 |
+
while len(indexList) < countStep:
|
| 55 |
+
position = len(indexList) - 1
|
| 56 |
+
|
| 57 |
+
tensorFeedObject = {
|
| 58 |
+
"tag": numpy.array([indexList[position]], dtype=numpy.int64),
|
| 59 |
+
"position": numpy.array([position], dtype=numpy.int64),
|
| 60 |
+
"memory": memory,
|
| 61 |
+
"buffer": buffer
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
logit, hidden, state = onnxSessionDecoder.run(None, tensorFeedObject)
|
| 65 |
+
|
| 66 |
+
buffer[:, position] = state[:, 0]
|
| 67 |
+
|
| 68 |
+
tag = tagList[int(numpy.argmax(logit[0]))]
|
| 69 |
+
|
| 70 |
+
if tag == "xcel":
|
| 71 |
+
tag = "lcel"
|
| 72 |
+
|
| 73 |
+
if tagList[indexList[position]] == "ucel" and tag == "lcel":
|
| 74 |
+
tag = "fcel"
|
| 75 |
+
|
| 76 |
+
if tag == "<end>":
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
if isSkip == False and tag != "lcel":
|
| 80 |
+
if isSpan:
|
| 81 |
+
mergeObject[indexSpan] = len(hiddenList)
|
| 82 |
+
|
| 83 |
+
hiddenList.append(hidden[0])
|
| 84 |
+
|
| 85 |
+
if tag != "lcel":
|
| 86 |
+
isSpan = False
|
| 87 |
+
elif isSpan == False:
|
| 88 |
+
isSpan = True
|
| 89 |
+
|
| 90 |
+
indexSpan = len(hiddenList)
|
| 91 |
+
|
| 92 |
+
hiddenList.append(hidden[0])
|
| 93 |
+
|
| 94 |
+
isSkip = tag in tagSkipList
|
| 95 |
+
|
| 96 |
+
indexList.append(tagList.index(tag))
|
| 97 |
+
|
| 98 |
+
return {
|
| 99 |
+
"tagList": [tagList[index] for index in indexList[1:]],
|
| 100 |
+
"hiddenList": hiddenList,
|
| 101 |
+
"mergeObject": mergeObject
|
| 102 |
+
}
|
| 103 |
+
|
| 104 |
+
def boxDecode(box, hiddenList, mergeObject, imageWidth, imageHeight):
|
| 105 |
+
resultList = []
|
| 106 |
+
|
| 107 |
+
coordinateList = []
|
| 108 |
+
|
| 109 |
+
for a in range(0, len(hiddenList), countBatch):
|
| 110 |
+
hiddenBatchList = hiddenList[a:a + countBatch]
|
| 111 |
+
|
| 112 |
+
tensor = numpy.zeros((countBatch, sizeHidden), dtype=numpy.float32)
|
| 113 |
+
tensor[0:len(hiddenBatchList)] = numpy.stack(hiddenBatchList)
|
| 114 |
+
|
| 115 |
+
tensorOutputList = onnxSessionBox.run(None, {"box": box, "hidden": tensor})
|
| 116 |
+
|
| 117 |
+
coordinateList += list(tensorOutputList[1][0:len(hiddenBatchList)])
|
| 118 |
+
|
| 119 |
+
indexSkipList = []
|
| 120 |
+
|
| 121 |
+
for a in range(len(coordinateList)):
|
| 122 |
+
centerX, centerY, width, height = coordinateList[a]
|
| 123 |
+
|
| 124 |
+
x1 = centerX - width / 2
|
| 125 |
+
y1 = centerY - height / 2
|
| 126 |
+
x2 = centerX + width / 2
|
| 127 |
+
y2 = centerY + height / 2
|
| 128 |
+
|
| 129 |
+
if a in mergeObject:
|
| 130 |
+
centerX, centerY, width, height = coordinateList[mergeObject[a]]
|
| 131 |
+
|
| 132 |
+
indexSkipList.append(mergeObject[a])
|
| 133 |
+
|
| 134 |
+
y1 = min(y1, centerY - height / 2)
|
| 135 |
+
x2 = centerX + width / 2
|
| 136 |
+
y2 = centerY + height / 2
|
| 137 |
+
|
| 138 |
+
if a in mergeObject or a not in indexSkipList:
|
| 139 |
+
resultList.append([float(x1) * imageWidth, float(y1) * imageHeight, float(x2) * imageWidth, float(y2) * imageHeight])
|
| 140 |
+
|
| 141 |
+
return resultList
|
| 142 |
+
|
| 143 |
+
def inference(imageRgb):
|
| 144 |
+
imageHeight, imageWidth = imageRgb.shape[0:2]
|
| 145 |
+
|
| 146 |
+
tensor = imageResize(imageRgb)
|
| 147 |
+
|
| 148 |
+
memory, box = onnxSessionEncoder.run(None, {"image": tensor})
|
| 149 |
+
|
| 150 |
+
decodeObject = tagDecode(memory)
|
| 151 |
+
|
| 152 |
+
return {
|
| 153 |
+
"tagList": decodeObject["tagList"],
|
| 154 |
+
"coordinateList": boxDecode(box, decodeObject["hiddenList"], decodeObject["mergeObject"], imageWidth, imageHeight)
|
| 155 |
+
}
|
| 156 |
+
|
| 157 |
+
image = cv2.imread(sys.argv[1])
|
| 158 |
+
imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 159 |
+
|
| 160 |
+
itemObject = inference(imageRgb)
|
| 161 |
+
|
| 162 |
+
print(" ".join(itemObject["tagList"]).replace("nl ", "nl\n"))
|
| 163 |
+
print("")
|
| 164 |
+
|
| 165 |
+
for a in range(len(itemObject["coordinateList"])):
|
| 166 |
+
coordinateList = []
|
| 167 |
+
|
| 168 |
+
for b in range(len(itemObject["coordinateList"][a])):
|
| 169 |
+
coordinateList.append(int(round(itemObject["coordinateList"][a][b])))
|
| 170 |
+
|
| 171 |
+
print(coordinateList)
|