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