🪪 PAN Card Classifier

TensorFlow/Keras model for detecting whether an image contains a PAN Card or Not PAN.

Classes

  • PAN_Card
  • Not_PAN

Install

pip install tensorflow huggingface_hub pillow numpy

Usage

import json
import numpy as np
import tensorflow as tf
import requests

from io import BytesIO
from PIL import Image
from huggingface_hub import hf_hub_download

# ---------------------------------------------------
# Download model from Hugging Face
# ---------------------------------------------------

MODEL_PATH = hf_hub_download(
    repo_id="shailgsits/pan-card-classifier",
    filename="pan_card_classifier.keras"
)

CLASS_PATH = hf_hub_download(
    repo_id="shailgsits/pan-card-classifier",
    filename="class_indices.json"
)

# ---------------------------------------------------
# Load model
# ---------------------------------------------------

model = tf.keras.models.load_model(MODEL_PATH)

# ---------------------------------------------------
# Load class labels
# ---------------------------------------------------

with open(CLASS_PATH) as f:
    class_indices = json.load(f)

labels = {v: k for k, v in class_indices.items()}

# ---------------------------------------------------
# Load image
# ---------------------------------------------------

def load_image(image_source):

    # Google Drive share link
    if "drive.google.com/file/d/" in image_source:
        file_id = image_source.split("/d/")[1].split("/")[0]
        image_source = (
            f"https://drive.google.com/uc?export=download&id={file_id}"
        )

    # URL
    if image_source.startswith(("http://", "https://")):

        response = requests.get(image_source)
        response.raise_for_status()

        return Image.open(BytesIO(response.content)).convert("RGB")

    # Local file
    return Image.open(image_source).convert("RGB")


# ---------------------------------------------------
# Predict
# ---------------------------------------------------

def predict(image_source):

    image = load_image(image_source)

    image = image.resize((224, 224))

    image = np.array(image).astype(np.float32) / 255.0

    image = np.expand_dims(image, axis=0)

    prediction = model.predict(image, verbose=0)[0]

    class_id = int(np.argmax(prediction))

    confidence = float(prediction[class_id])

    return {
        "label": labels[class_id],
        "confidence": round(confidence * 100, 2),
        "all_scores": {
            labels[i]: round(float(score) * 100, 2)
            for i, score in enumerate(prediction)
        },
    }


# ---------------------------------------------------
# Example
# ---------------------------------------------------

# Local file
# result = predict("pan.jpg")

# Direct image URL
# result = predict("https://example.com/pan.jpg")

# Google Drive Share Link
result = predict(
    "https://drive.google.com/file/d/1yeDbVn6uzG4mwu-OF6LNdKhen5YJl4F-/view?usp=sharing"
)

print(result)

Example Output

{
    "label": "PAN_Card",
    "confidence": 99.87
}

or

{
    "label": "Not_PAN",
    "confidence": 98.45
}

Repository Files

pan_card_classifier.keras
class_indices.json
README.md

Author

Shailendra Singh Tiwari

License: MIT

Downloads last month

-

Downloads are not tracked for this model. How to track
Inference Providers NEW
This model isn't deployed by any Inference Provider. 🙋 Ask for provider support