classify_image / app.py
ccwu0918's picture
Update app.py
fea9eb8
raw
history blame
2.51 kB
import gradio as gr
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from tensorflow.keras.applications import ResNet50V2
from tensorflow.keras.models import Sequential, load_model
from tensorflow.keras.layers import Dense
from tensorflow.keras.utils import to_categorical
from tensorflow.keras.applications.resnet_v2 import preprocess_input
from tensorflow.keras.preprocessing.image import load_img, img_to_array
image_folders = ['King_Crab', 'Wind_Lion_God', 'pavo_cristatus', 'otter', 'Upupa_epops']
labels = ["鱟", "ι‡‘ι–€ι’¨η…ηˆΊ", "金門藍孔雀", "歐亞水獺", "ι‡‘ι–€ζˆ΄ε‹ι³₯"]
base_dir = './'
thedir = base_dir + image_folders[0]
os.listdir(thedir)
data = []
target = []
for i in range(5):
thedir = base_dir + image_folders[i]
image_fnames = os.listdir(thedir)
for theimage in image_fnames:
if theimage == ".git" or theimage == ".ipynb_checkpoints":
continue
img_path = thedir + '/' + theimage
img = load_img(img_path , target_size = (256,256))
x = img_to_array(img)
data.append(x)
target.append(i)
model = load_model('my_cnn_model.pb') # Loading the Tensorflow Saved Model (PB)
print(model.summary())
def classify_image(inp):
inp = inp.reshape((-1, 256, 256, 3))
inp = preprocess_input(inp)
prediction = model.predict(inp).flatten()
return {labels[i]: float(prediction[i]) for i in range(5)}
image = gr.Image(shape=(256, 256), label="ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯照片")
label = gr.Label(num_top_classes=5, label="AI辨識硐果")
some_text="ζˆ‘θƒ½θΎ¨θ­˜ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯γ€‚ζ‰ΎεΌ΅ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯η…§η‰‡δΎ†θ€ƒζˆ‘ε§!"
sample_images = []
for i in range(5):
thedir = base_dir + image_folders[i]
for file in os.listdir(thedir):
if file == ".git" or file == ".ipynb_checkpoints":
continue
sample_images.append(image_folders[i] + '/' + file)
iface = gr.Interface(fn=classify_image,
inputs=image,
outputs=label,
title="AI ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯辨識機",
description=some_text,
examples=sample_images, live=True)
# def greet(name):
# model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
# return "Hello " + name + "!!" + model.summary()
# iface = gr.Interface(fn=greet, inputs="text", outputs="text")
# .launch(share=True)
iface.launch()