Ramzan0553 commited on
Commit
3200fc7
·
verified ·
1 Parent(s): 6b94d5d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -16
app.py CHANGED
@@ -3,32 +3,38 @@ import cv2
3
  import numpy as np
4
  from PIL import Image
5
  import pickle
 
6
  from tensorflow.keras.models import load_model
7
  from tensorflow.keras.preprocessing.image import img_to_array
8
  import easyocr
 
9
 
10
- # === Load Model and Label Encoder ===
 
 
 
 
11
  model_path = "MobileNetBest_Model.h5"
12
  label_path = "MobileNet_Label_Encoder.pkl"
13
 
14
  model = load_model(model_path)
15
- print("Model loaded.")
16
 
17
- # Load label encoder
18
  try:
19
  with open(label_path, 'rb') as f:
20
  label_map = pickle.load(f)
21
  index_to_label = {v: k for k, v in label_map.items()}
22
- print("Label encoder loaded:", index_to_label)
23
  except:
24
  index_to_label = {0: "Handwritten", 1: "Computerized"}
25
- print("Label encoder not found. Using default:", index_to_label)
26
 
27
- # === Initialize EasyOCR Reader Once (with GPU) ===
28
- reader = easyocr.Reader(['en'], gpu=True)
29
- print("EasyOCR Reader initialized with GPU.")
30
 
31
- # === Classify Region ===
32
  def classify_text_region(region_img):
33
  try:
34
  region_img = cv2.resize(region_img, (224, 224))
@@ -44,10 +50,10 @@ def classify_text_region(region_img):
44
  class_idx = np.argmax(preds[0])
45
  return index_to_label.get(class_idx, "Unknown")
46
  except Exception as e:
47
- print("Classification error:", e)
48
  return "Unknown"
49
 
50
- # === OCR + Annotation ===
51
  def AnnotatedTextDetection_EasyOCR_from_array(img):
52
  results = reader.readtext(img)
53
  annotated_results = []
@@ -59,6 +65,7 @@ def AnnotatedTextDetection_EasyOCR_from_array(img):
59
  x1, y1 = map(int, bbox[0])
60
  x2, y2 = map(int, bbox[2])
61
  crop = img[y1:y2, x1:x2]
 
62
  if crop.size == 0:
63
  continue
64
 
@@ -71,11 +78,10 @@ def AnnotatedTextDetection_EasyOCR_from_array(img):
71
 
72
  return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(annotated_results)
73
 
74
- # === Gradio Wrapper ===
75
  def infer(image):
76
  img = np.array(image)
77
 
78
- # Resize if image is too large
79
  max_dim = 1000
80
  if img.shape[0] > max_dim or img.shape[1] > max_dim:
81
  scale = max_dim / max(img.shape[0], img.shape[1])
@@ -84,7 +90,7 @@ def infer(image):
84
  annotated_img, result_text = AnnotatedTextDetection_EasyOCR_from_array(img)
85
  return Image.fromarray(annotated_img), result_text
86
 
87
- # === Custom CSS ===
88
  custom_css = """
89
  body {
90
  background-color: #e6f2ff;
@@ -100,7 +106,6 @@ body {
100
  }
101
  """
102
 
103
- # === Launch Interface ===
104
  demo = gr.Interface(
105
  fn=infer,
106
  inputs=gr.Image(type="pil", label="Upload Image"),
@@ -109,8 +114,9 @@ demo = gr.Interface(
109
  gr.Textbox(label="Detected Text and Classification")
110
  ],
111
  title="Text Detection and Classification",
112
- description="This application detects text using EasyOCR and classifies each text region as Handwritten or Computerized using a MobileNet model.",
113
  theme="soft",
114
  css=custom_css
115
  )
 
116
  demo.launch()
 
3
  import numpy as np
4
  from PIL import Image
5
  import pickle
6
+ import tensorflow as tf
7
  from tensorflow.keras.models import load_model
8
  from tensorflow.keras.preprocessing.image import img_to_array
9
  import easyocr
10
+ import torch
11
 
12
+ # ========== GPU Checks ==========
13
+ print("Torch GPU Available:", torch.cuda.is_available())
14
+ print("TensorFlow GPU Devices:", tf.config.list_physical_devices('GPU'))
15
+
16
+ # ========== Load Model and Label Encoder ==========
17
  model_path = "MobileNetBest_Model.h5"
18
  label_path = "MobileNet_Label_Encoder.pkl"
19
 
20
  model = load_model(model_path)
21
+ print(" MobileNet model loaded.")
22
 
23
+ # Label encoder
24
  try:
25
  with open(label_path, 'rb') as f:
26
  label_map = pickle.load(f)
27
  index_to_label = {v: k for k, v in label_map.items()}
28
+ print("Label encoder loaded:", index_to_label)
29
  except:
30
  index_to_label = {0: "Handwritten", 1: "Computerized"}
31
+ print("⚠️ Default labels used:", index_to_label)
32
 
33
+ # ========== Initialize EasyOCR (Force GPU) ==========
34
+ reader = easyocr.Reader(['en'], gpu=torch.cuda.is_available())
35
+ print("EasyOCR initialized with GPU:", torch.cuda.is_available())
36
 
37
+ # ========== Classify One Region ==========
38
  def classify_text_region(region_img):
39
  try:
40
  region_img = cv2.resize(region_img, (224, 224))
 
50
  class_idx = np.argmax(preds[0])
51
  return index_to_label.get(class_idx, "Unknown")
52
  except Exception as e:
53
+ print("Classification error:", e)
54
  return "Unknown"
55
 
56
+ # ========== OCR & Annotate ==========
57
  def AnnotatedTextDetection_EasyOCR_from_array(img):
58
  results = reader.readtext(img)
59
  annotated_results = []
 
65
  x1, y1 = map(int, bbox[0])
66
  x2, y2 = map(int, bbox[2])
67
  crop = img[y1:y2, x1:x2]
68
+
69
  if crop.size == 0:
70
  continue
71
 
 
78
 
79
  return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(annotated_results)
80
 
81
+ # ========== Inference Function ==========
82
  def infer(image):
83
  img = np.array(image)
84
 
 
85
  max_dim = 1000
86
  if img.shape[0] > max_dim or img.shape[1] > max_dim:
87
  scale = max_dim / max(img.shape[0], img.shape[1])
 
90
  annotated_img, result_text = AnnotatedTextDetection_EasyOCR_from_array(img)
91
  return Image.fromarray(annotated_img), result_text
92
 
93
+ # ========== Gradio UI ==========
94
  custom_css = """
95
  body {
96
  background-color: #e6f2ff;
 
106
  }
107
  """
108
 
 
109
  demo = gr.Interface(
110
  fn=infer,
111
  inputs=gr.Image(type="pil", label="Upload Image"),
 
114
  gr.Textbox(label="Detected Text and Classification")
115
  ],
116
  title="Text Detection and Classification",
117
+ description="Application detects text from images and classify into Handwritten/Computerized Text",
118
  theme="soft",
119
  css=custom_css
120
  )
121
+
122
  demo.launch()