mralamdari commited on
Commit
11c183d
·
1 Parent(s): a44cc01

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +56 -46
app.py CHANGED
@@ -1,11 +1,10 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
- import gradio as gr
5
- from PIL import Image
6
- import tensorflow as tf
7
 
8
  # model = tf.keras.models.Sequential([
 
9
  # tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
10
  # tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu'),
11
  # tf.keras.layers.BatchNormalization(),
@@ -23,33 +22,46 @@ import tensorflow as tf
23
  # loss=tf.keras.losses.CategoricalCrossentropy(),
24
  # metrics=[tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.AUC(), tf.keras.metrics.CategoricalAccuracy()])
25
 
26
- # model.load_model("my_model.keras")
27
 
28
-
29
- # def image_mod(image):
30
-
31
- # # img = Image.fromarray(image['composite'])
32
- # model = tf.keras.models.load_model('weights_1.h5')
33
- # test_img = np.array(image['composite']).reshape(1, 28, 28, 1)
34
- # # test_img = cv2.resize(np.array(image['composite']), (28, 28, 1))
35
- # prediction = model.predict(test_img)
36
- # pred = np.argmax(prediction, axis=1)[0]
37
- # return pred
 
38
 
39
 
40
  # title = "Draw to Search"
41
  # description = "Using the power of AI to detect the number you draw!"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
 
43
- # demo = gr.Interface(
44
- # fn=image_mod,
45
- # inputs='sketchpad',
46
- # outputs='text',
47
- # title=title,
48
- # description=description,
49
- # live=True)
50
 
51
- # demo.launch(share=False)
52
- # demo.launch(debug=True)
53
 
54
 
55
 
@@ -73,28 +85,26 @@ import tensorflow as tf
73
 
74
 
75
 
76
- model = tf.keras.models.Sequential([
77
- tf.keras.layers.Input(shape=(28, 28, 1)),
78
- tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
79
- tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu'),
80
- tf.keras.layers.BatchNormalization(),
81
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu'),
82
- tf.keras.layers.Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='relu'),
83
- tf.keras.layers.BatchNormalization(),
84
- tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same', activation='relu'),
85
- tf.keras.layers.Conv2D(filters=64, kernel_size=3, strides=2, padding='same', activation='relu'),
86
- tf.keras.layers.BatchNormalization(),
87
- tf.keras.layers.GlobalAveragePooling2D(),
88
- tf.keras.layers.Dense(10, activation='softmax')
89
- ])
90
 
91
- model.compile(optimizer=tf.keras.optimizers.Adam(),
92
- loss=tf.keras.losses.CategoricalCrossentropy(),
93
- metrics=[tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.AUC(), tf.keras.metrics.CategoricalAccuracy()])
94
 
95
- model = tf.keras.models.load_model('my_model.h5', compile=False)
 
 
 
 
 
 
 
 
 
 
 
 
96
 
97
  def classify_image(image):
 
 
 
98
  if len(np.array(image).shape) == 3:
99
  image = tf.image.rgb_to_grayscale(image)
100
  image_tensor = tf.convert_to_tensor(image)
@@ -114,8 +124,8 @@ article = "for source code you can visit [my github](https://github.com/mralamda
114
  example_list = [["examples/" + example] for example in os.listdir("examples")]
115
 
116
  interface = gr.Interface(fn=classify_image,
117
- inputs=gr.Image(type="pil"),
118
- # inputs='sketchpad',
119
  outputs=gr.Label(num_top_classes=3, label="Predictions"),
120
  examples=example_list,
121
  title=title,
 
1
+ # import os
2
+ # import numpy as np
3
+ # import gradio as gr
4
+ # import tensorflow as tf
 
 
5
 
6
  # model = tf.keras.models.Sequential([
7
+ # tf.keras.layers.Input(shape=(28, 28, 1)),
8
  # tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu', input_shape=(28, 28, 1)),
9
  # tf.keras.layers.Conv2D(filters=16, kernel_size=3, strides=1, padding='same', activation='relu'),
10
  # tf.keras.layers.BatchNormalization(),
 
22
  # loss=tf.keras.losses.CategoricalCrossentropy(),
23
  # metrics=[tf.keras.metrics.MeanSquaredError(), tf.keras.metrics.AUC(), tf.keras.metrics.CategoricalAccuracy()])
24
 
25
+ # model = tf.keras.models.load_model('my_model.h5', compile=False)
26
 
27
+ # def classify_image(image):
28
+ # if len(np.array(image).shape) == 3:
29
+ # image = tf.image.rgb_to_grayscale(image)
30
+ # image_tensor = tf.convert_to_tensor(image)
31
+ # image_tensor = tf.image.resize(image_tensor, (28, 28)),
32
+ # image_tensor = tf.cast(image_tensor, tf.float32)
33
+ # # image_tensor = tf.expand_dims(image_tensor, 0)
34
+ # image_tensor = image_tensor / 255.0
35
+ # prediction = model.predict(image_tensor)
36
+ # prediction_label = str(prediction.argmax())
37
+ # return prediction_label
38
 
39
 
40
  # title = "Draw to Search"
41
  # description = "Using the power of AI to detect the number you draw!"
42
+ # article = "for source code you can visit [my github](https://github.com/mralamdari)"
43
+
44
+ # example_list = [["examples/" + example] for example in os.listdir("examples")]
45
+
46
+ # interface = gr.Interface(fn=classify_image,
47
+ # inputs=gr.Image(type="pil"),
48
+ # # inputs='sketchpad',
49
+ # outputs=gr.Label(num_top_classes=3, label="Predictions"),
50
+ # examples=example_list,
51
+ # title=title,
52
+ # description=description,
53
+ # article=article)
54
+
55
+ # interface.launch()
56
+
57
+
58
+
59
+
60
+
61
+
62
+
63
 
 
 
 
 
 
 
 
64
 
 
 
65
 
66
 
67
 
 
85
 
86
 
87
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
88
 
 
 
 
89
 
90
+
91
+
92
+
93
+
94
+
95
+
96
+
97
+
98
+
99
+ import os
100
+ import numpy as np
101
+ import gradio as gr
102
+ import tensorflow as tf
103
 
104
  def classify_image(image):
105
+ print(type(image))
106
+ print(image)
107
+ print(np.array(image).shape)
108
  if len(np.array(image).shape) == 3:
109
  image = tf.image.rgb_to_grayscale(image)
110
  image_tensor = tf.convert_to_tensor(image)
 
124
  example_list = [["examples/" + example] for example in os.listdir("examples")]
125
 
126
  interface = gr.Interface(fn=classify_image,
127
+ # inputs=gr.Image(type="pil"),
128
+ inputs='sketchpad',
129
  outputs=gr.Label(num_top_classes=3, label="Predictions"),
130
  examples=example_list,
131
  title=title,