File size: 2,645 Bytes
024d810
bbc9388
262ef16
 
 
348c2c9
ffd507f
 
 
 
 
 
024d810
08e867a
49c4bcc
502e8f9
08e867a
 
502e8f9
bbc9388
08e867a
bbc9388
08e867a
bbc9388
49c4bcc
08e867a
bbc9388
 
c0f7187
bbc9388
 
 
 
bca051a
348c2c9
08e867a
 
 
 
 
18fdf8c
08e867a
18fdf8c
 
 
 
08e867a
bbc9388
49c4bcc
fea9eb8
bbc9388
 
08db411
bbc9388
fea9eb8
bbc9388
fea9eb8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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()