Spaces:
Build error
Build error
Upload 6 files
Browse files- app.py +49 -0
- get.py +0 -0
- keras_model.h5 +3 -0
- labels.txt +10 -0
- machine.py +42 -0
- requirements.txt +0 -0
app.py
ADDED
@@ -0,0 +1,49 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import tensorflow as tf
|
3 |
+
from PIL import Image
|
4 |
+
import numpy as np
|
5 |
+
|
6 |
+
# Teachable Machineで作成したモデルのパス
|
7 |
+
model_path = "keras_model.h5"
|
8 |
+
|
9 |
+
# モデルの読み込み
|
10 |
+
model = tf.keras.models.load_model(model_path)
|
11 |
+
|
12 |
+
# クラスのラベル(Teachable Machineで指定したクラス名と同じ順序で設定)
|
13 |
+
class_labels = [
|
14 |
+
"Cotton",
|
15 |
+
"Linen",
|
16 |
+
"Silk",
|
17 |
+
"Wool",
|
18 |
+
"Polyester",
|
19 |
+
"Nylon",
|
20 |
+
"Rayon",
|
21 |
+
"Fleece",
|
22 |
+
"Leather",
|
23 |
+
"Synth Leather"
|
24 |
+
]
|
25 |
+
|
26 |
+
# GradioのUIの設定
|
27 |
+
def classify_image(img):
|
28 |
+
# 画像の前処理
|
29 |
+
img = Image.fromarray((img * 255).astype(np.uint8))
|
30 |
+
img = img.resize((224, 224))
|
31 |
+
img_array = tf.keras.preprocessing.image.img_to_array(img)
|
32 |
+
img_array = tf.expand_dims(img_array, 0) # バッチの次元を追加
|
33 |
+
|
34 |
+
# モデルの予測
|
35 |
+
predictions = model.predict(img_array)
|
36 |
+
predicted_class = class_labels[np.argmax(predictions)]
|
37 |
+
|
38 |
+
return predicted_class
|
39 |
+
|
40 |
+
# Gradio UIの作成
|
41 |
+
iface = gr.Interface(
|
42 |
+
fn=classify_image,
|
43 |
+
inputs=gr.Image(),
|
44 |
+
outputs=gr.Textbox(),
|
45 |
+
# live=True,
|
46 |
+
)
|
47 |
+
|
48 |
+
# UIの起動
|
49 |
+
iface.launch(share=True)
|
get.py
ADDED
The diff for this file is too large to render.
See raw diff
|
|
keras_model.h5
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:51868c5813592fbe0e31a63067d18b78852869c7f31bfcaf5c9b0443cd8a7e2c
|
3 |
+
size 2458608
|
labels.txt
ADDED
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
0 綿(コットン Cotton)
|
2 |
+
1 麻(リネン Linen)
|
3 |
+
2 絹(シルク Silk)
|
4 |
+
3 毛(ウール Wool)
|
5 |
+
4 ポリエステル(Polyester)
|
6 |
+
5 ナイロン(Nylon)
|
7 |
+
6 レーヨン(Rayon)
|
8 |
+
7 フリース(Fleece)
|
9 |
+
8 革(レザー Leather)
|
10 |
+
9 合皮(フェイクレザー Synthetic leather)
|
machine.py
ADDED
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from keras.models import load_model # TensorFlow is required for Keras to work
|
2 |
+
from PIL import Image, ImageOps # Install pillow instead of PIL
|
3 |
+
import numpy as np
|
4 |
+
|
5 |
+
# Disable scientific notation for clarity
|
6 |
+
np.set_printoptions(suppress=True)
|
7 |
+
|
8 |
+
# Load the model
|
9 |
+
model = load_model("keras_model.h5", compile=False)
|
10 |
+
|
11 |
+
# Load the labels
|
12 |
+
class_names = open("labels.txt", "r").readlines()
|
13 |
+
|
14 |
+
# Create the array of the right shape to feed into the keras model
|
15 |
+
# The 'length' or number of images you can put into the array is
|
16 |
+
# determined by the first position in the shape tuple, in this case 1
|
17 |
+
data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
|
18 |
+
|
19 |
+
image = Image.open("216541_0.jpg").convert("RGB")
|
20 |
+
|
21 |
+
# resizing the image to be at least 224x224 and then cropping from the center
|
22 |
+
size = (224, 224)
|
23 |
+
image = ImageOps.fit(image, size, Image.Resampling.LANCZOS)
|
24 |
+
|
25 |
+
# turn the image into a numpy array
|
26 |
+
image_array = np.asarray(image)
|
27 |
+
|
28 |
+
# Normalize the image
|
29 |
+
normalized_image_array = (image_array.astype(np.float32) / 127.5) - 1
|
30 |
+
|
31 |
+
# Load the image into the array
|
32 |
+
data[0] = normalized_image_array
|
33 |
+
|
34 |
+
# Predicts the model
|
35 |
+
prediction = model.predict(data)
|
36 |
+
index = np.argmax(prediction)
|
37 |
+
class_name = class_names[index]
|
38 |
+
confidence_score = prediction[0][index]
|
39 |
+
|
40 |
+
# Print prediction and confidence score
|
41 |
+
print("Class:", class_name[2:], end="")
|
42 |
+
print("Confidence Score:", confidence_score)
|
requirements.txt
ADDED
Binary file (3.6 kB). View file
|
|