|
|
--- |
|
|
license: bsd-3-clause-clear |
|
|
datasets: |
|
|
- pouya-haghi/imagenet-subset |
|
|
- dajor85570/invoices-and-receipts_ocr_v1 |
|
|
tags: |
|
|
- classification |
|
|
- image-classification |
|
|
- invoice-detection |
|
|
- receipt-detection |
|
|
--- |
|
|
|
|
|
# Invoice Classifier |
|
|
|
|
|
This is the receipt/invoice classifier model, based on MobileNet V3. Its purpose is to check if an input image is a valid invoice or receipt. |
|
|
|
|
|
Trained using the following datasets: |
|
|
- https://huggingface.co/datasets/dajor85570/invoices-and-receipts_ocr_v1 |
|
|
- https://huggingface.co/datasets/pouya-haghi/imagenet-subset |
|
|
|
|
|
|
|
|
## Usage |
|
|
|
|
|
To use the model, first, install some neccessary dependencies: |
|
|
|
|
|
``` |
|
|
pip install huggingface_hub onnxruntime pillow numpy |
|
|
``` |
|
|
|
|
|
Then run the code: |
|
|
|
|
|
```python |
|
|
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 |
|
|
|
|
|
# pull files |
|
|
model_path = hf_hub_download(REPO_ID, MODEL_FN) |
|
|
labels_path = hf_hub_download(REPO_ID, LABELS_FN) |
|
|
|
|
|
# labels |
|
|
labels = {"0":"invalid","1":"valid"} |
|
|
if Path(labels_path).exists(): |
|
|
labels = json.loads(Path(labels_path).read_text()) |
|
|
|
|
|
# session |
|
|
sess = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"]) |
|
|
in_name, out_name = sess.get_inputs()[0].name, sess.get_outputs()[0].name |
|
|
|
|
|
# preprocess image input |
|
|
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, ...] |
|
|
|
|
|
# run |
|
|
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") |
|
|
``` |
|
|
|
|
|
## Demo |
|
|
|
|
|
 |
|
|
|
|
|
 |
|
|
|
|
|
|