Update app.py
Browse filesModify predict_text() to Split the Image
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 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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
|