Reaumur commited on
Commit
659b71a
·
verified ·
1 Parent(s): fde13b5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +50 -46
app.py CHANGED
@@ -1,46 +1,50 @@
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
-
10
- @st.cache_resource
11
- def load_model():
12
- model_path = "model.keras" # Update with the absolute file path
13
- return tf.keras.models.load_model(model_path)
14
-
15
- model = load_model()
16
-
17
- def prepare_image(img):
18
- img = img.resize((220, 220))
19
- img_array = img_to_array(img)
20
- img_array = np.expand_dims(img_array, axis=0)
21
-
22
- prediction = model.predict(img_array)
23
- predicted_class = "Smoking" if prediction > 0.5 else "Not Smoking"
24
-
25
- return predicted_class, prediction[0]
26
-
27
- def run():
28
- st.title("Smoking or Not Smoking Detection")
29
- img_file = st.file_uploader("Choose an Image", type=["jpg", "png"])
30
-
31
- if img_file is not None:
32
- img = Image.open(img_file).resize((250, 250))
33
- st.image(img, use_column_width=False)
34
-
35
- # Create the directory if it doesn't exist
36
- upload_dir = './upload_images/'
37
- os.makedirs(upload_dir, exist_ok=True)
38
-
39
- save_image_path = os.path.join(upload_dir, img_file.name)
40
- with open(save_image_path, "wb") as f:
41
- f.write(img_file.getbuffer())
42
-
43
- predicted_class, score = prepare_image(img)
44
- st.success(f"**Predicted : {predicted_class}, Score: {score}**")
45
-
46
- 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
+ @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()