UtkarshShivhare commited on
Commit
3d4c6d3
1 Parent(s): 511e4ba

Upload 3 files

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