Spaces:
Runtime error
Runtime error
naman.mistry
commited on
Commit
•
49ec72f
1
Parent(s):
b04ea5f
Initial commit
Browse files- app.py +64 -0
- count_words.pkl +3 -0
- inv_dict.pkl +3 -0
- model.h5 +3 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,64 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
from tensorflow.keras.models import load_model
|
3 |
+
import cv2
|
4 |
+
import numpy as np
|
5 |
+
from tensorflow.keras.applications import ResNet50
|
6 |
+
from tensorflow.keras.models import Model
|
7 |
+
from tensorflow.keras.preprocessing.sequence import pad_sequences
|
8 |
+
import pickle
|
9 |
+
|
10 |
+
# Define the previous model
|
11 |
+
incept_model = ResNet50(include_top=True)
|
12 |
+
last = incept_model.layers[-2].output
|
13 |
+
modele = Model(inputs=incept_model.input, outputs=last)
|
14 |
+
|
15 |
+
model = load_model("model.h5")
|
16 |
+
|
17 |
+
pickle_open = open("count_words.pkl","rb")
|
18 |
+
count_words = pickle.load(pickle_open)
|
19 |
+
pickle_open.close()
|
20 |
+
|
21 |
+
pickle_open = open("inv_dict.pkl","rb")
|
22 |
+
inv_dict = pickle.load(pickle_open)
|
23 |
+
pickle_open.close()
|
24 |
+
|
25 |
+
def getImage(image):
|
26 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
27 |
+
img = cv2.resize(img, (224,224))
|
28 |
+
img = np.reshape(img, (1,224,224,3))
|
29 |
+
return img
|
30 |
+
|
31 |
+
def predict(image):
|
32 |
+
test_feature = modele.predict(getImage(image)).reshape(1,2048)
|
33 |
+
|
34 |
+
test_img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
35 |
+
test_img = cv2.cvtColor(test_img, cv2.COLOR_BGR2RGB)
|
36 |
+
|
37 |
+
text_inp = ['startofseq']
|
38 |
+
count = 0
|
39 |
+
caption = ''
|
40 |
+
|
41 |
+
while count < 25:
|
42 |
+
count += 1
|
43 |
+
|
44 |
+
encoded = [count_words.get(word, count_words.get('unk', 0)) for word in text_inp]
|
45 |
+
encoded = [encoded]
|
46 |
+
|
47 |
+
encoded = pad_sequences(encoded, padding='post', truncating='post', maxlen=34)
|
48 |
+
|
49 |
+
prediction = np.argmax(model.predict([test_feature, encoded]))
|
50 |
+
sampled_word = inv_dict[prediction]
|
51 |
+
|
52 |
+
caption = caption + ' ' + sampled_word
|
53 |
+
|
54 |
+
if sampled_word == 'endofseq':
|
55 |
+
break
|
56 |
+
|
57 |
+
text_inp.append(sampled_word)
|
58 |
+
|
59 |
+
return caption
|
60 |
+
|
61 |
+
input_image = gr.inputs.Image(label="Upload an image")
|
62 |
+
output_text = gr.outputs.Textbox(label="Predictions")
|
63 |
+
|
64 |
+
gr.Interface(fn=predict, inputs=input_image, outputs=output_text).launch()
|
count_words.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c1fce97bb00c149e1a6faec128d10f18709483147112ef1a8a9be5bc0b5ab1dc
|
3 |
+
size 50118
|
inv_dict.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:35d21f34125580dcb91dc8375adeeb7f0c175d79c7c147dcce725eb562236814
|
3 |
+
size 50118
|
model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9d3c1276449f76c029d928f0ceb293b5c2197ea866de6bedd55634a19669aa61
|
3 |
+
size 38347112
|
requirements.txt
ADDED
Binary file (3.68 kB). View file
|
|