Spaces:
Runtime error
Runtime error
UtkarshShivhare
commited on
Commit
•
9b5c5c0
1
Parent(s):
b3c024c
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 Captioning App", layout="wide")
|
29 |
+
|
30 |
+
|
31 |
+
# Function to preprocess the input image
|
32 |
+
def preprocess_image(image):
|
33 |
+
image = image.convert("RGB")
|
34 |
+
image = image.resize((224, 224))
|
35 |
+
image = img_to_array(image)
|
36 |
+
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
|
37 |
+
image = preprocess_input(image)
|
38 |
+
return image
|
39 |
+
|
40 |
+
# Function to make predictions on the input image
|
41 |
+
def predict(image):
|
42 |
+
image = preprocess_image(image)
|
43 |
+
feature = vgg_model.predict(image, verbose=0)
|
44 |
+
preds = predict_caption(model, feature, tokenizer, max_length)
|
45 |
+
preds=preds[8:-7]
|
46 |
+
return preds
|
47 |
+
|
48 |
+
def idx_word(integer,tok):
|
49 |
+
for word,index in tok.word_index.items():
|
50 |
+
if index== integer:
|
51 |
+
return word
|
52 |
+
return None
|
53 |
+
|
54 |
+
def predict_caption(model,image,tok,max_len):
|
55 |
+
in_text="startseq"
|
56 |
+
for i in range(max_len):
|
57 |
+
seq=tok.texts_to_sequences([in_text])[0]
|
58 |
+
seq=pad_sequences([seq],max_len)
|
59 |
+
yhat = model.predict([image, seq], verbose=0)
|
60 |
+
yhat = np.argmax(yhat)
|
61 |
+
word = idx_word(yhat, tok)
|
62 |
+
if word is None:
|
63 |
+
break
|
64 |
+
in_text += " " + word
|
65 |
+
if word == 'endseq':
|
66 |
+
break
|
67 |
+
return in_text
|
68 |
+
|
69 |
+
# Streamlit app
|
70 |
+
def main():
|
71 |
+
st.title("Image Captioning App")
|
72 |
+
st.write("Upload an image and the app will predict its class.")
|
73 |
+
|
74 |
+
uploaded_image = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
|
75 |
+
|
76 |
+
if uploaded_image is not None:
|
77 |
+
image = Image.open(uploaded_image)
|
78 |
+
st.image(image, caption='Uploaded Image', use_column_width=True)
|
79 |
+
st.write("")
|
80 |
+
|
81 |
+
if st.button("Generate Caption"):
|
82 |
+
with st.spinner("Generating..."):
|
83 |
+
predictions = predict(image)
|
84 |
+
|
85 |
+
st.write(f"Top Caption:{predictions}")
|
86 |
+
|
87 |
+
# Run the app
|
88 |
+
if __name__ == "__main__":
|
89 |
+
main()
|