davelop commited on
Commit
84305aa
·
verified ·
1 Parent(s): 6ad8aaa

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +66 -66
app.py CHANGED
@@ -1,66 +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
-
 
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('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
+