Reaumur commited on
Commit
c9ed9d2
·
verified ·
1 Parent(s): b0e4c58

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -15
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 load_captcha_model():
17
- model_path = "model1.keras" # Update with the actual CAPTCHA model path
18
- return tf.keras.models.load_model(model_path, custom_objects={'CustomLSTM': CustomLSTM})
19
 
20
  # Load the model
21
- model = load_captcha_model()
22
 
23
  # Function to prepare the image for model prediction
24
- def prepare_captcha_image(img):
25
  try:
26
- # Resize image to the input shape required by the CAPTCHA model
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 CAPTCHA characters
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 = prepare_captcha_image(img)
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: