zihaoz96 commited on
Commit
1b87b27
1 Parent(s): d7b7621

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +18 -46
app.py CHANGED
@@ -1,69 +1,41 @@
1
  import gradio as gr
2
  import numpy as np
3
- from PIL import Image
4
- import os
5
- import json
6
 
7
- from hugsvision.inference.TorchVisionClassifierInference import TorchVisionClassifierInference
8
 
9
- models_name = [
10
- "VGG16",
11
- "ShuffleNetV2",
12
- "mobilenet_v2"
13
- ]
14
-
15
- colname = "mobilenet_v2"
16
-
17
- radio = gr.inputs.Radio(models_name, default="mobilenet_v2", type="value", label=colname)
18
- print(radio.label)
19
-
20
- def predict_image(image, model_name):
21
-
22
- image = Image.fromarray(np.uint8(image)).convert('RGB')
23
 
24
- print("======================")
25
- print(type(image))
26
- print(type(model_name))
27
- print("==========")
28
- print(image)
29
- print(model_name)
30
- print("======================")
31
 
32
- # image = np.array(image) / 255
33
- # image = np.expand_dims(image, axis=0)
34
 
35
- classifier = TorchVisionClassifierInference(
36
- model_path = "./models/" + colname,
37
- )
38
 
39
- pred = classifier.predict_image(img=image)
40
- print(pred)
41
 
42
- acc = dict((labels[i], 0.0) for i in range(len(labels)))
43
- acc[pred] = 100.0
44
  print(acc)
45
  return acc
46
- # return pred
47
 
48
- # open categories.txt in read mode
49
- categories = open("categories.txt", "r")
50
- labels = categories.readline().split(";")
51
 
52
- image = gr.inputs.Image(shape=(300, 300), label="Upload Your Image Here")
53
- print(image)
54
  label = gr.outputs.Label(num_top_classes=len(labels))
55
 
56
- samples = ['./samples/basking.jpg', './samples/blacktip.jpg']
57
- # , './samples/blacktip.jpg', './samples/blue.jpg', './samples/bull.jpg', './samples/hammerhead.jpg',
58
- # './samples/lemon.jpg', './samples/mako.jpg', './samples/nurse.jpg', './samples/sand tiger.jpg', './samples/thresher.jpg',
59
- # './samples/tigre.jpg', './samples/whale.jpg', './samples/white.jpg', './samples/whitetip.jpg']
60
 
61
  interface = gr.Interface(
62
  fn=predict_image,
63
- inputs=[image, radio],
64
  outputs=label,
65
  capture_session=True,
66
  allow_flagging=False,
67
- # examples=samples
68
  )
69
  interface.launch()
 
1
  import gradio as gr
2
  import numpy as np
3
+ import pandas as pd
4
+ from tensorflow.keras import models
 
5
 
6
+ import tensorflow as tf
7
 
8
+ # open categories.txt in read mode
9
+ categories = open("categories.txt", "r")
10
+ labels = categories.readline().split(";")
 
 
 
 
 
 
 
 
 
 
 
11
 
12
+ model = models.load_model('models/modelnet/best_model.h5')
 
 
 
 
 
 
13
 
 
 
14
 
15
+ def predict_image(image):
16
+ image = np.array(image) / 255
17
+ image = np.expand_dims(image, axis=0)
18
 
19
+ pred = model.predict(image)
 
20
 
21
+ acc = dict((labels[i], "%.2f" % pred[0][i]) for i in range(len(labels)))
 
22
  print(acc)
23
  return acc
 
24
 
 
 
 
25
 
26
+ image = gr.inputs.Image(shape=(224, 224), label="Upload Your Image Here")
 
27
  label = gr.outputs.Label(num_top_classes=len(labels))
28
 
29
+ samples = ['samples/basking.jpg', 'samples/blacktip.jpg', 'samples/blue.jpg', 'samples/bull.jpg', 'samples/hammerhead.jpg',
30
+ 'samples/lemon.jpg', 'samples/mako.jpg', 'samples/nurse.jpg', 'samples/sand tiger.jpg', 'samples/thresher.jpg',
31
+ 'samples/tigre.jpg', 'samples/whale.jpg', 'samples/white.jpg', 'samples/whitetip.jpg']
 
32
 
33
  interface = gr.Interface(
34
  fn=predict_image,
35
+ inputs=image,
36
  outputs=label,
37
  capture_session=True,
38
  allow_flagging=False,
39
+ examples=samples
40
  )
41
  interface.launch()