Spaces:
Runtime error
Runtime error
baotoan2002
commited on
Commit
•
ad11330
1
Parent(s):
3cf352d
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from PIL import Image
|
3 |
+
import numpy as np
|
4 |
+
from pickle import load
|
5 |
+
from tensorflow.keras.applications.xception import Xception
|
6 |
+
from tensorflow.keras.models import load_model
|
7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
+
from matplotlib import pyplot as plt
|
9 |
+
|
10 |
+
def extract_features(filename, model):
|
11 |
+
try:
|
12 |
+
image = Image.open(filename)
|
13 |
+
except:
|
14 |
+
print("ERROR: Couldn't open image! Make sure the image path and extension is correct")
|
15 |
+
image = image.resize((299,299))
|
16 |
+
image = np.array(image)
|
17 |
+
# for images that has 4 channels, we convert them into 3 channels
|
18 |
+
if image.shape[2] == 4:
|
19 |
+
image = image[..., :3]
|
20 |
+
image = np.expand_dims(image, axis=0)
|
21 |
+
image = image/127.5
|
22 |
+
image = image - 1.0
|
23 |
+
feature = model.predict(image)
|
24 |
+
return feature
|
25 |
+
|
26 |
+
def word_for_id(integer, tokenizer):
|
27 |
+
for word, index in tokenizer.word_index.items():
|
28 |
+
if index == integer:
|
29 |
+
return word
|
30 |
+
return None
|
31 |
+
|
32 |
+
def generate_desc(model, tokenizer, photo, max_length):
|
33 |
+
in_text = 'start'
|
34 |
+
for i in range(max_length):
|
35 |
+
sequence = tokenizer.texts_to_sequences([in_text])[0]
|
36 |
+
sequence = pad_sequences([sequence], maxlen=max_length)
|
37 |
+
pred = model.predict([photo,sequence], verbose=0)
|
38 |
+
pred = np.argmax(pred)
|
39 |
+
word = word_for_id(pred, tokenizer)
|
40 |
+
if word is None:
|
41 |
+
break
|
42 |
+
in_text += ' ' + word
|
43 |
+
if word == 'end':
|
44 |
+
break
|
45 |
+
return in_text.split()[1:-1]
|
46 |
+
|
47 |
+
max_length = 32
|
48 |
+
tokenizer = load(open("tokenizer.p","rb"))
|
49 |
+
model = load_model('models/model_9.h5')
|
50 |
+
xception_model = Xception(include_top=False, pooling="avg")
|
51 |
+
|
52 |
+
def caption_generator(img_path):
|
53 |
+
photo = extract_features(img_path, xception_model)
|
54 |
+
img = Image.open(img_path)
|
55 |
+
description = generate_desc(model, tokenizer, photo, max_length)
|
56 |
+
description = ' '.join(description)
|
57 |
+
return description
|
58 |
+
|
59 |
+
inputs = gr.inputs.File(label="Select an Image")
|
60 |
+
outputs = gr.outputs.Textbox(label="Description")
|
61 |
+
|
62 |
+
gr.Interface(fn=caption_generator , inputs=inputs, outputs=outputs, capture_session=True).launch()
|