NKASG commited on
Commit
520fe8b
·
verified ·
1 Parent(s): ae009bc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -10
app.py CHANGED
@@ -2,14 +2,18 @@ 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",
@@ -23,29 +27,32 @@ class_labels = [
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)
50
 
51
 
 
2
  import tensorflow as tf
3
  from PIL import Image
4
  import numpy as np
5
+ from fastai.vision.all import *
6
 
7
+
8
+ learn = load_learner('export.pkl')
9
  model_path = "keras_model.h5"
10
 
11
+
12
  model = tf.keras.models.load_model(model_path)
13
+ categories = ('Blouse', 'Dress', 'Pants', 'Shirt', 'Shorts')
14
+
15
+ title = "Clothing Identifier"
16
 
 
17
  class_labels = [
18
  "Cotton",
19
  "Linen",
 
27
  "Synth Leather"
28
  ]
29
 
30
+
31
  def classify_image(img):
32
+ pred, idx, probs = learn.predict(img)
33
+ highest_prob_index = probs.argmax()
34
  img = Image.fromarray((img * 255).astype(np.uint8))
35
  img = img.resize((224, 224))
36
  img_array = tf.keras.preprocessing.image.img_to_array(img)
37
+ img_array = tf.expand_dims(img_array, 0)
38
 
39
+
40
  predictions = model.predict(img_array)
41
  predicted_class = class_labels[np.argmax(predictions)]
42
 
43
+ return The cloth belongs to category categories[highest_prob_index] a predicted_class material
44
+
45
 
 
46
  iface = gr.Interface(
47
  fn=classify_image,
48
  inputs=gr.Image(),
49
  outputs=gr.Textbox(),
50
+ title = title,
51
+ examples = ['dress.jpg', 'shirt.jpg', 'pants.jpg', 'shorts.jpg'],
52
  # live=True,
53
  )
54
 
55
+
56
  iface.launch(share=True)
57
 
58