UtkarshShivhare commited on
Commit
10d1cf9
1 Parent(s): aaf7696

Delete app.py

Browse files
Files changed (1) hide show
  1. app.py +0 -84
app.py DELETED
@@ -1,84 +0,0 @@
1
- import streamlit as st
2
- import tensorflow as tf
3
- from PIL import Image
4
- import numpy as np
5
- from tensorflow.keras.applications.vgg16 import VGG16,preprocess_input
6
- from tensorflow.keras.preprocessing.image import load_img,img_to_array
7
- from tensorflow.keras.preprocessing.text import Tokenizer
8
- from tensorflow.keras.preprocessing.sequence import pad_sequences
9
- from tensorflow.keras.models import Model
10
- from tensorflow.keras.utils import to_categorical,plot_model
11
- from tensorflow.keras.layers import Input,Dense,LSTM,Embedding, Dropout, add
12
- from keras.models import load_model
13
-
14
- model = load_model('image_caption.h5')
15
- tokenizer = Tokenizer()
16
- max_length=35
17
- # Load pre-trained model
18
- vgg_model = VGG16()
19
- vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-2].output)
20
-
21
- # Set Streamlit configurations
22
- st.set_page_config(page_title="Image Classifier App", layout="wide")
23
-
24
-
25
- # Function to preprocess the input image
26
- def preprocess_image(image):
27
- image = load_img(image, target_size=(224, 224))
28
- image = img_to_array(image)
29
- image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
30
- image = preprocess_input(image)
31
- return image
32
-
33
- # Function to make predictions on the input image
34
- def predict(image):
35
- image = preprocess_image(image)
36
- feature = vgg_model.predict(image, verbose=0)
37
- preds = predict_caption(model, feature, tokenizer, max_length)
38
- preds=preds[8:-7]
39
- return preds
40
-
41
- def idx_word(integer,tok):
42
- for word,index in tok.word_index.items():
43
- if index== integer:
44
- return word
45
- return None
46
-
47
- def predict_caption(model,image,tok,max_len):
48
- in_text="startseq"
49
- for i in range(max_len):
50
- seq=tok.texts_to_sequences([in_text])[0]
51
- seq=pad_sequences([seq],max_len)
52
- yhat = model.predict([image, seq], verbose=0)
53
- yhat = np.argmax(yhat)
54
- word = idx_word(yhat, tok)
55
- if word is None:
56
- break
57
- in_text += " " + word
58
- if word == 'endseq':
59
- break
60
- return in_text
61
-
62
- # Streamlit app
63
- def main():
64
- st.title("Image Classifier App")
65
- st.write("Upload an image and the app will predict its class.")
66
-
67
- uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
68
-
69
- if uploaded_image is not None:
70
- image = Image.open(uploaded_image)
71
- st.image(image, caption='Uploaded Image', use_column_width=True)
72
- st.write("")
73
-
74
- if st.button("Predict"):
75
- with st.spinner("Predicting..."):
76
- predictions = predict(image)
77
-
78
- st.write("Top predictions:")
79
- for _, label, confidence in predictions:
80
- st.write(f"{label}: {round(confidence * 100, 2)}%")
81
-
82
- # Run the app
83
- if __name__ == "__main__":
84
- main()