Instructions to use shailgsits/pan-card-classifier with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use shailgsits/pan-card-classifier with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://shailgsits/pan-card-classifier") - Notebooks
- Google Colab
- Kaggle
🪪 PAN Card Classifier
TensorFlow/Keras model for detecting whether an image contains a PAN Card or Not PAN.
Classes
PAN_CardNot_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
- Hugging Face: https://huggingface.co/shailgsits
- GitHub: https://github.com/shailgsits
License: MIT