stshanks commited on
Commit
2a5ea2c
·
verified ·
1 Parent(s): 8c37f28

Update app.py

Browse files

Modify predict_text() to Split the Image

Files changed (1) hide show
  1. app.py +18 -8
app.py CHANGED
@@ -19,14 +19,24 @@ def preprocess_image(image):
19
 
20
  # Function to predict text from handwritten prescription
21
  def predict_text(image):
22
- processed_image = preprocess_image(image)
23
- prediction = model.predict(processed_image)
24
-
25
- print("Model output shape:", prediction.shape) # Should be (1, sequence_length, num_classes) ideally
26
- print("Model output values:", prediction) # Print raw output for analysis
27
-
28
- predicted_text = decode_prediction(prediction)
29
- return predicted_text
 
 
 
 
 
 
 
 
 
 
30
 
31
 
32
  import numpy as np
 
19
 
20
  # Function to predict text from handwritten prescription
21
  def predict_text(image):
22
+ image = image.convert("RGB")
23
+ image = image.resize((128, 64)) # Resize for better character separation
24
+ image = np.array(image) / 255.0 # Normalize
25
+
26
+ num_chars = 5 # Estimate word length
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
+ char_segment = np.expand_dims(char_segment, axis=0) # Add batch dimension
33
+
34
+ # Predict character for each segment
35
+ prediction = model.predict(char_segment)
36
+ char = decode_prediction(prediction)
37
+ predicted_text.append(char)
38
+
39
+ return "".join(predicted_text)
40
 
41
 
42
  import numpy as np