Update app.py
Browse files
app.py
CHANGED
@@ -3,33 +3,27 @@ from PIL import Image
|
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
5 |
import os
|
6 |
-
from tensorflow.keras.layers import LSTM
|
7 |
-
from keras.saving import register_keras_serializable
|
8 |
-
|
9 |
-
# Kelas LSTM Kustom
|
10 |
-
@register_keras_serializable()
|
11 |
-
class CustomLSTM(LSTM):
|
12 |
-
pass
|
13 |
|
14 |
# Caching the model loading function to optimize performance
|
15 |
@st.cache_resource
|
16 |
-
def
|
17 |
-
model_path = "
|
18 |
-
return tf.keras.models.load_model(model_path
|
19 |
|
20 |
# Load the model
|
21 |
-
model =
|
22 |
|
23 |
# Function to prepare the image for model prediction
|
24 |
-
def
|
25 |
try:
|
26 |
-
# Resize image to the input shape required by the
|
27 |
img = img.resize((200, 50)) # Adjust size according to the trained model
|
28 |
img_array = np.array(img.convert('L')) # Convert to grayscale if necessary
|
29 |
img_array = img_array / 255.0 # Normalize image
|
|
|
30 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
31 |
|
32 |
-
# Predict the
|
33 |
predictions = model.predict(img_array)
|
34 |
|
35 |
# Decode predictions assuming the model outputs probabilities
|
@@ -60,7 +54,7 @@ def run():
|
|
60 |
f.write(img_file.getbuffer())
|
61 |
|
62 |
# Predict the CAPTCHA
|
63 |
-
predicted_captcha, score =
|
64 |
if predicted_captcha:
|
65 |
st.success(f"**Predicted CAPTCHA: {predicted_captcha}**")
|
66 |
else:
|
|
|
3 |
import tensorflow as tf
|
4 |
import numpy as np
|
5 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
# Caching the model loading function to optimize performance
|
8 |
@st.cache_resource
|
9 |
+
def load_model():
|
10 |
+
model_path = "captcha.keras" # Update with the actual model path
|
11 |
+
return tf.keras.models.load_model(model_path)
|
12 |
|
13 |
# Load the model
|
14 |
+
model = load_model()
|
15 |
|
16 |
# Function to prepare the image for model prediction
|
17 |
+
def prepare_image(img):
|
18 |
try:
|
19 |
+
# Resize image to the input shape required by the model
|
20 |
img = img.resize((200, 50)) # Adjust size according to the trained model
|
21 |
img_array = np.array(img.convert('L')) # Convert to grayscale if necessary
|
22 |
img_array = img_array / 255.0 # Normalize image
|
23 |
+
img_array = np.expand_dims(img_array, axis=-1) # Add channel dimension
|
24 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
25 |
|
26 |
+
# Predict the output using the loaded model
|
27 |
predictions = model.predict(img_array)
|
28 |
|
29 |
# Decode predictions assuming the model outputs probabilities
|
|
|
54 |
f.write(img_file.getbuffer())
|
55 |
|
56 |
# Predict the CAPTCHA
|
57 |
+
predicted_captcha, score = prepare_image(img)
|
58 |
if predicted_captcha:
|
59 |
st.success(f"**Predicted CAPTCHA: {predicted_captcha}**")
|
60 |
else:
|