Update app.py
Browse filesReplaced character-level decoding with direct mapping from model output index to drug names
app.py
CHANGED
|
@@ -9,6 +9,30 @@ from PIL import Image
|
|
| 9 |
# Load the trained model
|
| 10 |
model = tf.keras.models.load_model("prescription_classification_model.keras")
|
| 11 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 12 |
# Function to preprocess the uploaded image
|
| 13 |
def preprocess_image(image):
|
| 14 |
image = image.convert("RGB") # Convert to grayscale
|
|
@@ -26,22 +50,16 @@ def predict_text(image):
|
|
| 26 |
num_chars = 5 # Estimated number of characters in the word
|
| 27 |
segment_width = image.shape[1] // num_chars # Split image into equal parts
|
| 28 |
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
# Predict character for each segment
|
| 40 |
-
prediction = model.predict(char_segment)
|
| 41 |
-
char = decode_prediction(prediction)
|
| 42 |
-
predicted_text.append(char)
|
| 43 |
-
|
| 44 |
-
return "".join(predicted_text)
|
| 45 |
|
| 46 |
|
| 47 |
import numpy as np
|
|
|
|
| 9 |
# Load the trained model
|
| 10 |
model = tf.keras.models.load_model("prescription_classification_model.keras")
|
| 11 |
|
| 12 |
+
# Define the 78 drug names in the exact order corresponding to your model's output classes
|
| 13 |
+
CLASS_NAMES = [
|
| 14 |
+
"Beklo", "Maxima", "Leptic", "Esoral", "Omastin", "Esonix", "Canazole", "Fixal",
|
| 15 |
+
"Progut", "Diflu", "Montair", "Flexilax", "Maxpro", "Vifas", "Conaz", "Fexofast",
|
| 16 |
+
"Fenadin", "Telfast", "Dinafex", "Ritch", "Renova", "Flugal", "Axodin", "Sergel",
|
| 17 |
+
"Nexum", "Opton", "Nexcap", "Fexo", "Montex", "Exium", "Lumona", "Napa", "Azithrocin",
|
| 18 |
+
"Atrizin", "Monas", "Nidazyl", "Metsina", "Baclon", "Rozith", "Bicozin", "Ace",
|
| 19 |
+
"Amodis", "Alatrol", "Napa Extend", "Rivotril", "Montene", "Filmet", "Aceta",
|
| 20 |
+
"Tamen", "Bacmax", "Disopan", "Rhinil", "Flamyd", "Metro", "Zithrin", "Candinil",
|
| 21 |
+
"Lucan-R", "Backtone", "Bacaid", "Etizin", "Az", "Romycin", "Azyth", "Cetisoft",
|
| 22 |
+
"Dancel", "Tridosil", "Nizoder", "Ketoral", "Ketocon", "Ketotab", "Ketozol",
|
| 23 |
+
"Denixil", "Provair", "Odmon", "Baclofen", "MKast", "Trilock", "Flexibac"
|
| 24 |
+
]
|
| 25 |
+
|
| 26 |
+
def decode_prediction(prediction):
|
| 27 |
+
"""
|
| 28 |
+
Expects prediction to be a numpy array of shape (1, 78).
|
| 29 |
+
It returns the drug name corresponding to the highest probability.
|
| 30 |
+
"""
|
| 31 |
+
# Get the index of the highest probability class
|
| 32 |
+
predicted_index = np.argmax(prediction, axis=-1)[0]
|
| 33 |
+
# Return the corresponding drug name
|
| 34 |
+
return CLASS_NAMES[predicted_index]
|
| 35 |
+
|
| 36 |
# Function to preprocess the uploaded image
|
| 37 |
def preprocess_image(image):
|
| 38 |
image = image.convert("RGB") # Convert to grayscale
|
|
|
|
| 50 |
num_chars = 5 # Estimated number of characters in the word
|
| 51 |
segment_width = image.shape[1] // num_chars # Split image into equal parts
|
| 52 |
|
| 53 |
+
def predict_text(image):
|
| 54 |
+
processed_image = preprocess_image(image) # Make sure the image is preprocessed to (64, 64, 3)
|
| 55 |
+
prediction = model.predict(processed_image)
|
| 56 |
+
|
| 57 |
+
print("Model output shape:", prediction.shape) # Should be (1, 78)
|
| 58 |
+
print("Model output values:", prediction) # Check the raw probabilities
|
| 59 |
+
|
| 60 |
+
# Decode the prediction to get the drug name
|
| 61 |
+
predicted_text = decode_prediction(prediction)
|
| 62 |
+
return predicted_text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
|
| 65 |
import numpy as np
|