Ramzan0553 commited on
Commit
53bee95
·
verified ·
1 Parent(s): 56ac515

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +116 -0
app.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ 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))
35
+ region_img = region_img.astype("float32") / 255.0
36
+ region_img = img_to_array(region_img)
37
+ region_img = np.expand_dims(region_img, axis=0)
38
+
39
+ preds = model.predict(region_img)
40
+
41
+ if preds.shape[-1] == 1:
42
+ return "Computerized" if preds[0][0] > 0.5 else "Handwritten"
43
+ else:
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 = []
54
+
55
+ for (bbox, text, conf) in results[:20]: # Limit to top 20 boxes
56
+ if conf < 0.3 or text.strip() == "":
57
+ continue
58
+
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
+
65
+ label = classify_text_region(crop)
66
+ annotated_results.append(f"{text.strip()} → {label}")
67
+
68
+ color = (0, 255, 0) if label == "Computerized" else (255, 0, 0)
69
+ cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
70
+ cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 1)
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])
82
+ img = cv2.resize(img, (int(img.shape[1]*scale), int(img.shape[0]*scale)))
83
+
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;
91
+ }
92
+ .gradio-container {
93
+ border-radius: 12px;
94
+ padding: 20px;
95
+ border: 2px solid #007acc;
96
+ }
97
+ .gr-input, .gr-output {
98
+ border: 1px solid #007acc;
99
+ border-radius: 10px;
100
+ }
101
+ """
102
+
103
+ # === Launch Interface ===
104
+ demo = gr.Interface(
105
+ fn=infer,
106
+ inputs=gr.Image(type="pil", label="Upload Image"),
107
+ outputs=[
108
+ gr.Image(type="pil", label="Annotated Image"),
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()