veronhii commited on
Commit
03cf1d4
·
1 Parent(s): 1d46b93

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +13 -15
app.py CHANGED
@@ -15,21 +15,19 @@ def normalize_image(img):
15
  return img
16
 
17
  def predict_top_classes(img, num_top_classes=5):
18
- img = img.convert('RGB')
19
- img_data = normalize_image(img)
20
- x = np.array(img_data)
21
- x = np.expand_dims(x, axis=0)
22
- temp = model.predict(x)
23
-
24
- top_class_indices = np.argpartition(temp, -num_top_classes)[-num_top_classes:]
25
- top_class_indices = top_class_indices[np.argsort(temp[0, top_class_indices])[::-1]]
26
-
27
- top_classes = [CLASS_LABEL[i] for i in top_class_indices]
28
- top_probabilities = [temp[0, i] for i in top_class_indices]
29
-
30
- return dict(zip(top_classes, top_probabilities))
31
-
32
  model = tf.keras.models.load_model("Xception.h5")
33
 
34
- interface = gr.Interface(predict_top_classes, gr.inputs.Image(type='pil'), outputs='json', args={'num_top_classes': 5})
35
  interface.launch()
 
15
  return img
16
 
17
  def predict_top_classes(img, num_top_classes=5):
18
+ img = img.convert('RGB')
19
+ img_data = _normalize_img(img)
20
+ x = np.array(img_data)
21
+ x = np.expand_dims(x, axis=0)
22
+ temp = model.predict(x)
23
+
24
+ idx = np.argsort(np.squeeze(temp))[::-1]
25
+ top5_value = np.asarray([temp[0][i] for i in idx[0:5]])
26
+ top5_idx = idx[0:5]
27
+
28
+ return {CLASS_LABEL[i]:str(v) for i,v in zip(top5_idx,top5_value)}
29
+
 
 
30
  model = tf.keras.models.load_model("Xception.h5")
31
 
32
+ interface = gr.Interface(predict_top_classes, gr.inputs.Image(type='pil'), outputs='label', args={'num_top_classes': 5})
33
  interface.launch()