Reaumur commited on
Commit
4f186ac
·
verified ·
1 Parent(s): a984e94

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +28 -17
app.py CHANGED
@@ -1,40 +1,48 @@
1
  import streamlit as st
2
  from PIL import Image
3
- from tensorflow.keras.models import load_model
4
  import tensorflow as tf
5
  import numpy as np
6
- from keras.preprocessing.image import img_to_array
7
  import os
8
 
 
9
  @st.cache_resource
10
- def load_model():
11
  model_path = "captcha_ocr_model.h5" # Update with the actual CAPTCHA model path
12
  return tf.keras.models.load_model(model_path)
13
 
14
- model = load_model()
 
15
 
 
16
  def prepare_captcha_image(img):
17
- # Resize image to the input shape required by the CAPTCHA model
18
- img = img.resize((200, 50)) # Adjust size according to the trained model
19
- img_array = np.array(img)
20
- img_array = img_array / 255.0 # Normalize image
21
- img_array = np.expand_dims(img_array, axis=0)
 
22
 
23
- # Predict the CAPTCHA characters
24
- predictions = model.predict(img_array)
25
 
26
- # Assuming the model outputs one-hot encoded characters, decode the predictions
27
- decoded_captcha = ''.join([chr(np.argmax(pred) + ord('A')) for pred in predictions])
 
28
 
29
- return decoded_captcha, predictions
30
 
 
 
 
 
 
31
  def run():
32
  st.title("CAPTCHA Prediction")
33
- img_file = st.file_uploader("Upload a CAPTCHA Image", type=["jpg", "png"])
34
 
35
  if img_file is not None:
36
  img = Image.open(img_file)
37
- st.image(img, use_column_width=False)
38
 
39
  # Create the directory if it doesn't exist
40
  upload_dir = './upload_images/'
@@ -47,6 +55,9 @@ def run():
47
 
48
  # Predict the CAPTCHA
49
  predicted_captcha, score = prepare_captcha_image(img)
50
- st.success(f"**Predicted CAPTCHA: {predicted_captcha}**")
 
 
 
51
 
52
  run()
 
1
  import streamlit as st
2
  from PIL import Image
 
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_captcha_model():
10
  model_path = "captcha_ocr_model.h5" # Update with the actual CAPTCHA model path
11
  return tf.keras.models.load_model(model_path)
12
 
13
+ # Load the model
14
+ model = load_captcha_model()
15
 
16
+ # Function to prepare the image for model prediction
17
  def prepare_captcha_image(img):
18
+ try:
19
+ # Resize image to the input shape required by the CAPTCHA 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=0) # Add batch dimension
24
 
25
+ # Predict the CAPTCHA characters
26
+ predictions = model.predict(img_array)
27
 
28
+ # Decode predictions assuming the model outputs probabilities
29
+ # Modify this part based on your specific model's output
30
+ decoded_captcha = ''.join([chr(np.argmax(pred) + ord('A')) for pred in predictions])
31
 
32
+ return decoded_captcha, predictions
33
 
34
+ except Exception as e:
35
+ st.error(f"Error preparing image: {e}")
36
+ return None, None
37
+
38
+ # Main function to run the Streamlit app
39
  def run():
40
  st.title("CAPTCHA Prediction")
41
+ img_file = st.file_uploader("Upload a CAPTCHA Image", type=["jpg", "png", "jpeg"])
42
 
43
  if img_file is not None:
44
  img = Image.open(img_file)
45
+ st.image(img, caption="Uploaded CAPTCHA", use_column_width=True)
46
 
47
  # Create the directory if it doesn't exist
48
  upload_dir = './upload_images/'
 
55
 
56
  # Predict the CAPTCHA
57
  predicted_captcha, score = prepare_captcha_image(img)
58
+ if predicted_captcha:
59
+ st.success(f"**Predicted CAPTCHA: {predicted_captcha}**")
60
+ else:
61
+ st.error("Failed to predict CAPTCHA.")
62
 
63
  run()