Upload 2 files
Browse files- EasyOpticalCharacterRecognition.py +81 -0
- MobileNetBest_Model.h5 +3 -0
EasyOpticalCharacterRecognition.py
ADDED
@@ -0,0 +1,81 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
import easyocr
|
4 |
+
from tensorflow.keras.models import load_model
|
5 |
+
from tensorflow.keras.preprocessing.image import img_to_array
|
6 |
+
import pickle
|
7 |
+
import os
|
8 |
+
from google.colab import drive
|
9 |
+
|
10 |
+
# === Mount Google Drive ===
|
11 |
+
drive.mount('/content/drive')
|
12 |
+
|
13 |
+
# === Load model and label encoder ===
|
14 |
+
model_path = '/content/drive/My Drive/ML1_Project/MobileNet/Model6/MobileNetBest_Model.h5'
|
15 |
+
pkl_path = '/content/drive/My Drive/ML1_Project/MobileNet/Model6/MobileNet_Label_Encoder.pkl'
|
16 |
+
|
17 |
+
model = load_model(model_path)
|
18 |
+
print("✅ Model loaded.")
|
19 |
+
|
20 |
+
if os.path.exists(pkl_path):
|
21 |
+
with open(pkl_path, 'rb') as f:
|
22 |
+
label_map = pickle.load(f)
|
23 |
+
index_to_label = {v: k for k, v in label_map.items()}
|
24 |
+
print("✅ Label encoder loaded.")
|
25 |
+
else:
|
26 |
+
index_to_label = {0: "Handwritten", 1: "Computerized"}
|
27 |
+
print("⚠️ Label encoder not found, using default mapping.")
|
28 |
+
|
29 |
+
# === Classification function ===
|
30 |
+
def classify_text_region(region_img):
|
31 |
+
try:
|
32 |
+
region_img = cv2.resize(region_img, (224, 224))
|
33 |
+
region_img = region_img.astype("float32") / 255.0
|
34 |
+
region_img = img_to_array(region_img)
|
35 |
+
region_img = np.expand_dims(region_img, axis=0)
|
36 |
+
|
37 |
+
preds = model.predict(region_img)
|
38 |
+
|
39 |
+
if preds.shape[-1] == 1:
|
40 |
+
return "Computerized" if preds[0][0] > 0.5 else "Handwritten"
|
41 |
+
else:
|
42 |
+
class_idx = np.argmax(preds[0])
|
43 |
+
return index_to_label.get(class_idx, "Unknown")
|
44 |
+
except Exception as e:
|
45 |
+
print("❌ Classification error:", e)
|
46 |
+
return "Unknown"
|
47 |
+
|
48 |
+
# === OCR + Annotation ===
|
49 |
+
def AnnotatedTextDetection_EasyOCR_from_array(img):
|
50 |
+
reader = easyocr.Reader(['en'], gpu=False)
|
51 |
+
results = reader.readtext(img)
|
52 |
+
annotated_results = []
|
53 |
+
|
54 |
+
for (bbox, text, conf) in results:
|
55 |
+
if conf < 0.3 or text.strip() == "":
|
56 |
+
continue
|
57 |
+
|
58 |
+
x1, y1 = map(int, bbox[0])
|
59 |
+
x2, y2 = map(int, bbox[2])
|
60 |
+
w, h = x2 - x1, y2 - y1
|
61 |
+
|
62 |
+
crop = img[y1:y2, x1:x2]
|
63 |
+
if crop.size == 0:
|
64 |
+
continue
|
65 |
+
|
66 |
+
label = classify_text_region(crop)
|
67 |
+
annotated_results.append(f"{text.strip()} → {label}")
|
68 |
+
|
69 |
+
color = (0, 255, 0) if label == "Computerized" else (255, 0, 0)
|
70 |
+
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
71 |
+
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 1)
|
72 |
+
|
73 |
+
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(annotated_results)
|
74 |
+
|
75 |
+
# === Main image processing function ===
|
76 |
+
def process_image(input_image):
|
77 |
+
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
78 |
+
result_img, text_result = AnnotatedTextDetection_EasyOCR_from_array(img)
|
79 |
+
return result_img, text_result
|
80 |
+
|
81 |
+
|
MobileNetBest_Model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:11fbc2c939321af199451bacab05f6533e6562310e41a498baf1e3cb80cf8b59
|
3 |
+
size 28405792
|