UtkarshShivhare commited on
Commit
3d502b7
1 Parent(s): babbc41

Upload 2 files

Browse files
Files changed (2) hide show
  1. Inage Captioning.ipynb +0 -0
  2. app.py +85 -0
Inage Captioning.ipynb ADDED
The diff for this file is too large to render. See raw diff
 
app.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # Load the .h5 model
15
+ model = load_model('image_caption.h5')
16
+ tokenizer = Tokenizer()
17
+ max_length=35
18
+ # Load pre-trained model
19
+ vgg_model = VGG16()
20
+ vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-2].output)
21
+
22
+ # Set Streamlit configurations
23
+ st.set_page_config(page_title="Image Classifier App", layout="wide")
24
+
25
+
26
+ # Function to preprocess the input image
27
+ def preprocess_image(image):
28
+ image = load_img(image, target_size=(224, 224))
29
+ image = img_to_array(image)
30
+ image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
31
+ image = preprocess_input(image)
32
+ return image
33
+
34
+ # Function to make predictions on the input image
35
+ def predict(image):
36
+ image = preprocess_image(image)
37
+ feature = vgg_model.predict(image, verbose=0)
38
+ preds = predict_caption(model, feature, tokenizer, max_length)
39
+ preds=preds[8:-7]
40
+ return preds
41
+
42
+ def idx_word(integer,tok):
43
+ for word,index in tok.word_index.items():
44
+ if index== integer:
45
+ return word
46
+ return None
47
+
48
+ def predict_caption(model,image,tok,max_len):
49
+ in_text="startseq"
50
+ for i in range(max_len):
51
+ seq=tok.texts_to_sequences([in_text])[0]
52
+ seq=pad_sequences([seq],max_len)
53
+ yhat = model.predict([image, seq], verbose=0)
54
+ yhat = np.argmax(yhat)
55
+ word = idx_word(yhat, tok)
56
+ if word is None:
57
+ break
58
+ in_text += " " + word
59
+ if word == 'endseq':
60
+ break
61
+ return in_text
62
+
63
+ # Streamlit app
64
+ def main():
65
+ st.title("Image Classifier App")
66
+ st.write("Upload an image and the app will predict its class.")
67
+
68
+ uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
69
+
70
+ if uploaded_image is not None:
71
+ image = Image.open(uploaded_image)
72
+ st.image(image, caption='Uploaded Image', use_column_width=True)
73
+ st.write("")
74
+
75
+ if st.button("Predict"):
76
+ with st.spinner("Predicting..."):
77
+ predictions = predict(image)
78
+
79
+ st.write("Top predictions:")
80
+ for _, label, confidence in predictions:
81
+ st.write(f"{label}: {round(confidence * 100, 2)}%")
82
+
83
+ # Run the app
84
+ if __name__ == "__main__":
85
+ main()