Spaces:
Runtime error
Runtime error
Commit
•
7d6771a
1
Parent(s):
e680abd
Update app.py
Browse files
app.py
CHANGED
@@ -2,33 +2,62 @@ import streamlit as st
|
|
2 |
import tensorflow as tf
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
|
|
|
|
|
|
6 |
# Load pre-trained model
|
7 |
-
|
|
|
8 |
|
9 |
# Set Streamlit configurations
|
10 |
st.set_page_config(page_title="Image Classifier App", layout="wide")
|
11 |
|
12 |
-
# Define labels for ImageNet classes
|
13 |
-
LABELS_PATH = 'https://raw.githubusercontent.com/anishathalye/imagenet-simple-labels/master/imagenet-simple-labels.json'
|
14 |
-
labels = tf.keras.utils.get_file('ImageNetLabels.json', LABELS_PATH)
|
15 |
-
with open(labels) as f:
|
16 |
-
classes = eval(f.read())
|
17 |
|
18 |
# Function to preprocess the input image
|
19 |
def preprocess_image(image):
|
20 |
-
image = image
|
21 |
-
image =
|
22 |
-
image =
|
23 |
-
image =
|
24 |
return image
|
25 |
|
26 |
# Function to make predictions on the input image
|
27 |
def predict(image):
|
28 |
image = preprocess_image(image)
|
29 |
-
|
30 |
-
|
31 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
32 |
|
33 |
# Streamlit app
|
34 |
def main():
|
|
|
2 |
import tensorflow as tf
|
3 |
from PIL import Image
|
4 |
import numpy as np
|
5 |
+
from tensorflow.keras.applications.vgg16 import VGG16,preprocess_input
|
6 |
+
from tensorflow.keras.preprocessing.image import load_img,img_to_array
|
7 |
+
from tensorflow.keras.preprocessing.text import Tokenizer
|
8 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
9 |
+
from tensorflow.keras.models import Model
|
10 |
+
from tensorflow.keras.utils import to_categorical,plot_model
|
11 |
+
from tensorflow.keras.layers import Input,Dense,LSTM,Embedding, Dropout, add
|
12 |
+
from keras.models import load_model
|
13 |
|
14 |
+
model = load_model('image_caption.h5')
|
15 |
+
tokenizer = Tokenizer()
|
16 |
+
max_length=35
|
17 |
# Load pre-trained model
|
18 |
+
vgg_model = VGG16()
|
19 |
+
vgg_model = Model(inputs=vgg_model.inputs, outputs=vgg_model.layers[-2].output)
|
20 |
|
21 |
# Set Streamlit configurations
|
22 |
st.set_page_config(page_title="Image Classifier App", layout="wide")
|
23 |
|
|
|
|
|
|
|
|
|
|
|
24 |
|
25 |
# Function to preprocess the input image
|
26 |
def preprocess_image(image):
|
27 |
+
image = load_img(image, target_size=(224, 224))
|
28 |
+
image = img_to_array(image)
|
29 |
+
image = image.reshape((1, image.shape[0], image.shape[1], image.shape[2]))
|
30 |
+
image = preprocess_input(image)
|
31 |
return image
|
32 |
|
33 |
# Function to make predictions on the input image
|
34 |
def predict(image):
|
35 |
image = preprocess_image(image)
|
36 |
+
feature = vgg_model.predict(image, verbose=0)
|
37 |
+
preds = predict_caption(model, feature, tokenizer, max_length)
|
38 |
+
preds=preds[8:-7]
|
39 |
+
return preds
|
40 |
+
|
41 |
+
def idx_word(integer,tok):
|
42 |
+
for word,index in tok.word_index.items():
|
43 |
+
if index== integer:
|
44 |
+
return word
|
45 |
+
return None
|
46 |
+
|
47 |
+
def predict_caption(model,image,tok,max_len):
|
48 |
+
in_text="startseq"
|
49 |
+
for i in range(max_len):
|
50 |
+
seq=tok.texts_to_sequences([in_text])[0]
|
51 |
+
seq=pad_sequences([seq],max_len)
|
52 |
+
yhat = model.predict([image, seq], verbose=0)
|
53 |
+
yhat = np.argmax(yhat)
|
54 |
+
word = idx_word(yhat, tok)
|
55 |
+
if word is None:
|
56 |
+
break
|
57 |
+
in_text += " " + word
|
58 |
+
if word == 'endseq':
|
59 |
+
break
|
60 |
+
return in_text
|
61 |
|
62 |
# Streamlit app
|
63 |
def main():
|