ccwu0918 commited on
Commit
bbc9388
Β·
1 Parent(s): ffd507f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -5
app.py CHANGED
@@ -1,5 +1,5 @@
1
  import gradio as gr
2
-
3
  import numpy as np
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
@@ -11,12 +11,64 @@ from tensorflow.keras.utils import to_categorical
11
  from tensorflow.keras.applications.resnet_v2 import preprocess_input
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
- def greet(name):
 
 
16
 
17
- model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
- return "Hello " + name + "!!" + model.summary()
 
 
 
20
 
21
- iface = gr.Interface(fn=greet, inputs="text", outputs="text")
22
  iface.launch()
 
1
  import gradio as gr
2
+ import os
3
  import numpy as np
4
  import pandas as pd
5
  import matplotlib.pyplot as plt
 
11
  from tensorflow.keras.applications.resnet_v2 import preprocess_input
12
  from tensorflow.keras.preprocessing.image import load_img, img_to_array
13
 
14
+ image_folders = ['King_Crab', 'Wind_Lion_God', 'pavo_cristatus', 'otter', 'Upupa_epops']
15
+ labels = ["鱟", "ι‡‘ι–€ι’¨η…ηˆΊ", "金門藍孔雀", "歐亞水獺", "ι‡‘ι–€ζˆ΄ε‹ι³₯"]
16
+
17
+ base_dir = './'
18
+
19
+ thedir = base_dir + image_folders[0]
20
+ os.listdir(thedir)
21
+
22
+ data = []
23
+ target = []
24
+ for i in range(5):
25
+ thedir = base_dir + image_folders[i]
26
+ image_fnames = os.listdir(thedir)
27
+ for theimage in image_fnames:
28
+ if theimage == ".git" or theimage == ".ipynb_checkpoints":
29
+ continue
30
+ img_path = thedir + '/' + theimage
31
+ img = load_img(img_path , target_size = (256,256))
32
+ x = img_to_array(img)
33
+ data.append(x)
34
+ target.append(i)
35
+
36
+ model = load_model('my_cnn_model.pb') # Loading the Tensorflow Saved Model (PB)
37
+ print(model.summary())
38
+
39
+ def classify_image(inp):
40
+ inp = inp.reshape((-1, 256, 256, 3))
41
+ inp = preprocess_input(inp)
42
+ prediction = model.predict(inp).flatten()
43
+ return {labels[i]: float(prediction[i]) for i in range(5)}
44
 
45
+ image = gr.Image(shape=(256, 256), label="ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯照片")
46
+ label = gr.Label(num_top_classes=5, label="AI辨識硐果")
47
+ some_text="ζˆ‘θƒ½θΎ¨θ­˜ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯γ€‚ζ‰ΎεΌ΅ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯η…§η‰‡δΎ†θ€ƒζˆ‘ε§!"
48
 
49
+ # sample_images = []
50
+ # for i in range(5):
51
+ # thedir = base_dir + image_folders[i]
52
+ # for file in os.listdir(thedir):
53
+ # if file == ".git" or file == ".ipynb_checkpoints":
54
+ # continue
55
+ # sample_images.append(image_folders[i] + '/' + file)
56
+
57
+ iface = gr.Interface(fn=classify_image,
58
+ inputs=image,
59
+ outputs=label,
60
+ title="AI ι‡‘ι–€θ—ε­”ι›€γ€ζ­δΊžζ°΄ηΊγ€ζˆ΄ε‹ι³₯辨識機",
61
+ description=some_text,
62
+ examples=sample_images, live=True)
63
+
64
+ # def greet(name):
65
+
66
+ # model = load_model('my_cnn_model.h5') # Loading the Tensorflow Saved Model (PB)
67
 
68
+ # return "Hello " + name + "!!" + model.summary()
69
+
70
+
71
+ # iface = gr.Interface(fn=greet, inputs="text", outputs="text")
72
 
73
+ # .launch(share=True)
74
  iface.launch()