TensorFlow/Keras model for detecting whether an image contains a PAN Card or Not PAN.
Source du modèle
Description de la source
TensorFlow/Keras model for detecting whether an image contains a PAN Card or Not PAN.
PAN_CardNot_PANSources
1 sourceVérifié 5 août
Artefacts du modèle
5 artefacts.venv/lib/python3.10/site-packages/_virtualenv.pth
pth · 18 B · SHA-256 69ac3d8f27e6…433d · Hugging Face
Télécharger.venv/lib/python3.10/site-packages/distutils-precedence.pth
pth · 151 B · SHA-256 2638ce9e2500…d224 · Hugging Face
TéléchargerExtraits de sources
2 extraitspip install tensorflow huggingface_hub pillow numpy
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)
{
"label": "PAN_Card",
"confidence": 99.87
}
or
{
"label": "Not_PAN",
"confidence": 98.45
}
pan_card_classifier.keras
class_indices.json
README.md
Shailendra Singh Tiwari
License: MIT
.venv/lib/python3.10/site-packages/h5py/tests/data_files/vlen_string_dset_utc.h5
h5 · 166 KB · SHA-256 85b728382b83…2f63 · Hugging Face
Télécharger.venv/lib/python3.10/site-packages/h5py/tests/data_files/vlen_string_dset.h5
h5 · 6,16 KB · SHA-256 900f8baf19d3…9793 · Hugging Face
Télécharger.venv/lib/python3.10/site-packages/h5py/tests/data_files/vlen_string_s390x.h5
h5 · 8,80 KB · SHA-256 ea990c68e037…4ca5 · Hugging Face
Télécharger--- library_name: tensorflow license: mit tags: - tensorflow - keras - image-classification - pan-card - computer-vision --- # 🪪 PAN Card Classifier TensorFlow/Keras model for detecting whether an image contains a **PAN Card** or **Not PAN**. ## Classes - `PAN_Card` - `Not_PAN` ## Install ```bash pip install tensorflow huggingface_hub pillow numpy ``` ## Usage ```python 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 { "lab...
Source context: 0 downloads · 0 likes · Pipeline image-classification · Library tensorflow · Repo shailgsits/pan-card-classifier