JamesJayamuni
commited on
Upload folder using huggingface_hub
Browse files- .ipynb_checkpoints/app-checkpoint.py +57 -0
- app.py +57 -165
- requirements.txt +5 -5
.ipynb_checkpoints/app-checkpoint.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Assuming you have already defined img_height, img_width, and class_names
|
9 |
+
# img_height, img_width = 180, 180
|
10 |
+
class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
|
11 |
+
|
12 |
+
# Load the fine-tuned model (from local)
|
13 |
+
resnet_model = tf.keras.models.load_model('./flower_image_classification_ResNet50_v1.0.h5')
|
14 |
+
|
15 |
+
def preprocess_image(image):
|
16 |
+
# Convert the PIL image to an array
|
17 |
+
image = np.array(image)
|
18 |
+
|
19 |
+
# Read and resize the image
|
20 |
+
image_resized = cv2.resize(image, (img_height, img_width))
|
21 |
+
|
22 |
+
# Preprocess the image
|
23 |
+
image = np.expand_dims(image_resized, axis=0)
|
24 |
+
|
25 |
+
# Predict with the model
|
26 |
+
pred = resnet_model.predict(image)
|
27 |
+
|
28 |
+
# Get the predicted class label
|
29 |
+
predicted_class = np.argmax(pred)
|
30 |
+
output_class = class_names[predicted_class]
|
31 |
+
|
32 |
+
# Get the confidence level (probability)
|
33 |
+
confidence_level = pred[0][predicted_class]
|
34 |
+
|
35 |
+
return image_resized, output_class, confidence_level
|
36 |
+
|
37 |
+
def predict(image):
|
38 |
+
image_resized, output_class, confidence_level = preprocess_image(image)
|
39 |
+
return Image.fromarray(image_resized), output_class, str(confidence_level)
|
40 |
+
|
41 |
+
# Define the Gradio interface
|
42 |
+
inputs = gr.Image(type="pil", label="Upload Image")
|
43 |
+
outputs = [
|
44 |
+
gr.Image(type="pil", label="Resized Image"),
|
45 |
+
gr.Textbox(label="Predicted Class"),
|
46 |
+
gr.Textbox(label="Confidence Level")
|
47 |
+
]
|
48 |
+
|
49 |
+
# Create the Gradio Interface
|
50 |
+
gr.Interface(
|
51 |
+
fn=predict,
|
52 |
+
inputs=inputs,
|
53 |
+
outputs=outputs,
|
54 |
+
title="Flower Classification with ResNet50",
|
55 |
+
description="Upload an image of a flower to classify it into one of the five categories.",
|
56 |
+
live=True
|
57 |
+
).launch()
|
app.py
CHANGED
@@ -1,165 +1,57 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
"text": [
|
59 |
-
"Traceback (most recent call last):\n",
|
60 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\gradio\\queueing.py\", line 522, in process_events\n",
|
61 |
-
" response = await route_utils.call_process_api(\n",
|
62 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\gradio\\route_utils.py\", line 260, in call_process_api\n",
|
63 |
-
" output = await app.get_blocks().process_api(\n",
|
64 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\gradio\\blocks.py\", line 1741, in process_api\n",
|
65 |
-
" result = await self.call_function(\n",
|
66 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\gradio\\blocks.py\", line 1296, in call_function\n",
|
67 |
-
" prediction = await anyio.to_thread.run_sync(\n",
|
68 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\anyio\\to_thread.py\", line 56, in run_sync\n",
|
69 |
-
" return await get_async_backend().run_sync_in_worker_thread(\n",
|
70 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 2134, in run_sync_in_worker_thread\n",
|
71 |
-
" return await future\n",
|
72 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\anyio\\_backends\\_asyncio.py\", line 851, in run\n",
|
73 |
-
" result = context.run(func, *args)\n",
|
74 |
-
" File \"C:\\Users\\ASUSS\\anaconda3\\envs\\bootcampai\\lib\\site-packages\\gradio\\utils.py\", line 751, in wrapper\n",
|
75 |
-
" response = f(*args, **kwargs)\n",
|
76 |
-
" File \"C:\\Users\\ASUSS\\AppData\\Local\\Temp\\ipykernel_3748\\1829143819.py\", line 37, in predict\n",
|
77 |
-
" image_resized, output_class, confidence_level = preprocess_image(image)\n",
|
78 |
-
" File \"C:\\Users\\ASUSS\\AppData\\Local\\Temp\\ipykernel_3748\\1829143819.py\", line 19, in preprocess_image\n",
|
79 |
-
" image_resized = cv2.resize(image, (img_height, img_width))\n",
|
80 |
-
"NameError: name 'img_height' is not defined\n"
|
81 |
-
]
|
82 |
-
}
|
83 |
-
],
|
84 |
-
"source": [
|
85 |
-
"import gradio as gr\n",
|
86 |
-
"import cv2\n",
|
87 |
-
"import numpy as np\n",
|
88 |
-
"import tensorflow as tf\n",
|
89 |
-
"from PIL import Image\n",
|
90 |
-
"\n",
|
91 |
-
"# Assuming you have already defined img_height, img_width, and class_names\n",
|
92 |
-
"# img_height, img_width = 180, 180\n",
|
93 |
-
"class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']\n",
|
94 |
-
"\n",
|
95 |
-
"# Load the fine-tuned model (from local)\n",
|
96 |
-
"resnet_model = tf.keras.models.load_model('./flower_image_classification_ResNet50_v1.0.h5')\n",
|
97 |
-
"\n",
|
98 |
-
"def preprocess_image(image):\n",
|
99 |
-
" # Convert the PIL image to an array\n",
|
100 |
-
" image = np.array(image)\n",
|
101 |
-
" \n",
|
102 |
-
" # Read and resize the image\n",
|
103 |
-
" image_resized = cv2.resize(image, (img_height, img_width))\n",
|
104 |
-
" \n",
|
105 |
-
" # Preprocess the image\n",
|
106 |
-
" image = np.expand_dims(image_resized, axis=0)\n",
|
107 |
-
" \n",
|
108 |
-
" # Predict with the model\n",
|
109 |
-
" pred = resnet_model.predict(image)\n",
|
110 |
-
" \n",
|
111 |
-
" # Get the predicted class label\n",
|
112 |
-
" predicted_class = np.argmax(pred)\n",
|
113 |
-
" output_class = class_names[predicted_class]\n",
|
114 |
-
" \n",
|
115 |
-
" # Get the confidence level (probability)\n",
|
116 |
-
" confidence_level = pred[0][predicted_class]\n",
|
117 |
-
" \n",
|
118 |
-
" return image_resized, output_class, confidence_level\n",
|
119 |
-
"\n",
|
120 |
-
"def predict(image):\n",
|
121 |
-
" image_resized, output_class, confidence_level = preprocess_image(image)\n",
|
122 |
-
" return Image.fromarray(image_resized), output_class, str(confidence_level)\n",
|
123 |
-
"\n",
|
124 |
-
"# Define the Gradio interface\n",
|
125 |
-
"inputs = gr.Image(type=\"pil\", label=\"Upload Image\")\n",
|
126 |
-
"outputs = [\n",
|
127 |
-
" gr.Image(type=\"pil\", label=\"Resized Image\"),\n",
|
128 |
-
" gr.Textbox(label=\"Predicted Class\"),\n",
|
129 |
-
" gr.Textbox(label=\"Confidence Level\")\n",
|
130 |
-
"]\n",
|
131 |
-
"\n",
|
132 |
-
"# Create the Gradio Interface\n",
|
133 |
-
"gr.Interface(\n",
|
134 |
-
" fn=predict,\n",
|
135 |
-
" inputs=inputs,\n",
|
136 |
-
" outputs=outputs,\n",
|
137 |
-
" title=\"Flower Classification with ResNet50\",\n",
|
138 |
-
" description=\"Upload an image of a flower to classify it into one of the five categories.\",\n",
|
139 |
-
" live=True\n",
|
140 |
-
").launch()\n"
|
141 |
-
]
|
142 |
-
}
|
143 |
-
],
|
144 |
-
"metadata": {
|
145 |
-
"kernelspec": {
|
146 |
-
"display_name": "Python 3 (ipykernel)",
|
147 |
-
"language": "python",
|
148 |
-
"name": "python3"
|
149 |
-
},
|
150 |
-
"language_info": {
|
151 |
-
"codemirror_mode": {
|
152 |
-
"name": "ipython",
|
153 |
-
"version": 3
|
154 |
-
},
|
155 |
-
"file_extension": ".py",
|
156 |
-
"mimetype": "text/x-python",
|
157 |
-
"name": "python",
|
158 |
-
"nbconvert_exporter": "python",
|
159 |
-
"pygments_lexer": "ipython3",
|
160 |
-
"version": "3.10.12"
|
161 |
-
}
|
162 |
-
},
|
163 |
-
"nbformat": 4,
|
164 |
-
"nbformat_minor": 5
|
165 |
-
}
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import cv2
|
3 |
+
import numpy as np
|
4 |
+
import tensorflow as tf
|
5 |
+
|
6 |
+
from PIL import Image
|
7 |
+
|
8 |
+
# Assuming you have already defined img_height, img_width, and class_names
|
9 |
+
# img_height, img_width = 180, 180
|
10 |
+
class_names = ['daisy', 'dandelion', 'roses', 'sunflowers', 'tulips']
|
11 |
+
|
12 |
+
# Load the fine-tuned model (from local)
|
13 |
+
resnet_model = tf.keras.models.load_model('./flower_image_classification_ResNet50_v1.0.h5')
|
14 |
+
|
15 |
+
def preprocess_image(image):
|
16 |
+
# Convert the PIL image to an array
|
17 |
+
image = np.array(image)
|
18 |
+
|
19 |
+
# Read and resize the image
|
20 |
+
image_resized = cv2.resize(image, (img_height, img_width))
|
21 |
+
|
22 |
+
# Preprocess the image
|
23 |
+
image = np.expand_dims(image_resized, axis=0)
|
24 |
+
|
25 |
+
# Predict with the model
|
26 |
+
pred = resnet_model.predict(image)
|
27 |
+
|
28 |
+
# Get the predicted class label
|
29 |
+
predicted_class = np.argmax(pred)
|
30 |
+
output_class = class_names[predicted_class]
|
31 |
+
|
32 |
+
# Get the confidence level (probability)
|
33 |
+
confidence_level = pred[0][predicted_class]
|
34 |
+
|
35 |
+
return image_resized, output_class, confidence_level
|
36 |
+
|
37 |
+
def predict(image):
|
38 |
+
image_resized, output_class, confidence_level = preprocess_image(image)
|
39 |
+
return Image.fromarray(image_resized), output_class, str(confidence_level)
|
40 |
+
|
41 |
+
# Define the Gradio interface
|
42 |
+
inputs = gr.Image(type="pil", label="Upload Image")
|
43 |
+
outputs = [
|
44 |
+
gr.Image(type="pil", label="Resized Image"),
|
45 |
+
gr.Textbox(label="Predicted Class"),
|
46 |
+
gr.Textbox(label="Confidence Level")
|
47 |
+
]
|
48 |
+
|
49 |
+
# Create the Gradio Interface
|
50 |
+
gr.Interface(
|
51 |
+
fn=predict,
|
52 |
+
inputs=inputs,
|
53 |
+
outputs=outputs,
|
54 |
+
title="Flower Classification with ResNet50",
|
55 |
+
description="Upload an image of a flower to classify it into one of the five categories.",
|
56 |
+
live=True
|
57 |
+
).launch()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
requirements.txt
CHANGED
@@ -1,6 +1,6 @@
|
|
1 |
-
# Python 3.10.12
|
2 |
-
gradio==4.25.0
|
3 |
-
opencv-python==4.10.0
|
4 |
-
numpy==1.26.4
|
5 |
-
tensorflow==2.16.1
|
6 |
pillow==10.3.0
|
|
|
1 |
+
# Python 3.10.12
|
2 |
+
gradio==4.25.0
|
3 |
+
opencv-python==4.10.0
|
4 |
+
numpy==1.26.4
|
5 |
+
tensorflow==2.16.1
|
6 |
pillow==10.3.0
|