stshanks commited on
Commit
d305c5e
·
verified ·
1 Parent(s): a2dba21

Update app.py

Browse files

Replaced character-level decoding with direct mapping from model output index to drug names

Files changed (1) hide show
  1. app.py +34 -16
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
- predicted_text = []
30
- for i in range(num_chars):
31
- char_segment = image[:, i * segment_width:(i + 1) * segment_width, :]
32
-
33
- # Resize each character segment to (64, 64, 3) to match model input
34
- char_segment = Image.fromarray((char_segment * 255).astype(np.uint8)) # Convert back to PIL
35
- char_segment = char_segment.resize((64, 64)) # Resize to match model input
36
- char_segment = np.array(char_segment) / 255.0 # Normalize again
37
- char_segment = np.expand_dims(char_segment, axis=0) # Add batch dimension
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