hitty commited on
Commit
5e6e1a6
·
1 Parent(s): fca4368

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +33 -259
app.py CHANGED
@@ -1,273 +1,47 @@
 
 
 
 
 
1
 
 
 
2
 
3
- # import tensorflow as tf
4
- # import tensorflow_hub as hub
5
-
6
- # import requests
7
- # from PIL import Image
8
- # from io import BytesIO
9
-
10
- # import matplotlib.pyplot as plt
11
- # import numpy as np
12
- # import gradio as gr
13
-
14
- # #@title Helper functions for loading image (hidden)
15
-
16
- # original_image_cache = {}
17
-
18
- # def preprocess_image(image):
19
- # image = np.array(image)
20
- # # reshape into shape [batch_size, height, width, num_channels]
21
- # img_reshaped = tf.reshape(image, [1, image.shape[0], image.shape[1], image.shape[2]])
22
- # # Use `convert_image_dtype` to convert to floats in the [0,1] range.
23
- # image = tf.image.convert_image_dtype(img_reshaped, tf.float32)
24
- # return image
25
-
26
- # def load_image_from_url(img_url):
27
- # """Returns an image with shape [1, height, width, num_channels]."""
28
- # user_agent = {'User-agent': 'Colab Sample (https://tensorflow.org)'}
29
- # response = requests.get(img_url, headers=user_agent)
30
- # image = Image.open(BytesIO(response.content))
31
- # image = preprocess_image(image)
32
- # return image
33
-
34
- # def load_image(image_url, image_size=256, dynamic_size=False, max_dynamic_size=512):
35
- # """Loads and preprocesses images."""
36
- # # Cache image file locally.
37
- # if image_url in original_image_cache:
38
- # img = original_image_cache[image_url]
39
- # elif image_url.startswith('https://'):
40
- # img = load_image_from_url(image_url)
41
- # else:
42
- # fd = tf.io.gfile.GFile(image_url, 'rb')
43
- # img = preprocess_image(Image.open(fd))
44
- # original_image_cache[image_url] = img
45
- # # Load and convert to float32 numpy array, add batch dimension, and normalize to range [0, 1].
46
- # img_raw = img
47
- # if tf.reduce_max(img) > 1.0:
48
- # img = img / 255.
49
- # if len(img.shape) == 3:
50
- # img = tf.stack([img, img, img], axis=-1)
51
- # if not dynamic_size:
52
- # img = tf.image.resize_with_pad(img, image_size, image_size)
53
- # elif img.shape[1] > max_dynamic_size or img.shape[2] > max_dynamic_size:
54
- # img = tf.image.resize_with_pad(img, max_dynamic_size, max_dynamic_size)
55
- # return img, img_raw
56
-
57
-
58
-
59
- # image_size = 224
60
- # dynamic_size = False
61
-
62
- # model_name = "efficientnet_b0"
63
-
64
- # model_handle_map = {
65
- # "efficientnetv2-s": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_s/classification/2",
66
- # "efficientnetv2-m": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_m/classification/2",
67
- # "efficientnetv2-l": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_l/classification/2",
68
- # "efficientnetv2-s-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_s/classification/2",
69
- # "efficientnetv2-m-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_m/classification/2",
70
- # "efficientnetv2-l-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_l/classification/2",
71
- # "efficientnetv2-xl-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_xl/classification/2",
72
- # "efficientnetv2-b0-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b0/classification/2",
73
- # "efficientnetv2-b1-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b1/classification/2",
74
- # "efficientnetv2-b2-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b2/classification/2",
75
- # "efficientnetv2-b3-21k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_b3/classification/2",
76
- # "efficientnetv2-s-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_s/classification/2",
77
- # "efficientnetv2-m-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_m/classification/2",
78
- # "efficientnetv2-l-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_l/classification/2",
79
- # "efficientnetv2-xl-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_xl/classification/2",
80
- # "efficientnetv2-b0-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b0/classification/2",
81
- # "efficientnetv2-b1-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b1/classification/2",
82
- # "efficientnetv2-b2-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b2/classification/2",
83
- # "efficientnetv2-b3-21k-ft1k": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet21k_ft1k_b3/classification/2",
84
- # "efficientnetv2-b0": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b0/classification/2",
85
- # "efficientnetv2-b1": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b1/classification/2",
86
- # "efficientnetv2-b2": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b2/classification/2",
87
- # "efficientnetv2-b3": "https://tfhub.dev/google/imagenet/efficientnet_v2_imagenet1k_b3/classification/2",
88
- # "efficientnet_b0": "https://tfhub.dev/tensorflow/efficientnet/b0/classification/1",
89
- # "efficientnet_b1": "https://tfhub.dev/tensorflow/efficientnet/b1/classification/1",
90
- # "efficientnet_b2": "https://tfhub.dev/tensorflow/efficientnet/b2/classification/1",
91
- # "efficientnet_b3": "https://tfhub.dev/tensorflow/efficientnet/b3/classification/1",
92
- # "efficientnet_b4": "https://tfhub.dev/tensorflow/efficientnet/b4/classification/1",
93
- # "efficientnet_b5": "https://tfhub.dev/tensorflow/efficientnet/b5/classification/1",
94
- # "efficientnet_b6": "https://tfhub.dev/tensorflow/efficientnet/b6/classification/1",
95
- # "efficientnet_b7": "https://tfhub.dev/tensorflow/efficientnet/b7/classification/1",
96
- # "bit_s-r50x1": "https://tfhub.dev/google/bit/s-r50x1/ilsvrc2012_classification/1",
97
- # "inception_v3": "https://tfhub.dev/google/imagenet/inception_v3/classification/4",
98
- # "inception_resnet_v2": "https://tfhub.dev/google/imagenet/inception_resnet_v2/classification/4",
99
- # "resnet_v1_50": "https://tfhub.dev/google/imagenet/resnet_v1_50/classification/4",
100
- # "resnet_v1_101": "https://tfhub.dev/google/imagenet/resnet_v1_101/classification/4",
101
- # "resnet_v1_152": "https://tfhub.dev/google/imagenet/resnet_v1_152/classification/4",
102
- # "resnet_v2_50": "https://tfhub.dev/google/imagenet/resnet_v2_50/classification/4",
103
- # "resnet_v2_101": "https://tfhub.dev/google/imagenet/resnet_v2_101/classification/4",
104
- # "resnet_v2_152": "https://tfhub.dev/google/imagenet/resnet_v2_152/classification/4",
105
- # "nasnet_large": "https://tfhub.dev/google/imagenet/nasnet_large/classification/4",
106
- # "nasnet_mobile": "https://tfhub.dev/google/imagenet/nasnet_mobile/classification/4",
107
- # "pnasnet_large": "https://tfhub.dev/google/imagenet/pnasnet_large/classification/4",
108
- # "mobilenet_v2_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/4",
109
- # "mobilenet_v2_130_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_130_224/classification/4",
110
- # "mobilenet_v2_140_224": "https://tfhub.dev/google/imagenet/mobilenet_v2_140_224/classification/4",
111
- # "mobilenet_v3_small_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_100_224/classification/5",
112
- # "mobilenet_v3_small_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_small_075_224/classification/5",
113
- # "mobilenet_v3_large_100_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_100_224/classification/5",
114
- # "mobilenet_v3_large_075_224": "https://tfhub.dev/google/imagenet/mobilenet_v3_large_075_224/classification/5",
115
- # }
116
-
117
- # model_image_size_map = {
118
- # "efficientnetv2-s": 384,
119
- # "efficientnetv2-m": 480,
120
- # "efficientnetv2-l": 480,
121
- # "efficientnetv2-b0": 224,
122
- # "efficientnetv2-b1": 240,
123
- # "efficientnetv2-b2": 260,
124
- # "efficientnetv2-b3": 300,
125
- # "efficientnetv2-s-21k": 384,
126
- # "efficientnetv2-m-21k": 480,
127
- # "efficientnetv2-l-21k": 480,
128
- # "efficientnetv2-xl-21k": 512,
129
- # "efficientnetv2-b0-21k": 224,
130
- # "efficientnetv2-b1-21k": 240,
131
- # "efficientnetv2-b2-21k": 260,
132
- # "efficientnetv2-b3-21k": 300,
133
- # "efficientnetv2-s-21k-ft1k": 384,
134
- # "efficientnetv2-m-21k-ft1k": 480,
135
- # "efficientnetv2-l-21k-ft1k": 480,
136
- # "efficientnetv2-xl-21k-ft1k": 512,
137
- # "efficientnetv2-b0-21k-ft1k": 224,
138
- # "efficientnetv2-b1-21k-ft1k": 240,
139
- # "efficientnetv2-b2-21k-ft1k": 260,
140
- # "efficientnetv2-b3-21k-ft1k": 300,
141
- # "efficientnet_b0": 224,
142
- # "efficientnet_b1": 240,
143
- # "efficientnet_b2": 260,
144
- # "efficientnet_b3": 300,
145
- # "efficientnet_b4": 380,
146
- # "efficientnet_b5": 456,
147
- # "efficientnet_b6": 528,
148
- # "efficientnet_b7": 600,
149
- # "inception_v3": 299,
150
- # "inception_resnet_v2": 299,
151
- # "mobilenet_v2_100_224": 224,
152
- # "mobilenet_v2_130_224": 224,
153
- # "mobilenet_v2_140_224": 224,
154
- # "nasnet_large": 331,
155
- # "nasnet_mobile": 224,
156
- # "pnasnet_large": 331,
157
- # "resnet_v1_50": 224,
158
- # "resnet_v1_101": 224,
159
- # "resnet_v1_152": 224,
160
- # "resnet_v2_50": 224,
161
- # "resnet_v2_101": 224,
162
- # "resnet_v2_152": 224,
163
- # "mobilenet_v3_small_100_224": 224,
164
- # "mobilenet_v3_small_075_224": 224,
165
- # "mobilenet_v3_large_100_224": 224,
166
- # "mobilenet_v3_large_075_224": 224,
167
- # }
168
-
169
- # model_handle = model_handle_map[model_name]
170
-
171
-
172
- # max_dynamic_size = 512
173
- # if model_name in model_image_size_map:
174
- # image_size = model_image_size_map[model_name]
175
- # dynamic_size = False
176
- # print(f"Images will be converted to {image_size}x{image_size}")
177
- # else:
178
- # dynamic_size = True
179
- # print(f"Images will be capped to a max size of {max_dynamic_size}x{max_dynamic_size}")
180
-
181
- # labels_file = "https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt"
182
-
183
- # #download labels and creates a maps
184
- # downloaded_file = tf.keras.utils.get_file("labels.txt", origin=labels_file)
185
-
186
- # classes = []
187
-
188
- # with open(downloaded_file) as f:
189
- # labels = f.readlines()
190
- # classes = [l.strip() for l in labels]
191
-
192
-
193
- # classifier = hub.load(model_handle)
194
-
195
-
196
- # def inference(img):
197
- # image, original_image = load_image(img, image_size, dynamic_size, max_dynamic_size)
198
-
199
-
200
- # input_shape = image.shape
201
- # warmup_input = tf.random.uniform(input_shape, 0, 1.0)
202
- # warmup_logits = classifier(warmup_input).numpy()
203
-
204
- # # Run model on image
205
- # probabilities = tf.nn.softmax(classifier(image)).numpy()
206
-
207
- # top_5 = tf.argsort(probabilities, axis=-1, direction="DESCENDING")[0][:5].numpy()
208
- # np_classes = np.array(classes)
209
-
210
- # # Some models include an additional 'background' class in the predictions, so
211
- # # we must account for this when reading the class labels.
212
- # includes_background_class = probabilities.shape[1] == 1001
213
- # result = {}
214
- # for i, item in enumerate(top_5):
215
- # class_index = item if includes_background_class else item + 1
216
- # line = f'({i+1}) {class_index:4} - {classes[class_index]}: {probabilities[0][top_5][i]}'
217
- # result[classes[class_index]] = probabilities[0][top_5][i].item()
218
- # return result
219
-
220
- # title="efficientnet_b0"
221
- # description="Gradio Demo for efficientnet_b0: Imagenet (ILSVRC-2012-CLS) classification with EfficientNet-B0. To use it, simply upload your image or click on one of the examples to load them. Read more at the links below"
222
-
223
- # article = "<p style='text-align: center'><a href='https://tfhub.dev/google/efficientnet/b0/classification/1' target='_blank'>Tensorflow Hub</a></p>"
224
- # # examples=[['apple1.jpg']]
225
-
226
- # gr.Interface(inference,gr.inputs.Image(type="filepath"),"label",title=title,description=description,article=article).launch(enable_queue=True)
227
-
228
-
229
-
230
-
231
-
232
-
233
-
234
-
235
-
236
-
237
 
 
 
 
 
238
 
 
 
239
 
240
- import gradio as gr
241
- from PIL import Image
242
- import tensorflow as tf
243
- from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
244
 
245
- # Load pre-trained model
246
- model = tf.keras.applications.ResNet50(weights='imagenet')
247
-
248
- # Define prediction function
249
- def classify_image(image):
250
  # Preprocess image
251
- img_array = tf.keras.preprocessing.image.img_to_array(image)
 
252
  img_array = tf.expand_dims(img_array, 0)
253
  img_array = preprocess_input(img_array)
254
 
255
  # Make prediction
256
  predictions = model.predict(img_array)
257
- decoded_predictions = decode_predictions(predictions, top=3)[0]
 
 
 
258
 
259
- # Format predictions
260
- results = []
261
  for pred in decoded_predictions:
262
- label = pred[1]
263
- confidence = pred[2] * 100
264
- results.append(f'Class: {label}, Confidence: {confidence:.2f}%')
265
-
266
- return results
267
-
268
- # Create image classification interface
269
- image_input = gr.inputs.Image()
270
- label_output = gr.outputs.Textbox()
271
-
272
- interface = gr.Interface(fn=classify_image, inputs=image_input, outputs=label_output, title='Image Classification App')
273
- interface.launch()
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import tensorflow as tf
4
+ from tensorflow.keras.applications.resnet50 import preprocess_input
5
+ from tensorflow.keras.preprocessing.image import img_to_array, load_img
6
 
7
+ # Load pre-trained model
8
+ model = tf.keras.models.load_model('./final_model.h5')
9
 
10
+ # Set page title and layout
11
+ st.title('Image Classification App')
12
+ st.markdown("""
13
+ ### Upload an image and let the model classify it!
14
+ """)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
15
 
16
+ # Define vegetable names
17
+ vegetable_names = ['Bean', 'Bitter Gourd', 'Bottle Gourd', 'Brinjal', 'Broccoli', 'Cabbage',
18
+ 'Capsicum', 'Carrot', 'Cauliflower', 'Cucumber', 'Papaya', 'Potato', 'Pumpkin',
19
+ 'Radish', 'Tomato']
20
 
21
+ # File uploader
22
+ uploaded_file = st.file_uploader('Choose an image', type=['jpg', 'jpeg', 'png'])
23
 
24
+ if uploaded_file is not None:
25
+ # Display uploaded image
26
+ image = Image.open(uploaded_file)
27
+ st.image(image, caption='Uploaded Image', use_column_width=True)
28
 
 
 
 
 
 
29
  # Preprocess image
30
+ image = load_img(uploaded_file, target_size=(224, 224))
31
+ img_array = img_to_array(image)
32
  img_array = tf.expand_dims(img_array, 0)
33
  img_array = preprocess_input(img_array)
34
 
35
  # Make prediction
36
  predictions = model.predict(img_array)
37
+ class_indices = tf.argmax(predictions, axis=1).numpy()
38
+ class_names = [vegetable_names[i] for i in class_indices]
39
+ confidence_values = tf.reduce_max(predictions, axis=1).numpy() * 100
40
+ decoded_predictions = list(zip(class_names, confidence_values))
41
 
42
+ # Display predictions
43
+ st.subheader('Predictions')
44
  for pred in decoded_predictions:
45
+ label = pred[0]
46
+ confidence = pred[1]
47
+ st.write(f'Class: {label}, Confidence: {confidence:.2f}%')