Spaces:
Sleeping
Sleeping
Upload 9 files
Browse files- .gitattributes +2 -0
- 1015584366.jpg +0 -0
- 1024138940_f1fefbdce1.jpg +0 -0
- 1028205764_7e8df9a2ea.jpg +0 -0
- 108899015_bf36131a57.jpg +0 -0
- app.py +65 -0
- best_model_inceptionv3.keras +3 -0
- captions.txt +0 -0
- feature_model.keras +3 -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
|
1015584366.jpg
ADDED
1024138940_f1fefbdce1.jpg
ADDED
1028205764_7e8df9a2ea.jpg
ADDED
108899015_bf36131a57.jpg
ADDED
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
|
43 |
+
return in_text
|
44 |
+
|
45 |
+
def generate_caption(image):
|
46 |
+
print(image)
|
47 |
+
# image = tf.keras.preprocessing.image.load_img(image_path, target_size=(299, 299))
|
48 |
+
# image = tf.keras.preprocessing.image.img_to_array(image)
|
49 |
+
# image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
|
50 |
+
image = image.resize((299, 299))
|
51 |
+
image_array = tf.keras.preprocessing.image.img_to_array(image)
|
52 |
+
image_array = image_array.reshape((1, 299, 299, 3))
|
53 |
+
image = tf.keras.applications.inception_v3.preprocess_input(image_array)
|
54 |
+
feature = feature_model.predict(image, verbose=0)
|
55 |
+
caption = predict_caption(feature)
|
56 |
+
return caption
|
57 |
+
|
58 |
+
gr.Interface(fn=generate_caption,
|
59 |
+
inputs=gr.Image(label='Upload a photo',type="pil"),
|
60 |
+
outputs=gr.Label(label='Predicted Car'),
|
61 |
+
examples=['1001773457.jpg','1014785440.jpg'],
|
62 |
+
title='Image Caption Generator',
|
63 |
+
theme='dark'
|
64 |
+
).launch(share=True)
|
65 |
+
|
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
|
|
feature_model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c90d24d975e6f20e0bcbce2c5cc19de645539670431eea8e5ff87b44452240ec
|
3 |
+
size 88217949
|
tokenizer.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2dabdab5afb1687437a8f7c271825dba6ea4f35129f29c81817f38bad91e7da2
|
3 |
+
size 334854
|