Spaces:
Sleeping
Sleeping
uploaded remaining files
Browse files- .gitattributes +2 -0
- 1001773457.jpg +0 -0
- 1015584366.jpg +0 -0
- 1024138940_f1fefbdce1.jpg +0 -0
- 1028205764_7e8df9a2ea.jpg +0 -0
- 108899015_bf36131a57.jpg +0 -0
- 123.jpg +0 -0
- app.py +66 -0
- best_model_inceptionv3.keras +3 -0
- captions.txt +0 -0
- captions.txt.zip +3 -0
- feature_model.keras +3 -0
- requirements.txt +5 -0
- tokenizer.pkl +3 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
best_model_inceptionv3.keras filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
feature_model.keras filter=lfs diff=lfs merge=lfs -text
|
1001773457.jpg
ADDED
|
1015584366.jpg
ADDED
|
1024138940_f1fefbdce1.jpg
ADDED
|
1028205764_7e8df9a2ea.jpg
ADDED
|
108899015_bf36131a57.jpg
ADDED
|
123.jpg
ADDED
|
app.py
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import tensorflow as tf
|
| 5 |
+
import pickle
|
| 6 |
+
from PIL import Image
|
| 7 |
+
|
| 8 |
+
with open('C:/Users/shlok/OneDrive/Desktop/Projects/Image-Caption-Generator/tokenizer.pkl', 'rb') as handle:
|
| 9 |
+
tokenizer = pickle.load(handle)
|
| 10 |
+
feature_model=tf.keras.models.load_model('feature_model.keras')
|
| 11 |
+
model = tf.keras.models.load_model('best_model_inceptionv3.keras')
|
| 12 |
+
def idx_to_word(integer, tokenizer):
|
| 13 |
+
for word, index in tokenizer.word_index.items():
|
| 14 |
+
if index == integer:
|
| 15 |
+
return word
|
| 16 |
+
return None
|
| 17 |
+
|
| 18 |
+
def predict_caption(image):
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
in_text = 'startseq'
|
| 22 |
+
# iterate over the max length of sequence
|
| 23 |
+
for i in range(35):
|
| 24 |
+
# encode input sequence
|
| 25 |
+
sequence = tokenizer.texts_to_sequences([in_text])[0]
|
| 26 |
+
# pad the sequence
|
| 27 |
+
sequence = tf.keras.preprocessing.sequence.pad_sequences([sequence], 35, padding='post')
|
| 28 |
+
# predict next word
|
| 29 |
+
yhat = model.predict([image, sequence], verbose=0)
|
| 30 |
+
# get index with high probability
|
| 31 |
+
yhat = np.argmax(yhat)
|
| 32 |
+
# convert index to word
|
| 33 |
+
word = idx_to_word(yhat, tokenizer)
|
| 34 |
+
# stop if word not found
|
| 35 |
+
if word is None:
|
| 36 |
+
break
|
| 37 |
+
# append word as input for generating next word
|
| 38 |
+
in_text += " " + word
|
| 39 |
+
# stop if we reach end tag
|
| 40 |
+
if word == 'endseq':
|
| 41 |
+
break
|
| 42 |
+
# Split the generated sequence to exclude the first and last words
|
| 43 |
+
final_caption = in_text.split()[1:-1]
|
| 44 |
+
# Join the words to form the final caption
|
| 45 |
+
final_caption = ' '.join(final_caption)
|
| 46 |
+
|
| 47 |
+
return final_caption
|
| 48 |
+
|
| 49 |
+
def generate_caption(image):
|
| 50 |
+
print(image)
|
| 51 |
+
|
| 52 |
+
image = image.resize((299, 299))
|
| 53 |
+
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
| 54 |
+
image_array = image_array.reshape((1, 299, 299, 3))
|
| 55 |
+
image = tf.keras.applications.inception_v3.preprocess_input(image_array)
|
| 56 |
+
feature = feature_model.predict(image, verbose=0)
|
| 57 |
+
caption = predict_caption(feature)
|
| 58 |
+
return caption
|
| 59 |
+
|
| 60 |
+
gr.Interface(fn=generate_caption,
|
| 61 |
+
inputs=gr.Image(label='Upload a photo',type="pil"),
|
| 62 |
+
outputs=gr.Label(label='Caption'),
|
| 63 |
+
examples=['1028205764_7e8df9a2ea.jpg','123.jpg','1001773457.jpg','1024138940_f1fefbdce1.jpg'],
|
| 64 |
+
title='Image Caption Generator',
|
| 65 |
+
).launch(share=True)
|
| 66 |
+
|
best_model_inceptionv3.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1c2a83b6ce7d8e1e1e677cbd13ce7ee59f68ff2322fd143a471a499e8ee2c1a8
|
| 3 |
+
size 59280932
|
captions.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
captions.txt.zip
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:dd405c85c04317175a79d282128626045da72dc2a3cc7af75fdf6b6feb03929f
|
| 3 |
+
size 850962
|
feature_model.keras
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:c90d24d975e6f20e0bcbce2c5cc19de645539670431eea8e5ff87b44452240ec
|
| 3 |
+
size 88217949
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
numpy
|
| 3 |
+
pandas
|
| 4 |
+
tensorflow
|
| 5 |
+
Pillow
|
tokenizer.pkl
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:2dabdab5afb1687437a8f7c271825dba6ea4f35129f29c81817f38bad91e7da2
|
| 3 |
+
size 334854
|