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()