Delete EasyOpticalCharacterRecognition.py
Browse files
EasyOpticalCharacterRecognition.py
DELETED
|
@@ -1,66 +0,0 @@
|
|
| 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 |
-
|
| 9 |
-
# === Load model and label encoder ===
|
| 10 |
-
model_path = 'MobileNetBest_Model.h5'
|
| 11 |
-
pkl_path = 'MobileNet_Label_Encoder.pkl'
|
| 12 |
-
|
| 13 |
-
model = load_model(model_path)
|
| 14 |
-
print("✅ Model loaded.")
|
| 15 |
-
|
| 16 |
-
# === Classification function ===
|
| 17 |
-
def classify_text_region(region_img):
|
| 18 |
-
try:
|
| 19 |
-
region_img = cv2.resize(region_img, (224, 224))
|
| 20 |
-
region_img = region_img.astype("float32") / 255.0
|
| 21 |
-
region_img = img_to_array(region_img)
|
| 22 |
-
region_img = np.expand_dims(region_img, axis=0)
|
| 23 |
-
|
| 24 |
-
preds = model.predict(region_img)
|
| 25 |
-
|
| 26 |
-
if preds.shape[-1] == 1:
|
| 27 |
-
return "Computerized" if preds[0][0] > 0.5 else "Handwritten"
|
| 28 |
-
else:
|
| 29 |
-
class_idx = np.argmax(preds[0])
|
| 30 |
-
return index_to_label.get(class_idx, "Unknown")
|
| 31 |
-
except Exception as e:
|
| 32 |
-
print("❌ Classification error:", e)
|
| 33 |
-
return "Unknown"
|
| 34 |
-
|
| 35 |
-
# === OCR + Annotation ===
|
| 36 |
-
def AnnotatedTextDetection_EasyOCR_from_array(img):
|
| 37 |
-
reader = easyocr.Reader(['en'], gpu=False)
|
| 38 |
-
results = reader.readtext(img)
|
| 39 |
-
annotated_results = []
|
| 40 |
-
|
| 41 |
-
for (bbox, text, conf) in results:
|
| 42 |
-
if conf < 0.3 or text.strip() == "":
|
| 43 |
-
continue
|
| 44 |
-
|
| 45 |
-
x1, y1 = map(int, bbox[0])
|
| 46 |
-
x2, y2 = map(int, bbox[2])
|
| 47 |
-
w, h = x2 - x1, y2 - y1
|
| 48 |
-
|
| 49 |
-
crop = img[y1:y2, x1:x2]
|
| 50 |
-
if crop.size == 0:
|
| 51 |
-
continue
|
| 52 |
-
|
| 53 |
-
label = classify_text_region(crop)
|
| 54 |
-
annotated_results.append(f"{text.strip()} → {label}")
|
| 55 |
-
|
| 56 |
-
color = (0, 255, 0) if label == "Computerized" else (255, 0, 0)
|
| 57 |
-
cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
|
| 58 |
-
cv2.putText(img, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, color, 1)
|
| 59 |
-
|
| 60 |
-
return cv2.cvtColor(img, cv2.COLOR_BGR2RGB), "\n".join(annotated_results)
|
| 61 |
-
|
| 62 |
-
# === Main image processing function ===
|
| 63 |
-
def process_image(input_image):
|
| 64 |
-
img = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
| 65 |
-
result_img, text_result = AnnotatedTextDetection_EasyOCR_from_array(img)
|
| 66 |
-
return result_img, text_result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|