AnJ47/CNN_Caption_generator
0
1import streamlit as st2from tensorflow import keras3from keras.models import load_model4from keras.preprocessing.text import Tokenizer5from keras.utils import pad_sequences6from keras.applications.vgg16 import VGG16, preprocess_input7from keras.models import Model8from keras.layers import Input, Dense, LSTM ,add,Dropout, Embedding 9import numpy as np10import cv211from PIL import Image, ImageOps12 13 14# Obtained earlier 15# MAX_LENGTH=3516 17def idx_to_word(integer,tokenizer):18 for word, index in tokenizer.word_index.items():19 if index==integer:20 return word21 return None22 23def read_list(file_path):24 lst = []25 with open(file_path, 'r') as file:26 for line in file:27 lst.append(line.strip())28 return lst29 30def preprocess_image(img):31 size = (224,224) 32 image = ImageOps.fit(img, size, Image.ANTIALIAS)33 image = np.asarray(image)34 img = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)35 img_reshape = img[np.newaxis,...]36 image = preprocess_input(img_reshape)37 return image38 39def predict_caption(model,feature,tokenizer):40 max_length =3541 # add start tag for generation process 42 in_text='startseq'43 #iterate over the max length of sequence44 for i in range(max_length):45 # Encode input sequence46 sequence=tokenizer.texts_to_sequences([in_text])[0]47 # Pad the sequence48 sequence= pad_sequences([sequence],max_length)49 # Predict the next word50 # image=image.reshape((1,4096))51 yhat = model.predict([feature,sequence],verbose=0) 52 # Get the index with the highest probability53 yhat=np.argmax(yhat)54 # Convert the index to word55 word = idx_to_word(yhat,tokenizer)56 if word is None:57 break58 # Append word as input59 in_text+= " "+word60 # Stop if we reach end tag61 if word=='endseq':62 break63 return in_text64 65def generate_clean_caption(in_text):66 # Split the string into a list of words67 words = in_text.split()68 # Join all words except the first and last69 result = ' '.join(words[1:-1]) 70 return result71 72 73 74## Layout ##75rad = st.sidebar.radio("Navigation",["Home","Caption Generator"])76 77 78if rad=="Home":79 st.title("CNN Caption Generator")80 st.subheader("By Anuraj Bhaskar")81 st.markdown("""Upload an Image in the Caption Generator page to generate Caption""")82 83 # st.image("sample.jpg")84 85elif rad=="Caption Generator":86 st.write("""87 # Caption Generation88 """)89 90 vocab_size =848591 vgg = VGG16()92 # restructure the model93 vgg_model = Model(inputs=vgg.inputs, outputs=vgg.layers[-2].output)94 95 with st.spinner("Loading Model..."):96 model = load_model("model.h5") 97 98 file = st.file_uploader("Upload an Image")99 st.write(file)100 st.set_option('deprecation.showfileUploaderEncoding', False)101 102 if file != None:103 image = Image.open(file)104 st.image(image,caption="Uploaded Image",use_column_width=True)105 # Prepocessing the Image106 image=preprocess_image(image)107 feature =vgg_model.predict(image,verbose=0)108 109 all_captions = read_list('all_captions.txt')110 #Tokenize the text111 tokenizer=Tokenizer()112 tokenizer.fit_on_texts(all_captions)113 114 115 116 # feature = vgg_model.predict(image,verbose=0)117 118 in_text=predict_caption(model,feature,tokenizer) 119 120 # Removing Startseq and Endseq121 caption= generate_clean_caption(in_text)122 123 # # Display the Caption124 st.markdown('<b>Generated Caption:</b>',True)125 st.markdown(caption.upper())126 else:127 st.write("No file uploaded")128 