File size: 2,949 Bytes
6e2f66d | 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 | import sys
import cv2
import numpy
from tokenizers import Tokenizer
sys.dont_write_bytecode = True
# Source
from helper import onnxSessionBuild
pathModel = "./PP-FormulaNet_plus-L/"
imageSizeModel = 768
levelInk = 200
meanList = numpy.array([0.7931, 0.7931, 0.7931], dtype=numpy.float32)
standardList = numpy.array([0.1738, 0.1738, 0.1738], dtype=numpy.float32)
countLayer = 8
countStep = 1024
sizeHidden = 512
tokenizer = Tokenizer.from_file(f"{pathModel}onnx/tokenizer.json")
idEnd = tokenizer.token_to_id("</s>")
onnxSessionEncoder = onnxSessionBuild(f"{pathModel}onnx/pp-formulaNet_plus-l_encoder.onnx")
onnxSessionDecoder = onnxSessionBuild(f"{pathModel}onnx/pp-formulaNet_plus-l_decoder.onnx")
def imageResize(image):
imageGray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY).astype(numpy.float32)
low = float(imageGray.min())
high = float(imageGray.max())
if high > low:
imageInk = ((imageGray - low) / (high - low) * 255.0 < levelInk).astype(numpy.uint8)
inkX, inkY, inkWidth, inkHeight = cv2.boundingRect(cv2.findNonZero(imageInk))
image = image[inkY:inkY + inkHeight, inkX:inkX + inkWidth]
imageHeight, imageWidth = image.shape[0:2]
ratio = imageSizeModel / float(max(imageHeight, imageWidth))
widthResized = max(1, int(imageWidth * ratio))
heightResized = max(1, int(imageHeight * ratio))
padX = (imageSizeModel - widthResized) // 2
padY = (imageSizeModel - heightResized) // 2
imagePadded = numpy.zeros((imageSizeModel, imageSizeModel, 3), dtype=numpy.uint8)
imagePadded[padY:padY + heightResized, padX:padX + widthResized] = cv2.resize(cv2.cvtColor(image, cv2.COLOR_BGR2RGB), (widthResized, heightResized), interpolation=cv2.INTER_LINEAR)
return imagePadded
def tokenDecode(memory):
idList = [idEnd]
buffer = numpy.zeros((countLayer, countStep, sizeHidden), dtype=numpy.float32)
while len(idList) < countStep:
position = len(idList) - 1
tensorFeedObject = {
"token": numpy.array([idList[position]], dtype=numpy.int64),
"position": numpy.array(position, dtype=numpy.int64),
"memory": memory,
"buffer": buffer
}
logit, state = onnxSessionDecoder.run(None, tensorFeedObject)
buffer[:, position] = state
idList.append(int(numpy.argmax(logit[0])))
if idList[position + 1] == idEnd:
break
return idList
def inference(image):
imagePadded = imageResize(image)
tensor = imagePadded.astype(numpy.float32) / 255.0
tensor = (tensor - meanList) / standardList
tensor = numpy.expand_dims(tensor.transpose((2, 0, 1)), axis=0).astype(numpy.float32)
tensorOutputList = onnxSessionEncoder.run(None, {"image": tensor})
idList = tokenDecode(tensorOutputList[0])
return tokenizer.decode(idList, skip_special_tokens=True)
image = cv2.imread(sys.argv[1])
text = inference(image)
print(text)
|