Update app.py
Browse files
app.py
CHANGED
|
@@ -3,13 +3,34 @@ from PIL import Image
|
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
# Caching the model loading function to optimize performance
|
| 9 |
@st.cache_resource
|
| 10 |
def load_model():
|
| 11 |
model_path = "captcha.keras" # Update with the actual model path
|
| 12 |
-
return tf.keras.models.load_model(model_path)
|
| 13 |
|
| 14 |
# Load the model
|
| 15 |
model = load_model()
|
|
@@ -45,15 +66,6 @@ def run():
|
|
| 45 |
img = Image.open(img_file)
|
| 46 |
st.image(img, caption="Uploaded CAPTCHA", use_column_width=True)
|
| 47 |
|
| 48 |
-
# Create the directory if it doesn't exist
|
| 49 |
-
upload_dir = './upload_images/'
|
| 50 |
-
os.makedirs(upload_dir, exist_ok=True)
|
| 51 |
-
|
| 52 |
-
# Save the uploaded image
|
| 53 |
-
save_image_path = os.path.join(upload_dir, img_file.name)
|
| 54 |
-
with open(save_image_path, "wb") as f:
|
| 55 |
-
f.write(img_file.getbuffer())
|
| 56 |
-
|
| 57 |
# Predict the CAPTCHA
|
| 58 |
predicted_captcha, score = prepare_image(img)
|
| 59 |
if predicted_captcha:
|
|
|
|
| 3 |
import tensorflow as tf
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
+
|
| 7 |
+
# Definisi CustomLayer
|
| 8 |
+
class CustomLayer(tf.keras.layers.Layer):
|
| 9 |
+
def __init__(self, units=32, activation=None):
|
| 10 |
+
super(CustomLayer, self).__init__()
|
| 11 |
+
self.units = units
|
| 12 |
+
self.activation = activation
|
| 13 |
+
self.dense = tf.keras.layers.Dense(units)
|
| 14 |
+
|
| 15 |
+
def call(self, inputs):
|
| 16 |
+
x = self.dense(inputs)
|
| 17 |
+
if self.activation:
|
| 18 |
+
x = self.activation(x)
|
| 19 |
+
return x
|
| 20 |
+
|
| 21 |
+
def get_config(self):
|
| 22 |
+
config = super(CustomLayer, self).get_config()
|
| 23 |
+
config.update({
|
| 24 |
+
'units': self.units,
|
| 25 |
+
'activation': self.activation,
|
| 26 |
+
})
|
| 27 |
+
return config
|
| 28 |
|
| 29 |
# Caching the model loading function to optimize performance
|
| 30 |
@st.cache_resource
|
| 31 |
def load_model():
|
| 32 |
model_path = "captcha.keras" # Update with the actual model path
|
| 33 |
+
return tf.keras.models.load_model(model_path, custom_objects={'CustomLayer': CustomLayer})
|
| 34 |
|
| 35 |
# Load the model
|
| 36 |
model = load_model()
|
|
|
|
| 66 |
img = Image.open(img_file)
|
| 67 |
st.image(img, caption="Uploaded CAPTCHA", use_column_width=True)
|
| 68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
# Predict the CAPTCHA
|
| 70 |
predicted_captcha, score = prepare_image(img)
|
| 71 |
if predicted_captcha:
|