Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
import json
|
| 5 |
-
from PIL import Image
|
| 6 |
|
| 7 |
# 1. Load Model and Labels
|
| 8 |
model = tf.keras.models.load_model('devanagari_model.keras')
|
|
@@ -14,15 +14,26 @@ with open('labels.json', 'r') as f:
|
|
| 14 |
def process_image(image):
|
| 15 |
# Convert to grayscale (L)
|
| 16 |
image = image.convert('L')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
# Resize to 32x32 (dataset size)
|
| 18 |
image = image.resize((32, 32))
|
|
|
|
| 19 |
# Convert to array
|
| 20 |
img_array = np.array(image)
|
|
|
|
| 21 |
# Normalize to 0-1
|
| 22 |
img_array = img_array / 255.0
|
|
|
|
| 23 |
# Add batch dimension (1, 32, 32, 1)
|
| 24 |
img_array = np.expand_dims(img_array, axis=0)
|
| 25 |
img_array = np.expand_dims(img_array, axis=-1)
|
|
|
|
| 26 |
return img_array
|
| 27 |
|
| 28 |
# 3. Prediction Function
|
|
@@ -35,9 +46,14 @@ def predict_character(image):
|
|
| 35 |
|
| 36 |
# Get top prediction
|
| 37 |
predicted_class_index = np.argmax(predictions)
|
|
|
|
|
|
|
| 38 |
predicted_label = labels[str(predicted_class_index)]
|
|
|
|
|
|
|
| 39 |
confidence = float(np.max(predictions))
|
| 40 |
|
|
|
|
| 41 |
return {predicted_label: confidence}
|
| 42 |
|
| 43 |
# 4. Gradio Interface
|
|
|
|
| 2 |
import tensorflow as tf
|
| 3 |
import numpy as np
|
| 4 |
import json
|
| 5 |
+
from PIL import Image, ImageOps # Added ImageOps for inversion
|
| 6 |
|
| 7 |
# 1. Load Model and Labels
|
| 8 |
model = tf.keras.models.load_model('devanagari_model.keras')
|
|
|
|
| 14 |
def process_image(image):
|
| 15 |
# Convert to grayscale (L)
|
| 16 |
image = image.convert('L')
|
| 17 |
+
|
| 18 |
+
# --- CRITICAL FIX START ---
|
| 19 |
+
# Invert colors: Black text/White bg -> White text/Black bg
|
| 20 |
+
# This matches the UCI dataset format used in training.
|
| 21 |
+
image = ImageOps.invert(image)
|
| 22 |
+
# --- CRITICAL FIX END ---
|
| 23 |
+
|
| 24 |
# Resize to 32x32 (dataset size)
|
| 25 |
image = image.resize((32, 32))
|
| 26 |
+
|
| 27 |
# Convert to array
|
| 28 |
img_array = np.array(image)
|
| 29 |
+
|
| 30 |
# Normalize to 0-1
|
| 31 |
img_array = img_array / 255.0
|
| 32 |
+
|
| 33 |
# Add batch dimension (1, 32, 32, 1)
|
| 34 |
img_array = np.expand_dims(img_array, axis=0)
|
| 35 |
img_array = np.expand_dims(img_array, axis=-1)
|
| 36 |
+
|
| 37 |
return img_array
|
| 38 |
|
| 39 |
# 3. Prediction Function
|
|
|
|
| 46 |
|
| 47 |
# Get top prediction
|
| 48 |
predicted_class_index = np.argmax(predictions)
|
| 49 |
+
|
| 50 |
+
# JSON keys are strings, so cast index to str
|
| 51 |
predicted_label = labels[str(predicted_class_index)]
|
| 52 |
+
|
| 53 |
+
# Convert numpy float to python float for Gradio
|
| 54 |
confidence = float(np.max(predictions))
|
| 55 |
|
| 56 |
+
# Return dictionary for Gradio Label output
|
| 57 |
return {predicted_label: confidence}
|
| 58 |
|
| 59 |
# 4. Gradio Interface
|