Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -128,127 +128,6 @@ def preprocess_image(image):
|
|
128 |
def get_akc_breeds_link():
|
129 |
return "https://www.akc.org/dog-breeds/"
|
130 |
|
131 |
-
# def predict(image):
|
132 |
-
# if image is None:
|
133 |
-
# return "Please upload an image to get started.", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
134 |
-
|
135 |
-
# try:
|
136 |
-
# image_tensor = preprocess_image(image)
|
137 |
-
# with torch.no_grad():
|
138 |
-
# output = model(image_tensor)
|
139 |
-
# logits = output[0] if isinstance(output, tuple) else output
|
140 |
-
|
141 |
-
# probabilities = F.softmax(logits, dim=1)
|
142 |
-
# topk_probs, topk_indices = torch.topk(probabilities, k=3)
|
143 |
-
|
144 |
-
# top1_prob = topk_probs[0][0].item()
|
145 |
-
# topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
|
146 |
-
# topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
|
147 |
-
|
148 |
-
# if top1_prob >= 0.5:
|
149 |
-
# breed = topk_breeds[0]
|
150 |
-
# description = get_dog_description(breed)
|
151 |
-
# return format_description(description, breed), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
152 |
-
|
153 |
-
# elif top1_prob < 0.2:
|
154 |
-
# return ("The image is too unclear or the dog breed is not in the dataset. Please upload a clearer image of the dog.",
|
155 |
-
# gr.update(visible=False), gr.update(visible=False), gr.update(visible=False))
|
156 |
-
# else:
|
157 |
-
# explanation = (
|
158 |
-
# f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
159 |
-
# f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
160 |
-
# f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
161 |
-
# f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
162 |
-
# "Click on a button to view more information about the breed."
|
163 |
-
# )
|
164 |
-
# return explanation, gr.update(visible=True, value=f"More about {topk_breeds[0]}"), gr.update(visible=True, value=f"More about {topk_breeds[1]}"), gr.update(visible=True, value=f"More about {topk_breeds[2]}")
|
165 |
-
|
166 |
-
# except Exception as e:
|
167 |
-
# return f"An error occurred: {e}", gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
168 |
-
|
169 |
-
|
170 |
-
# def format_description(description, breed):
|
171 |
-
# if isinstance(description, dict):
|
172 |
-
# formatted_description = "\n\n".join([f"**{key}**: {value}" for key, value in description.items()])
|
173 |
-
# else:
|
174 |
-
# formatted_description = description
|
175 |
-
|
176 |
-
# akc_link = get_akc_breeds_link()
|
177 |
-
# formatted_description += f"\n\n**Want to learn more about dog breeds?** [Visit the AKC dog breeds page]({akc_link}) and search for {breed} to find detailed information."
|
178 |
-
|
179 |
-
# disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
|
180 |
-
# "You may need to search for the specific breed on that page. "
|
181 |
-
# "I am not responsible for the content on external sites. "
|
182 |
-
# "Please refer to the AKC's terms of use and privacy policy.*")
|
183 |
-
# formatted_description += disclaimer
|
184 |
-
|
185 |
-
# return formatted_description
|
186 |
-
|
187 |
-
# def show_details(breed):
|
188 |
-
# breed_name = breed.split("More about ")[-1]
|
189 |
-
# description = get_dog_description(breed_name)
|
190 |
-
# return format_description(description, breed_name)
|
191 |
-
|
192 |
-
# with gr.Blocks(css="""
|
193 |
-
# .container {
|
194 |
-
# max-width: 900px;
|
195 |
-
# margin: 0 auto;
|
196 |
-
# padding: 20px;
|
197 |
-
# background-color: rgba(255, 255, 255, 0.9);
|
198 |
-
# border-radius: 15px;
|
199 |
-
# box-shadow: 0 0 20px rgba(0, 0, 0, 0.1);
|
200 |
-
# }
|
201 |
-
# .gr-form { display: flex; flex-direction: column; align-items: center; }
|
202 |
-
# .gr-box { width: 100%; max-width: 500px; }
|
203 |
-
# .output-markdown, .output-image {
|
204 |
-
# margin-top: 20px;
|
205 |
-
# padding: 15px;
|
206 |
-
# background-color: #f5f5f5;
|
207 |
-
# border-radius: 10px;
|
208 |
-
# }
|
209 |
-
# .examples {
|
210 |
-
# display: flex;
|
211 |
-
# justify-content: center;
|
212 |
-
# flex-wrap: wrap;
|
213 |
-
# gap: 10px;
|
214 |
-
# margin-top: 20px;
|
215 |
-
# }
|
216 |
-
# .examples img {
|
217 |
-
# width: 100px;
|
218 |
-
# height: 100px;
|
219 |
-
# object-fit: cover;
|
220 |
-
# }
|
221 |
-
# """) as iface:
|
222 |
-
|
223 |
-
# gr.HTML("<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
224 |
-
# gr.HTML("<p style='font-family:Open Sans; color:#34495E; text-align:center;'>Upload a picture of a dog, and the model will predict its breed, provide detailed information, and include an extra information link!</p>")
|
225 |
-
|
226 |
-
# with gr.Row():
|
227 |
-
# input_image = gr.Image(label="Upload a dog image", type="numpy")
|
228 |
-
# output = gr.Markdown(label="Prediction Results")
|
229 |
-
|
230 |
-
# with gr.Row():
|
231 |
-
# btn1 = gr.Button("View More 1", visible=False)
|
232 |
-
# btn2 = gr.Button("View More 2", visible=False)
|
233 |
-
# btn3 = gr.Button("View More 3", visible=False)
|
234 |
-
|
235 |
-
# input_image.change(predict, inputs=input_image, outputs=[output, btn1, btn2, btn3])
|
236 |
-
|
237 |
-
# btn1.click(show_details, inputs=btn1, outputs=output)
|
238 |
-
# btn2.click(show_details, inputs=btn2, outputs=output)
|
239 |
-
# btn3.click(show_details, inputs=btn3, outputs=output)
|
240 |
-
|
241 |
-
# gr.Examples(
|
242 |
-
# examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
243 |
-
# inputs=input_image
|
244 |
-
# )
|
245 |
-
|
246 |
-
# gr.HTML('For more details on this project and other work, feel free to visit my GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog%20Breed%20Classifier">Dog Breed Classifier</a>')
|
247 |
-
|
248 |
-
# # launch the program
|
249 |
-
# if __name__ == "__main__":
|
250 |
-
# iface.launch()
|
251 |
-
|
252 |
|
253 |
def format_description(description, breed):
|
254 |
if isinstance(description, dict):
|
@@ -284,7 +163,7 @@ def _predict_single_dog(image):
|
|
284 |
return top1_prob, topk_breeds, topk_probs_percent
|
285 |
|
286 |
|
287 |
-
async def detect_multiple_dogs(image, conf_threshold=0.
|
288 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
289 |
dogs = []
|
290 |
for box in results.boxes:
|
@@ -304,7 +183,7 @@ async def predict(image):
|
|
304 |
if isinstance(image, np.ndarray):
|
305 |
image = Image.fromarray(image)
|
306 |
|
307 |
-
dogs = await detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.
|
308 |
|
309 |
if len(dogs) <= 1:
|
310 |
return await process_single_dog(image)
|
@@ -362,6 +241,7 @@ async def predict(image):
|
|
362 |
error_msg = f"An error occurred: {str(e)}"
|
363 |
print(error_msg) # 添加日誌輸出
|
364 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
|
|
365 |
|
366 |
async def process_single_dog(image):
|
367 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
|
|
128 |
def get_akc_breeds_link():
|
129 |
return "https://www.akc.org/dog-breeds/"
|
130 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
131 |
|
132 |
def format_description(description, breed):
|
133 |
if isinstance(description, dict):
|
|
|
163 |
return top1_prob, topk_breeds, topk_probs_percent
|
164 |
|
165 |
|
166 |
+
async def detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.3):
|
167 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
168 |
dogs = []
|
169 |
for box in results.boxes:
|
|
|
183 |
if isinstance(image, np.ndarray):
|
184 |
image = Image.fromarray(image)
|
185 |
|
186 |
+
dogs = await detect_multiple_dogs(image, conf_threshold=0.25, iou_threshold=0.3)
|
187 |
|
188 |
if len(dogs) <= 1:
|
189 |
return await process_single_dog(image)
|
|
|
241 |
error_msg = f"An error occurred: {str(e)}"
|
242 |
print(error_msg) # 添加日誌輸出
|
243 |
return error_msg, None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
244 |
+
|
245 |
|
246 |
async def process_single_dog(image):
|
247 |
top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|