mralamdari commited on
Commit
f49fa5d
·
1 Parent(s): 9a08d18

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -63
app.py CHANGED
@@ -3,7 +3,7 @@ import numpy as np
3
  import gradio as gr
4
  import tensorflow as tf
5
 
6
- print(33333333333333333333333333333)
7
  model = tf.keras.models.Sequential([
8
  tf.keras.layers.Input(shape=(28, 28, 1)),
9
  tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
@@ -24,35 +24,20 @@ model.compile(optimizer=tf.keras.optimizers.Adam(),
24
  metrics=[tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.AUC(), tf.keras.metrics.CategoricalAccuracy()])
25
 
26
  model = tf.keras.models.load_model('my_model.h5', compile=False)
27
- print(222222222222222222222222)
28
 
29
  def classify_image(image):
30
  image = np.array(image['composite'])[:, :, 3] * 255
31
  image = image[..., np.newaxis]
32
- # if len(image.shape) >= 3:
33
- # image = tf.image.rgb_to_grayscale(image)
34
-
35
  image_tensor = tf.convert_to_tensor(image)
36
-
37
  image_tensor = tf.image.resize(image_tensor, (28, 28)),
38
- # print(1111111111111111111111111111111111111111111)
39
- # print(image_tensor.shape)
40
-
41
  image_tensor = tf.cast(image_tensor, tf.float32)
42
- print(1111111111111111111111111111111111111111111)
43
- # print(image_tensor.shape)
44
-
45
- # image_tensor = tf.expand_dims(image_tensor, 0)
46
  image_tensor = image_tensor / 255.0
47
- print(1111111111111111111111111111111111111111111)
48
- # print(image_tensor.shape)
49
-
50
  prediction = model.predict(image_tensor)
 
 
51
 
52
- print(1111111111111111111111111111111111111111111)
53
  prediction_label = str(prediction.argmax())
54
- print(1111111111111111111111111111111111111111111)
55
-
56
  return prediction_label
57
 
58
 
@@ -60,51 +45,19 @@ title = "Draw to Search"
60
  description = "Using the power of AI to detect the number you draw!"
61
  article = "for source code you can visit [my github](https://github.com/mralamdari)"
62
 
63
- # example_list = [["examples/" + example] for example in os.listdir("examples")]
 
 
 
64
 
65
  interface = gr.Interface(fn=classify_image,
 
66
  inputs=gr.Sketchpad(),
67
- outputs='text')
68
-
69
- # interface = gr.Interface(fn=classify_image,
70
- # # inputs=gr.Image(type="pil"),
71
- # inputs=gr.Sketchpad(),
72
- # # outputs=gr.Label(num_top_classes=3, label="Predictions"),
73
- # outputs='text',
74
- # examples=example_list,
75
- # title=title,
76
- # description=description,
77
- # article=article)
78
 
79
- interface.launch()
80
-
81
-
82
-
83
-
84
- # import gradio as gr
85
- # # from PIL import Image
86
-
87
- # def predict(img):
88
- # print(img)
89
- # print(11111111111111111111111111111)
90
- # print(np.unique(np.array(img['composite'])[:, :, 0], return_counts=True))
91
-
92
- # print(11111111111111111111111111111)
93
- # print(np.unique(np.array(img['composite'])[:, :, 1], return_counts=True))
94
-
95
- # print(11111111111111111111111111111)
96
- # print(np.unique(np.array(img['composite'])[:, :, 2], return_counts=True))
97
-
98
- # print(11111111111111111111111111111)
99
- # print(np.unique(np.array(img['composite'])[:, :, 3], return_counts=True))
100
- # img = np.array(img['composite'])[:, :, 3] * 255
101
- # print(img)
102
- # # print(np.squeeze(np.array(img['composite']), axis=-1))
103
- # return img
104
-
105
- # sp = gr.Sketchpad()
106
-
107
- # gr.Interface(fn=predict,
108
- # inputs=sp,
109
- # outputs='image',
110
- # live=True).launch()
 
3
  import gradio as gr
4
  import tensorflow as tf
5
 
6
+
7
  model = tf.keras.models.Sequential([
8
  tf.keras.layers.Input(shape=(28, 28, 1)),
9
  tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
 
24
  metrics=[tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.AUC(), tf.keras.metrics.CategoricalAccuracy()])
25
 
26
  model = tf.keras.models.load_model('my_model.h5', compile=False)
27
+
28
 
29
  def classify_image(image):
30
  image = np.array(image['composite'])[:, :, 3] * 255
31
  image = image[..., np.newaxis]
 
 
 
32
  image_tensor = tf.convert_to_tensor(image)
 
33
  image_tensor = tf.image.resize(image_tensor, (28, 28)),
 
 
 
34
  image_tensor = tf.cast(image_tensor, tf.float32)
 
 
 
 
35
  image_tensor = image_tensor / 255.0
 
 
 
36
  prediction = model.predict(image_tensor)
37
+ print(prediction)
38
+ print(prediction.argmax)
39
 
 
40
  prediction_label = str(prediction.argmax())
 
 
41
  return prediction_label
42
 
43
 
 
45
  description = "Using the power of AI to detect the number you draw!"
46
  article = "for source code you can visit [my github](https://github.com/mralamdari)"
47
 
48
+ example_list = [["examples/" + example] for example in os.listdir("examples")]
49
+ # interface = gr.Interface(fn=classify_image,
50
+ # inputs=gr.Sketchpad(),
51
+ # outputs='text')
52
 
53
  interface = gr.Interface(fn=classify_image,
54
+ # inputs=gr.Image(type="pil"),
55
  inputs=gr.Sketchpad(),
56
+ # outputs=gr.Label(num_top_classes=3, label="Predictions"),
57
+ outputs='text',
58
+ examples=example_list,
59
+ title=title,
60
+ description=description,
61
+ article=article)
 
 
 
 
 
62
 
63
+ interface.launch()