返回模型
说明文档
发票分类器
这是一个收据/发票分类器模型,基于 MobileNet V3。其目的是检查输入图像是否为有效的发票或收据。
使用以下数据集进行训练:
- https://huggingface.co/datasets/dajor85570/invoices-and-receipts_ocr_v1
- https://huggingface.co/datasets/pouya-haghi/imagenet-subset
使用方法
要使用该模型,首先安装一些必要的依赖项:
pip install huggingface_hub onnxruntime pillow numpy
然后运行以下代码:
import json, time, numpy as np
from pathlib import Path
from PIL import Image, ImageOps
import onnxruntime as ort
from huggingface_hub import hf_hub_download
REPO_ID = "huytd189/invoice-classifier"
MODEL_FN = "model.onnx"
LABELS_FN = "class_mapping.json"
IMG = "demo.jpg"
IMG_SIZE = 192
# 拉取文件
model_path = hf_hub_download(REPO_ID, MODEL_FN)
labels_path = hf_hub_download(REPO_ID, LABELS_FN)
# 标签
labels = {"0":"invalid","1":"valid"}
if Path(labels_path).exists():
labels = json.loads(Path(labels_path).read_text())
# 会话
sess = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"])
in_name, out_name = sess.get_inputs()[0].name, sess.get_outputs()[0].name
# 预处理图像输入
img = ImageOps.exif_transpose(Image.open(IMG)).convert("RGB").resize((IMG_SIZE, IMG_SIZE))
x = (np.asarray(img, np.float32) / 255.0)
x = np.transpose(x, (2,0,1))[None, ...]
# 运行
t0 = time.time()
(logits,) = sess.run([out_name], {in_name: x})
probs = np.exp(logits - logits.max()) / np.exp(logits - logits.max()).sum(-1, keepdims=True)
idx = int(probs.argmax())
print(f"pred={labels.get(str(idx), f'class{idx}')}, probs={probs[0].round(4).tolist()}, {1000*(time.time()-t0):.1f}ms")
演示


huytd189/invoice-classifier
作者 huytd189
image-classification
↓ 0
♥ 2
创建时间: 2025-10-25 04:50:32+00:00
更新时间: 2025-10-25 05:13:21+00:00
在 Hugging Face 上查看文件 (4)
.gitattributes
README.md
class_mapping.json
model.onnx
ONNX