Update app.py
Browse files
app.py
CHANGED
@@ -1,46 +1,50 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
from PIL import Image
|
3 |
-
|
4 |
-
import
|
5 |
-
import
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
img_array =
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
|
|
|
|
|
|
|
|
|
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 |
+
@st.cache_resource
|
8 |
+
def load_captcha_model():
|
9 |
+
model_path = "captcha_model.keras" # Update with the actual CAPTCHA model path
|
10 |
+
return tf.keras.models.load_model(model_path)
|
11 |
+
|
12 |
+
model = load_captcha_model()
|
13 |
+
|
14 |
+
def prepare_captcha_image(img):
|
15 |
+
# Resize image to the input shape required by the CAPTCHA model
|
16 |
+
img = img.resize((200, 50)) # Adjust size according to the trained model
|
17 |
+
img_array = np.array(img)
|
18 |
+
img_array = img_array / 255.0 # Normalize image
|
19 |
+
img_array = np.expand_dims(img_array, axis=0)
|
20 |
+
|
21 |
+
# Predict the CAPTCHA characters
|
22 |
+
predictions = model.predict(img_array)
|
23 |
+
|
24 |
+
# Assuming the model outputs one-hot encoded characters, decode the predictions
|
25 |
+
decoded_captcha = ''.join([chr(np.argmax(pred) + ord('A')) for pred in predictions])
|
26 |
+
|
27 |
+
return decoded_captcha, predictions
|
28 |
+
|
29 |
+
def run():
|
30 |
+
st.title("CAPTCHA Prediction")
|
31 |
+
img_file = st.file_uploader("Upload a CAPTCHA Image", type=["jpg", "png"])
|
32 |
+
|
33 |
+
if img_file is not None:
|
34 |
+
img = Image.open(img_file)
|
35 |
+
st.image(img, use_column_width=False)
|
36 |
+
|
37 |
+
# Create the directory if it doesn't exist
|
38 |
+
upload_dir = './upload_images/'
|
39 |
+
os.makedirs(upload_dir, exist_ok=True)
|
40 |
+
|
41 |
+
# Save the uploaded image
|
42 |
+
save_image_path = os.path.join(upload_dir, img_file.name)
|
43 |
+
with open(save_image_path, "wb") as f:
|
44 |
+
f.write(img_file.getbuffer())
|
45 |
+
|
46 |
+
# Predict the CAPTCHA
|
47 |
+
predicted_captcha, score = prepare_captcha_image(img)
|
48 |
+
st.success(f"**Predicted CAPTCHA: {predicted_captcha}**")
|
49 |
+
|
50 |
+
run()
|