Instructions to use Ali0044/Qalam-Net with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Keras
How to use Ali0044/Qalam-Net with Keras:
# Available backend options are: "jax", "torch", "tensorflow". import os os.environ["KERAS_BACKEND"] = "jax" import keras model = keras.saving.load_model("hf://Ali0044/Qalam-Net") - Notebooks
- Google Colab
- Kaggle
Upload README.md with huggingface_hub
Browse files
README.md
CHANGED
|
@@ -12,11 +12,11 @@ tags:
|
|
| 12 |
|
| 13 |
# Qalam-Net (قلم-نت): Advanced Arabic OCR (v2 Portable)
|
| 14 |
|
| 15 |
-
Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic.
|
| 16 |
|
| 17 |
## 🚀 Quick Start (Robust Usage)
|
| 18 |
|
| 19 |
-
|
| 20 |
|
| 21 |
### 1. Installation
|
| 22 |
```bash
|
|
@@ -35,37 +35,43 @@ from huggingface_hub import hf_hub_download
|
|
| 35 |
|
| 36 |
class QalamNet:
|
| 37 |
def __init__(self, repo_id="Ali0044/Qalam-Net"):
|
| 38 |
-
# 1. Download
|
| 39 |
-
print(f"
|
| 40 |
model_path = hf_hub_download(repo_id=repo_id, filename="model.keras")
|
| 41 |
-
|
| 42 |
-
# 2. Load the model (Self-contained v2 requires no custom_objects)
|
| 43 |
self.model = keras.saving.load_model(model_path)
|
| 44 |
-
print("Model loaded successfully!")
|
| 45 |
|
| 46 |
-
#
|
| 47 |
-
|
|
|
|
| 48 |
|
| 49 |
def preprocess(self, image_path):
|
| 50 |
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
| 51 |
img = cv2.resize(img, (128, 32)) / 255.0
|
| 52 |
-
img = img.T #
|
| 53 |
-
img = np.expand_dims(img, axis=(-1, 0))
|
| 54 |
return img.astype(np.float32)
|
| 55 |
|
| 56 |
def predict(self, image_path):
|
| 57 |
batch_img = self.preprocess(image_path)
|
| 58 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
|
| 63 |
|
| 64 |
-
|
|
|
|
| 65 |
|
| 66 |
-
#
|
| 67 |
# ocr = QalamNet()
|
| 68 |
-
# print(f"
|
| 69 |
```
|
| 70 |
|
| 71 |
## 🧠 Model Architecture
|
|
|
|
| 12 |
|
| 13 |
# Qalam-Net (قلم-نت): Advanced Arabic OCR (v2 Portable)
|
| 14 |
|
| 15 |
+
Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic. Patched for **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow**.
|
| 16 |
|
| 17 |
## 🚀 Quick Start (Robust Usage)
|
| 18 |
|
| 19 |
+
This guide uses a custom **NumPy-based decoder** to ensure compatibility across all Keras 3 backends without needing `tf.keras.backend.ctc_decode`.
|
| 20 |
|
| 21 |
### 1. Installation
|
| 22 |
```bash
|
|
|
|
| 35 |
|
| 36 |
class QalamNet:
|
| 37 |
def __init__(self, repo_id="Ali0044/Qalam-Net"):
|
| 38 |
+
# 1. Download and Load Model
|
| 39 |
+
print(f"Loading Qalam-Net from {repo_id}...")
|
| 40 |
model_path = hf_hub_download(repo_id=repo_id, filename="model.keras")
|
|
|
|
|
|
|
| 41 |
self.model = keras.saving.load_model(model_path)
|
|
|
|
| 42 |
|
| 43 |
+
# 2. Define the exact 38-character Arabic Vocabulary
|
| 44 |
+
# [ALIF, BA, TA, THA, JEEM, HAA, KHAA, DAL, THAL, RA, ZAY, SEEN, SHEEN, SAD, DAD, TAA, ZAA, AIN, GHAIN, FA, QAF, KAF, LAM, MEEM, NOON, HA, WAW, YA, TEH_MARBUTA, ALEF_MAKSURA, ALEF_HAMZA_ABOVE, ALEF_HAMZA_BELOW, ALEF_MADDA, WAW_HAMZA, YEH_HAMZA, HAMZA, SPACE, TATWEEL]
|
| 45 |
+
self.vocab = ['ا', 'ب', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ي', 'ة', 'ى', 'أ', 'إ', 'آ', 'ؤ', 'ئ', 'ء', ' ', 'ـ']
|
| 46 |
|
| 47 |
def preprocess(self, image_path):
|
| 48 |
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
| 49 |
img = cv2.resize(img, (128, 32)) / 255.0
|
| 50 |
+
img = img.T # Transpose for CRNN architecture
|
| 51 |
+
img = np.expand_dims(img, axis=(-1, 0))
|
| 52 |
return img.astype(np.float32)
|
| 53 |
|
| 54 |
def predict(self, image_path):
|
| 55 |
batch_img = self.preprocess(image_path)
|
| 56 |
+
preds = self.model.predict(batch_img) # Output shape: (1, 32, 39)
|
| 57 |
+
|
| 58 |
+
# 3. NumPy-based CTC Greedy Decoding (Cross-Backend)
|
| 59 |
+
argmax_preds = np.argmax(preds, axis=-1)[0]
|
| 60 |
+
|
| 61 |
+
# Remove consecutive duplicates
|
| 62 |
+
unique_indices = [argmax_preds[i] for i in range(len(argmax_preds))
|
| 63 |
+
if i == 0 or argmax_preds[i] != argmax_preds[i-1]]
|
| 64 |
|
| 65 |
+
# Remove blank index (index 38)
|
| 66 |
+
blank_index = preds.shape[-1] - 1
|
| 67 |
+
final_indices = [idx for idx in unique_indices if idx != blank_index]
|
| 68 |
|
| 69 |
+
# Map to vocabulary
|
| 70 |
+
return "".join([self.vocab[idx] for idx in final_indices if idx < len(self.vocab)])
|
| 71 |
|
| 72 |
+
# Usage
|
| 73 |
# ocr = QalamNet()
|
| 74 |
+
# print(f"Predicted Arabic Text: {ocr.predict('sample.png')}")
|
| 75 |
```
|
| 76 |
|
| 77 |
## 🧠 Model Architecture
|