Spaces:
Runtime error
Runtime error
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 = ['Merops_philippinus', 'pavo_cristatus', 'Upupa_epops', 'King_Crab', 'otter'] | |
# ็บไบๅพ้ข็้่ฆ๏ผๆๅๅฐไบ็จฎ้กๅฅ็ ง็็็ญๆก็จ `labels` ๅ็พ | |
labels = ["ๆ ๅ่่", "่ๅญ้", "ๆดๅ", "้ฑ", "ๆญไบๆฐด็บ"] | |
num_classes = len(labels) | |
base_dir = './classify_image/' | |
# ่ผๅ ฅไธฆๆชข่ฆ่จ็ทดๅฎๆ็ๆจกๅใ | |
model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB) | |
print(model.summary()) | |
# ๆณจๆ็พๅจไธปๅฝๆธๅ่พจ่ญๅชๆไบๅ็จฎ้กใ่ไธๆฏไฝฟ็จๆๅ่ช่ก่จ็ทด็ model! | |
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(num_classes)} | |
image = gr.Image(shape=(256, 256), label="ๆ ๅ่่ใ่ๅญ้ใๆดๅใ้ฑๅๆญไบๆฐด็บ็ ง็") | |
label = gr.Label(num_top_classes=num_classes, label="AI ResNet50V2้ท็งปๅผๅญธ็ฟ่พจ่ญ็ตๆ") | |
some_text="ๆ่ฝ่พจ่ญ้้ๆ ๅ่่ใ่ๅญ้ใๆดๅใ้ฑๅๆญไบๆฐด็บใๆพๅผต้้ๆ ๅ่่ใ่ๅญ้ใๆดๅใ้ฑๅๆญไบๆฐด็บ็ ง็ไพ่ๆๅง!" | |
# ๆๅๅฐ้้ๆ ๅ่่ใ่ๅญ้ใๆดๅใ้ฑๅๆญไบๆฐด็บๆธๆๅบซไธญ็ๅ็ๆฟๅบไพ็ถไฝ็ฏไพๅ็่ฎไฝฟ็จ่ ไฝฟ็จ | |
sample_images = [] | |
for i in range(num_classes): | |
thedir = base_dir + image_folders[i] | |
for file in os.listdir(thedir): | |
if file == ".git" or file == ".ipynb_checkpoints": | |
continue | |
sample_images.append(base_dir + image_folders[i] + '/' + file) | |
# ๆๅพ๏ผๅฐๆๆๆฑ่ฅฟ็ต่ฃๅจไธ่ตท๏ผๅฐฑๅคงๅๅๆไบ๏ผ | |
iface = gr.Interface(fn=classify_image, | |
inputs=image, | |
outputs=label, | |
title="AI ๆ ๅ่่ใ่ๅญ้ใๆดๅใ้ฑๅๆญไบๆฐด็บ่พจ่ญๆฉ", | |
description=some_text, | |
examples=sample_images, live=True) | |
iface.launch() |