File size: 4,992 Bytes
3958e29 | 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 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | import sys
import cv2
import numpy
sys.dont_write_bytecode = True
# Source
from helper import onnxSessionBuild
pathModel = "./TableFormer/"
imageSizePage = 1024
imageSizeModel = 448
meanList = numpy.array([0.94247851, 0.94254675, 0.94292611], dtype=numpy.float32)
standardList = numpy.array([0.17910956, 0.17940403, 0.17931663], dtype=numpy.float32)
countStep = 1024
countLayer = 6
countBatch = 32
sizeHidden = 512
tagList = ["<pad>", "<unk>", "<start>", "<end>", "ecel", "fcel", "lcel", "ucel", "xcel", "nl", "ched", "rhed", "srow"]
tagSkipList = ["nl", "ucel", "xcel"]
onnxSessionEncoder = onnxSessionBuild(f"{pathModel}onnx/tableFormer_encoder.onnx")
onnxSessionDecoder = onnxSessionBuild(f"{pathModel}onnx/tableFormer_decoder.onnx")
onnxSessionBox = onnxSessionBuild(f"{pathModel}onnx/tableFormer_box.onnx")
def imageResize(imageRgb):
imageHeight, imageWidth = imageRgb.shape[0:2]
ratio = imageSizePage / float(imageHeight)
imageResized = cv2.resize(imageRgb, (int(imageWidth * ratio), imageSizePage), interpolation=cv2.INTER_AREA)
tensor = (imageResized.astype(numpy.float32) - 255.0 * meanList) / standardList
tensor = cv2.resize(tensor, (imageSizeModel, imageSizeModel), interpolation=cv2.INTER_LINEAR)
return numpy.expand_dims(tensor.transpose((2, 1, 0)) / 255.0, axis=0).astype(numpy.float32)
def tagDecode(memory):
indexList = [tagList.index("<start>")]
hiddenList = []
mergeObject = {}
buffer = numpy.zeros((countLayer, countStep, 1, sizeHidden), dtype=numpy.float32)
isSkip = True
isSpan = False
indexSpan = -1
while len(indexList) < countStep:
position = len(indexList) - 1
tensorFeedObject = {
"tag": numpy.array([indexList[position]], dtype=numpy.int64),
"position": numpy.array([position], dtype=numpy.int64),
"memory": memory,
"buffer": buffer
}
logit, hidden, state = onnxSessionDecoder.run(None, tensorFeedObject)
buffer[:, position] = state[:, 0]
tag = tagList[int(numpy.argmax(logit[0]))]
if tag == "xcel":
tag = "lcel"
if tagList[indexList[position]] == "ucel" and tag == "lcel":
tag = "fcel"
if tag == "<end>":
break
if isSkip == False and tag != "lcel":
if isSpan:
mergeObject[indexSpan] = len(hiddenList)
hiddenList.append(hidden[0])
if tag != "lcel":
isSpan = False
elif isSpan == False:
isSpan = True
indexSpan = len(hiddenList)
hiddenList.append(hidden[0])
isSkip = tag in tagSkipList
indexList.append(tagList.index(tag))
return {
"tagList": [tagList[index] for index in indexList[1:]],
"hiddenList": hiddenList,
"mergeObject": mergeObject
}
def boxDecode(box, hiddenList, mergeObject, imageWidth, imageHeight):
resultList = []
coordinateList = []
for a in range(0, len(hiddenList), countBatch):
hiddenBatchList = hiddenList[a:a + countBatch]
tensor = numpy.zeros((countBatch, sizeHidden), dtype=numpy.float32)
tensor[0:len(hiddenBatchList)] = numpy.stack(hiddenBatchList)
tensorOutputList = onnxSessionBox.run(None, {"box": box, "hidden": tensor})
coordinateList += list(tensorOutputList[1][0:len(hiddenBatchList)])
indexSkipList = []
for a in range(len(coordinateList)):
centerX, centerY, width, height = coordinateList[a]
x1 = centerX - width / 2
y1 = centerY - height / 2
x2 = centerX + width / 2
y2 = centerY + height / 2
if a in mergeObject:
centerX, centerY, width, height = coordinateList[mergeObject[a]]
indexSkipList.append(mergeObject[a])
y1 = min(y1, centerY - height / 2)
x2 = centerX + width / 2
y2 = centerY + height / 2
if a in mergeObject or a not in indexSkipList:
resultList.append([float(x1) * imageWidth, float(y1) * imageHeight, float(x2) * imageWidth, float(y2) * imageHeight])
return resultList
def inference(imageRgb):
imageHeight, imageWidth = imageRgb.shape[0:2]
tensor = imageResize(imageRgb)
memory, box = onnxSessionEncoder.run(None, {"image": tensor})
decodeObject = tagDecode(memory)
return {
"tagList": decodeObject["tagList"],
"coordinateList": boxDecode(box, decodeObject["hiddenList"], decodeObject["mergeObject"], imageWidth, imageHeight)
}
image = cv2.imread(sys.argv[1])
imageRgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
itemObject = inference(imageRgb)
print(" ".join(itemObject["tagList"]).replace("nl ", "nl\n"))
print("")
for a in range(len(itemObject["coordinateList"])):
coordinateList = []
for b in range(len(itemObject["coordinateList"][a])):
coordinateList.append(int(round(itemObject["coordinateList"][a][b])))
print(coordinateList)
|