Delete app.py
Browse files
app.py
DELETED
@@ -1,108 +0,0 @@
|
|
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 |
-
# === Classify Region ===
|
28 |
-
def classify_text_region(region_img):
|
29 |
-
try:
|
30 |
-
region_img = cv2.resize(region_img, (224, 224))
|
31 |
-
region_img = region_img.astype("float32") / 255.0
|
32 |
-
region_img = img_to_array(region_img)
|
33 |
-
region_img = np.expand_dims(region_img, axis=0)
|
34 |
-
|
35 |
-
preds = model.predict(region_img)
|
36 |
-
if preds.shape[-1] == 1:
|
37 |
-
return "Computerized" if preds[0][0] > 0.5 else "Handwritten"
|
38 |
-
else:
|
39 |
-
class_idx = np.argmax(preds[0])
|
40 |
-
return index_to_label.get(class_idx, "Unknown")
|
41 |
-
except Exception as e:
|
42 |
-
print("❌ Classification error:", e)
|
43 |
-
return "Unknown"
|
44 |
-
|
45 |
-
# === OCR and Annotate ===
|
46 |
-
def AnnotatedTextDetection_EasyOCR_from_array(img):
|
47 |
-
reader = easyocr.Reader(['en'], gpu=False)
|
48 |
-
results = reader.readtext(img)
|
49 |
-
annotated_results = []
|
50 |
-
|
51 |
-
for (bbox, text, conf) in results:
|
52 |
-
if conf < 0.3 or text.strip() == "":
|
53 |
-
continue
|
54 |
-
|
55 |
-
x1, y1 = map(int, bbox[0])
|
56 |
-
x2, y2 = map(int, bbox[2])
|
57 |
-
w, h = x2 - x1, y2 - y1
|
58 |
-
|
59 |
-
crop = img[y1:y2, x1:x2]
|
60 |
-
if crop.size == 0:
|
61 |
-
continue
|
62 |
-
|
63 |
-
label = classify_text_region(crop)
|
64 |
-
annotated_results.append(f"{text.strip()} → {label}")
|
65 |
-
|
66 |
-
color = (0, 255, 0) if label == "Computerized" else (255, 0, 0)
|
67 |
-
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
68 |
-
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 1)
|
69 |
-
|
70 |
-
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(annotated_results)
|
71 |
-
|
72 |
-
# === Gradio Wrapper ===
|
73 |
-
def infer(image):
|
74 |
-
img = np.array(image)
|
75 |
-
annotated_img, result_text = AnnotatedTextDetection_EasyOCR_from_array(img)
|
76 |
-
return Image.fromarray(annotated_img), result_text
|
77 |
-
|
78 |
-
# === Custom CSS ===
|
79 |
-
custom_css = """
|
80 |
-
body {
|
81 |
-
background-color: #e6f2ff;
|
82 |
-
}
|
83 |
-
.gradio-container {
|
84 |
-
border-radius: 12px;
|
85 |
-
padding: 20px;
|
86 |
-
border: 2px solid #007acc;
|
87 |
-
}
|
88 |
-
.gr-input, .gr-output {
|
89 |
-
border: 1px solid #007acc;
|
90 |
-
border-radius: 10px;
|
91 |
-
}
|
92 |
-
"""
|
93 |
-
|
94 |
-
# === Launch Interface ===
|
95 |
-
demo = gr.Interface(
|
96 |
-
fn=infer,
|
97 |
-
inputs=gr.Image(type="pil", label="Upload Image"),
|
98 |
-
outputs=[
|
99 |
-
gr.Image(type="pil", label="Annotated Image"),
|
100 |
-
gr.Textbox(label="Detected Text and Classification")
|
101 |
-
],
|
102 |
-
title="Text Detection and Clssification",
|
103 |
-
description="This application detects text using EasyOCR and classifies each text region as Handwritten or Computerized using a MobileNet model.",
|
104 |
-
theme="soft",
|
105 |
-
css=custom_css
|
106 |
-
)
|
107 |
-
|
108 |
-
demo.launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|