Ali0044 commited on
Commit
dbd74b6
·
verified ·
1 Parent(s): 73dec90

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +18 -26
README.md CHANGED
@@ -6,11 +6,13 @@ tags:
6
  - arabic
7
  - keras
8
  - jax
 
 
9
  ---
10
 
11
  # Qalam-Net (قلم-نت): Advanced Arabic OCR
12
 
13
- Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic. Built on **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow** backends.
14
 
15
  ## 🚀 Quick Start (Advanced Usage)
16
 
@@ -24,36 +26,32 @@ pip install -U "keras>=3.0" jax jaxlib huggingface_hub opencv-python
24
  ### 2. Implementation
25
  ```python
26
  import os
27
- os.environ["KERAS_BACKEND"] = "jax" # Options: "jax", "tensorflow", "torch"
28
 
29
  import keras
30
  import numpy as np
31
  import cv2
 
32
 
33
  class QalamNet:
34
  def __init__(self, repo_id="Ali0044/Qalam-Net"):
35
- # Download and load the latest model using the hf:// shorthand
36
- # This automatically downloads the model.keras file from the root of the repo
37
- self.model = keras.saving.load_model(f"hf://{repo_id}")
 
38
 
39
- # Standard Arabic Vocabulary (Matches training set)
40
  self.vocab = [' ', '!', '"', '#', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '=', '?', '[', ']', 'ء', 'آ', 'أ', 'ؤ', 'إ', 'ئ', 'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ـ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ', 'ُ', 'ِ', 'ّ', 'ْ', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
41
 
42
  def preprocess(self, image_path):
43
- # 1. Load as grayscale
44
  img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
45
- # 2. Resize to 128 (width) x 32 (height)
46
  img = cv2.resize(img, (128, 32))
47
- # 3. Normalize
48
  img = (img / 255.0).astype(np.float32)
49
- # 4. Transpose (W, H) -> (H, W) for CRNN processing
50
  img = img.T
51
- # 5. Expand dimensions for batch and channel
52
  img = np.expand_dims(img, axis=-1)
53
  return np.expand_dims(img, axis=0)
54
 
55
  def predict(self, image_path):
56
- # Run inference
57
  batch_img = self.preprocess(image_path)
58
  predictions = self.model.predict(batch_img)
59
 
@@ -62,26 +60,20 @@ class QalamNet:
62
  results = keras.backend.ctc_decode(predictions, input_length=input_len, greedy=True)[0][0]
63
 
64
  # Map indices to characters
65
- text = ""
66
- for res in results[0]:
67
- if res != -1:
68
- text += self.vocab[int(res)]
69
  return text
70
 
71
- # Initialize
72
- ocr = QalamNet()
73
-
74
- # Predict
75
- # text = ocr.predict("sample_arabic_text.png")
76
- # print(f"Predicted Text: {text}")
77
  ```
78
 
79
  ## 🧠 Model Architecture
80
  Qalam-Net employs a specialized **CNN-BiLSTM-Attention** pipeline:
81
- - **Spatial Features**: 3-block CNN with BatchNormalization.
82
- - **Sequence Context**: Stacked Bidirectional LSTMs.
83
- - **Focus Mechanism**: Self-attention layer to resolve overlapping Arabic characters.
84
- - **Loss**: Trained using Connectionist Temporal Classification (CTC).
85
 
86
  ---
87
  **Developed by [Ali Khalid](https://github.com/Ali0044)**
 
6
  - arabic
7
  - keras
8
  - jax
9
+ - tensorflow
10
+ - pytorch
11
  ---
12
 
13
  # Qalam-Net (قلم-نت): Advanced Arabic OCR
14
 
15
+ Qalam-Net is a high-performance, cross-backend Optical Character Recognition (OCR) model for Arabic. Built on **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow** backends for seamless deployment across any infrastructure.
16
 
17
  ## 🚀 Quick Start (Advanced Usage)
18
 
 
26
  ### 2. Implementation
27
  ```python
28
  import os
29
+ os.environ["KERAS_BACKEND"] = "jax" # Switch to "tensorflow" or "torch" if preferred
30
 
31
  import keras
32
  import numpy as np
33
  import cv2
34
+ from huggingface_hub import hf_hub_download
35
 
36
  class QalamNet:
37
  def __init__(self, repo_id="Ali0044/Qalam-Net"):
38
+ # Download and load the latest model from Hugging Face
39
+ print(f"Fetching model 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
+ # Standard Arabic Vocabulary
44
  self.vocab = [' ', '!', '"', '#', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '=', '?', '[', ']', 'ء', 'آ', 'أ', 'ؤ', 'إ', 'ئ', 'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ـ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ', 'ُ', 'ِ', 'ّ', 'ْ', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
45
 
46
  def preprocess(self, image_path):
 
47
  img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
 
48
  img = cv2.resize(img, (128, 32))
 
49
  img = (img / 255.0).astype(np.float32)
 
50
  img = img.T
 
51
  img = np.expand_dims(img, axis=-1)
52
  return np.expand_dims(img, axis=0)
53
 
54
  def predict(self, image_path):
 
55
  batch_img = self.preprocess(image_path)
56
  predictions = self.model.predict(batch_img)
57
 
 
60
  results = keras.backend.ctc_decode(predictions, input_length=input_len, greedy=True)[0][0]
61
 
62
  # Map indices to characters
63
+ text = "".join([self.vocab[int(res)] for res in results[0] if res != -1])
 
 
 
64
  return text
65
 
66
+ # Usage
67
+ # ocr = QalamNet()
68
+ # print(f"Predicted: {ocr.predict('image.png')}")
 
 
 
69
  ```
70
 
71
  ## 🧠 Model Architecture
72
  Qalam-Net employs a specialized **CNN-BiLSTM-Attention** pipeline:
73
+ - **Spatial Features**: 3-block CNN for robust feature extraction.
74
+ - **Sequence Context**: Dual Bidirectional LSTMs to capture Arabic script flow.
75
+ - **Focus Mechanism**: Self-attention layer for character-level precision.
76
+ - **Decoding**: Connectionist Temporal Classification (CTC).
77
 
78
  ---
79
  **Developed by [Ali Khalid](https://github.com/Ali0044)**