Lokesh1024 commited on
Commit
9aec085
·
verified ·
1 Parent(s): 474bdab

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +58 -0
app.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import tensorflow as tf
4
+ import numpy as np
5
+
6
+ # Load the pre-trained model
7
+ caption_model = get_caption_model()
8
+
9
+ # Load the index lookup dictionary
10
+ with open('index_lookup.pkl', 'rb') as f:
11
+ index_lookup = pickle.load(f)
12
+
13
+ # Set the maximum decoded sentence length
14
+ max_decoded_sentence_length = 40
15
+
16
+ def generate_caption(img):
17
+ # Preprocess the image
18
+ img = tf.expand_dims(img, 0)
19
+ img_embed = caption_model.cnn_model(img)
20
+
21
+ # Pass the image features to the Transformer encoder
22
+ encoded_img = caption_model.encoder(img_embed, training=False)
23
+
24
+ # Generate the caption using the Transformer decoder
25
+ decoded_caption = "<start> "
26
+ for i in range(max_decoded_sentence_length):
27
+ tokenized_caption = vectorization([decoded_caption])[:, :-1]
28
+ mask = tf.math.not_equal(tokenized_caption, 0)
29
+ predictions = caption_model.decoder(
30
+ tokenized_caption, encoded_img, training=False, mask=mask
31
+ )
32
+ sampled_token_index = np.argmax(predictions[0, i, :])
33
+ sampled_token = index_lookup[sampled_token_index]
34
+ if sampled_token == "<end>":
35
+ break
36
+ decoded_caption += " " + sampled_token
37
+
38
+ decoded_caption = decoded_caption.replace("<start> ", "")
39
+ decoded_caption = decoded_caption.replace(" <end>", "").strip()
40
+ return decoded_caption
41
+
42
+ st.title("Image Captioning")
43
+
44
+ # Upload an image
45
+ uploaded_file = st.file_uploader("Choose an image", type=["jpg", "jpeg", "png"])
46
+
47
+ if uploaded_file is not None:
48
+ # Display the uploaded image
49
+ image = Image.open(uploaded_file)
50
+ st.image(image, caption='Uploaded Image', use_column_width=True)
51
+
52
+ # Generate the caption
53
+ img = tf.keras.preprocessing.image.img_to_array(image)
54
+ img = tf.image.resize(img, (299, 299))
55
+ caption = generate_caption(img)
56
+
57
+ # Display the generated caption
58
+ st.write("Generated Caption:", caption)