Upload app files
Browse files- app.py +68 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from keras.models import load_model
|
| 2 |
+
from PIL import Image, ImageOps
|
| 3 |
+
import numpy as np
|
| 4 |
+
import gradio as gr
|
| 5 |
+
import pandas as pd
|
| 6 |
+
|
| 7 |
+
def format_label(label):
|
| 8 |
+
"""
|
| 9 |
+
From '0 rùa khác\n' to 'rùa khác'
|
| 10 |
+
"""
|
| 11 |
+
return label[label.find(" ")+1:-1]
|
| 12 |
+
|
| 13 |
+
def predict(image):
|
| 14 |
+
|
| 15 |
+
# Load the model
|
| 16 |
+
model = load_model('keras_model.h5')
|
| 17 |
+
|
| 18 |
+
# Create the array of the right shape to feed into the keras model
|
| 19 |
+
# The 'length' or number of images you can put into the array is
|
| 20 |
+
# determined by the first position in the shape tuple, in this case 1.
|
| 21 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
| 22 |
+
|
| 23 |
+
#resize the image to a 224x224 with the same strategy as in TM2:
|
| 24 |
+
#resizing the image to be at least 224x224 and then cropping from the center
|
| 25 |
+
size = (224, 224)
|
| 26 |
+
image = ImageOps.fit(image, size, Image.ANTIALIAS)
|
| 27 |
+
|
| 28 |
+
#turn the image into a numpy array
|
| 29 |
+
image_array = np.asarray(image)
|
| 30 |
+
# Normalize the image
|
| 31 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.0) - 1
|
| 32 |
+
# Load the image into the array
|
| 33 |
+
data[0] = normalized_image_array
|
| 34 |
+
|
| 35 |
+
# run the inference
|
| 36 |
+
pred = model.predict(data)
|
| 37 |
+
pred = pred.tolist()
|
| 38 |
+
|
| 39 |
+
with open('labels.txt','r') as f:
|
| 40 |
+
labels = f.readlines()
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
result = {format_label(labels[i]): round(pred[0][i],2) for i in range(len(pred[0]))}
|
| 44 |
+
|
| 45 |
+
return result
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
description="""
|
| 49 |
+
|
| 50 |
+
Description
|
| 51 |
+
|
| 52 |
+
"""
|
| 53 |
+
|
| 54 |
+
title = """
|
| 55 |
+
|
| 56 |
+
Title
|
| 57 |
+
|
| 58 |
+
"""
|
| 59 |
+
|
| 60 |
+
examples = [['example1.jpg'], ['example2.jpg'], ['example3.jpg']]
|
| 61 |
+
|
| 62 |
+
gr.Interface(fn=predict,
|
| 63 |
+
inputs=gr.Image(type="pil", label="Input Image"),
|
| 64 |
+
outputs=[gr.Label(), gr.Markdown()],
|
| 65 |
+
live=True,
|
| 66 |
+
title=title,
|
| 67 |
+
description=description,
|
| 68 |
+
examples=examples).launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
Pillow
|
| 3 |
+
numpy
|
| 4 |
+
pandas
|