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

Update app.py

Browse files

Updated predict_text() to Resize Segments

Files changed (1) hide show
  1. app.py +7 -2
app.py CHANGED
@@ -20,15 +20,20 @@ def preprocess_image(image):
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
 
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 segmentation
24
  image = np.array(image) / 255.0 # Normalize
25
 
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