Download TableFormer/src/example.py from cimo001/docling: direct link, hf CLI and curl.
- Browser
- Download file 4.99 kB
-
https://huggingface.co/cimo001/docling/resolve/main/TableFormer/src/example.py
- Command line
-
hf download hf://cimo001/docling/TableFormer/src/example.py
-
curl -L -o example.py https://huggingface.co/cimo001/docling/resolve/main/TableFormer/src/example.py
4.99 kB
| 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) | |