ashish-001 commited on
Commit
7f6f099
·
1 Parent(s): 13c4b7d

Upload 5 files

Browse files
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import tensorflow as tf
2
+ from tensorflow.keras.utils import pad_sequences
3
+ import pickle
4
+ import streamlit as st
5
+ import numpy as np
6
+ from PIL import Image
7
+ from tensorflow.keras.applications.xception import preprocess_input
8
+ import logging
9
+ tf.get_logger().setLevel(logging.ERROR)
10
+
11
+ model = tf.keras.models.load_model('image_captioning_model.h5')
12
+ feature_extractor = tf.keras.models.load_model(r"feature_extractor.h5")
13
+ with open(r'tokenizer_data.pkl', 'rb') as f:
14
+ pickle_data = pickle.load(f)
15
+ tokenizer = pickle_data['tokenizer']
16
+ idx_to_word = pickle_data['word_mapping']
17
+ max_length = 35
18
+
19
+
20
+ def generate_caption(img):
21
+ in_text = 'startseq'
22
+ new_img = Image.open(img)
23
+ image = np.asarray(new_img.resize((299, 299)))
24
+ image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
25
+ image = preprocess_input(image)
26
+ feature = feature_extractor.predict(image, verbose=0)
27
+ for i in range(max_length):
28
+ sequence = tokenizer.texts_to_sequences([in_text])[0]
29
+ sequence = pad_sequences([sequence], max_length)
30
+ yhat = model.predict([feature, sequence], verbose=0)
31
+ yhat = np.argmax(yhat)
32
+ if yhat not in idx_to_word.keys():
33
+ break
34
+ else:
35
+ word = idx_to_word[yhat]
36
+ if word == 'endseq':
37
+ break
38
+ in_text += " " + word
39
+
40
+ return in_text.replace("startseq", '')
41
+
42
+
43
+ st.title("Image Caption Generator")
44
+ img = st.file_uploader("Upload image", type=["png", "jpg", "jpeg"],)
45
+ if img is not None:
46
+ st.image(Image.open(img), width=300)
47
+
48
+ if st.button("Generate Caption"):
49
+ if img is not None:
50
+ st.write(generate_caption(img))
51
+ else:
52
+ st.write("Please upload an image")
feature_extractor.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:c6722667a1650c529d7399e825e13f2ed1fde48877a3b67509d75851c968cfd6
3
+ size 83863704
image_captioning_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:1d3886a34db43f251b5b19bb685a1b9e7ff5c0ba735c74ef7be12081d4ca16ef
3
+ size 65678548
requirements.txt ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ tensorflow
2
+ streamlit
tokenizer_data.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:46ed22b5947e48c1fc401ff55a336302389d90d173f9402201bcbafd5471f56c
3
+ size 401787