Spaces:
Runtime error
Runtime error
hrishikesh
commited on
Commit
·
25fa374
1
Parent(s):
260608b
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
|
| 2 |
+
import numpy as np
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import matplotlib.pylab as plt
|
| 5 |
+
import gradio as gr
|
| 6 |
+
import PIL.Image as Image
|
| 7 |
+
import tensorflow as tf
|
| 8 |
+
import tensorflow_hub as hub
|
| 9 |
+
|
| 10 |
+
TF_MODEL_URL = 'https://tfhub.dev/google/on_device_vision/classifier/landmarks_classifier_asia_V1/1'
|
| 11 |
+
LABEL_MAP_URL = 'https://www.gstatic.com/aihub/tfhub/labelmaps/landmarks_classifier_asia_V1_label_map.csv'
|
| 12 |
+
IMAGE_SHAPE = (321, 321)
|
| 13 |
+
|
| 14 |
+
classifier = tf.keras.Sequential([hub.KerasLayer(TF_MODEL_URL,
|
| 15 |
+
input_shape=IMAGE_SHAPE+(3,),
|
| 16 |
+
output_key="predictions:logits")])
|
| 17 |
+
|
| 18 |
+
df = pd.read_csv(LABEL_MAP_URL)
|
| 19 |
+
|
| 20 |
+
label_map = dict(zip(df.id, df.name))
|
| 21 |
+
|
| 22 |
+
label_map
|
| 23 |
+
|
| 24 |
+
img_loc = "image.jpeg"
|
| 25 |
+
|
| 26 |
+
img = Image.open(img_loc).resize(IMAGE_SHAPE)
|
| 27 |
+
|
| 28 |
+
img
|
| 29 |
+
|
| 30 |
+
img = np.array(img)/255.0
|
| 31 |
+
img.shape
|
| 32 |
+
|
| 33 |
+
img = img[np.newaxis, ...]
|
| 34 |
+
|
| 35 |
+
img.shape
|
| 36 |
+
|
| 37 |
+
result = classifier.predict(img)
|
| 38 |
+
|
| 39 |
+
result
|
| 40 |
+
|
| 41 |
+
label_map[np.argmax(result)]
|
| 42 |
+
|
| 43 |
+
class_names=list(label_map.values())
|
| 44 |
+
|
| 45 |
+
def classify_image(image):
|
| 46 |
+
img = np.array(image)/255.0
|
| 47 |
+
img = img[np.newaxis, ...]
|
| 48 |
+
prediction = classifier.predict(img)
|
| 49 |
+
return label_map[np.argmax(prediction)]
|
| 50 |
+
|
| 51 |
+
image = gr.inputs.Image(shape=(321, 321))
|
| 52 |
+
label = gr.outputs.Label(num_top_classes=1)
|
| 53 |
+
|
| 54 |
+
gr.Interface(
|
| 55 |
+
classify_image,
|
| 56 |
+
image,
|
| 57 |
+
label,
|
| 58 |
+
capture_session=True).launch(debug=True);
|