Ali0044 commited on
Commit
fc09984
·
verified ·
1 Parent(s): 0901703

Upload README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. README.md +24 -18
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. Built on **Keras 3**, it supports **JAX**, **PyTorch**, and **TensorFlow** backends.
16
 
17
  ## 🚀 Quick Start (Robust Usage)
18
 
19
- For maximum reliability across all environments, use the `huggingface_hub` downloader.
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 the portable model file from the Hub
39
- print(f"Downloading model from {repo_id}...")
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
- # Standard Arabic Vocabulary
47
- self.vocab = [' ', '!', '"', '#', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', ':', ';', '=', '?', '[', ']', 'ء', 'آ', 'أ', 'ؤ', 'إ', 'ئ', 'ا', 'ب', 'ة', 'ت', 'ث', 'ج', 'ح', 'خ', 'د', 'ذ', 'ر', 'ز', 'س', 'ش', 'ص', 'ض', 'ط', 'ظ', 'ع', 'غ', 'ـ', 'ف', 'ق', 'ك', 'ل', 'م', 'ن', 'ه', 'و', 'ى', 'ي', 'ً', 'ٌ', 'ٍ', 'َ', 'ُ', 'ِ', 'ّ', 'ْ', '٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩']
 
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 # (Height, Width) -> (Width, Height)
53
- img = np.expand_dims(img, axis=(-1, 0)) # Add Channel and Batch
54
  return img.astype(np.float32)
55
 
56
  def predict(self, image_path):
57
  batch_img = self.preprocess(image_path)
58
- predictions = self.model.predict(batch_img)
 
 
 
 
 
 
 
59
 
60
- # CTC Decode
61
- input_len = np.ones(predictions.shape[0]) * predictions.shape[1]
62
- results = keras.backend.ctc_decode(predictions, input_length=input_len, greedy=True)[0][0]
63
 
64
- return "".join([self.vocab[int(res)] for res in results[0] if res != -1])
 
65
 
66
- # Run
67
  # ocr = QalamNet()
68
- # print(f"Output: {ocr.predict('test_sample.png')}")
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