DawnC commited on
Commit
49a13b2
·
1 Parent(s): b7618a5

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +1 -225
app.py CHANGED
@@ -132,19 +132,6 @@ def get_akc_breeds_link(breed):
132
  return f"{base_url}{breed_url}/"
133
 
134
 
135
- # async def predict_single_dog(image):
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
- # probabilities = F.softmax(logits, dim=1)
141
- # topk_probs, topk_indices = torch.topk(probabilities, k=3)
142
- # top1_prob = topk_probs[0][0].item()
143
- # topk_breeds = [dog_breeds[idx.item()] for idx in topk_indices[0]]
144
- # topk_probs_percent = [f"{prob.item() * 100:.2f}%" for prob in topk_probs[0]]
145
- # return top1_prob, topk_breeds, topk_probs_percent
146
-
147
-
148
  async def predict_single_dog(image):
149
  image_tensor = preprocess_image(image)
150
  with torch.no_grad():
@@ -200,6 +187,7 @@ def non_max_suppression(boxes, iou_threshold):
200
  boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
201
  return keep
202
 
 
203
  def calculate_iou(box1, box2):
204
  x1 = max(box1[0], box2[0])
205
  y1 = max(box1[1], box2[1])
@@ -214,160 +202,6 @@ def calculate_iou(box1, box2):
214
  return iou
215
 
216
 
217
-
218
- # async def process_single_dog(image):
219
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
220
- # if top1_prob < 0.15:
221
- # initial_state = {
222
- # "explanation": "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.",
223
- # "buttons": [],
224
- # "show_back": False,
225
- # "image": None,
226
- # "is_multi_dog": False
227
- # }
228
- # return initial_state["explanation"], None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
229
-
230
- # breed = topk_breeds[0]
231
- # description = get_dog_description(breed)
232
-
233
- # if top1_prob >= 0.45:
234
- # formatted_description = format_description(description, breed)
235
- # initial_state = {
236
- # "explanation": formatted_description,
237
- # "buttons": [],
238
- # "show_back": False,
239
- # "image": image,
240
- # "is_multi_dog": False
241
- # }
242
- # return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), initial_state
243
- # else:
244
- # explanation = (
245
- # f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
246
- # f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
247
- # f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
248
- # f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
249
- # "Click on a button to view more information about the breed."
250
- # )
251
- # buttons = [
252
- # gr.update(visible=True, value=f"More about {topk_breeds[0]}"),
253
- # gr.update(visible=True, value=f"More about {topk_breeds[1]}"),
254
- # gr.update(visible=True, value=f"More about {topk_breeds[2]}")
255
- # ]
256
- # initial_state = {
257
- # "explanation": explanation,
258
- # "buttons": buttons,
259
- # "show_back": True,
260
- # "image": image,
261
- # "is_multi_dog": False
262
- # }
263
- # return explanation, image, buttons[0], buttons[1], buttons[2], gr.update(visible=True), initial_state
264
-
265
-
266
- # async def predict(image):
267
- # if image is None:
268
- # return "Please upload an image to start.", None, gr.update(visible=False, choices=[]), None
269
-
270
- # try:
271
- # if isinstance(image, np.ndarray):
272
- # image = Image.fromarray(image)
273
-
274
- # dogs = await detect_multiple_dogs(image)
275
-
276
- # color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
277
- # buttons = []
278
- # annotated_image = image.copy()
279
- # draw = ImageDraw.Draw(annotated_image)
280
- # font = ImageFont.load_default()
281
-
282
- # dogs_info = ""
283
-
284
- # for i, (cropped_image, detection_confidence, box) in enumerate(dogs):
285
- # buttons_html = ""
286
- # top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
287
- # color = color_list[i % len(color_list)]
288
- # draw.rectangle(box, outline=color, width=3)
289
- # draw.text((box[0] + 5, box[1] + 5), f"Dog {i+1}", fill=color, font=font)
290
-
291
- # combined_confidence = detection_confidence * top1_prob
292
- # dogs_info += f'<div class="dog-info" style="border-left: 5px solid {color}; margin-bottom: 20px; padding: 15px;">'
293
- # dogs_info += f'<h2>Dog {i+1}</h2>'
294
-
295
- # if top1_prob >= 0.45:
296
- # breed = topk_breeds[0]
297
- # description = get_dog_description(breed)
298
- # dogs_info += format_description_html(description, breed)
299
-
300
- # elif combined_confidence >= 0.15:
301
- # dogs_info += f"<p>Top 3 possible breeds:</p><ul>"
302
- # for j, (breed, prob) in enumerate(zip(topk_breeds[:3], topk_probs_percent[:3])):
303
- # prob = float(prob.replace('%', ''))
304
- # dogs_info += f"<li><strong>{breed}</strong> ({prob:.2f}% confidence)</li>"
305
- # dogs_info += "</ul>"
306
-
307
- # for breed in topk_breeds[:3]:
308
- # button_id = f"Dog {i+1}: More about {breed}"
309
- # buttons_html += f'<button class="breed-button" onclick="handle_button_click(\'{button_id}\')">{breed}</button>'
310
- # buttons.append(button_id)
311
-
312
- # else:
313
- # dogs_info += "<p>The image is unclear or the breed is not in the dataset. Please upload a clearer image.</p>"
314
-
315
- # dogs_info += '</div>'
316
-
317
-
318
- # buttons_html = ""
319
-
320
- # html_output = f"""
321
- # <style>
322
- # .dog-info {{ border: 1px solid #ddd; margin-bottom: 20px; padding: 15px; border-radius: 5px; box-shadow: 0 2px 5px rgba(0,0,0,0.1); }}
323
- # .dog-info h2 {{ background-color: #f0f0f0; padding: 10px; margin: -15px -15px 15px -15px; border-radius: 5px 5px 0 0; }}
324
- # .breed-buttons {{ margin-top: 10px; }}
325
- # .breed-button {{ margin-right: 10px; margin-bottom: 10px; padding: 5px 10px; background-color: #4CAF50; color: white; border: none; border-radius: 3px; cursor: pointer; }}
326
- # </style>
327
- # {dogs_info}
328
- # """
329
-
330
- # if buttons:
331
- # html_output += """
332
- # <script>
333
- # function handle_button_click(button_id) {
334
- # const radio = document.querySelector('input[type=radio][value="' + button_id + '"]');
335
- # if (radio) {
336
- # radio.click();
337
- # } else {
338
- # console.error("Radio button not found:", button_id);
339
- # }
340
- # }
341
- # </script>
342
- # """
343
- # initial_state = {
344
- # "dogs_info": dogs_info,
345
- # "buttons": buttons,
346
- # "show_back": True,
347
- # "image": annotated_image,
348
- # "is_multi_dog": len(dogs) > 1,
349
- # "html_output": html_output
350
- # }
351
- # return html_output, annotated_image, gr.update(visible=True, choices=buttons), initial_state
352
- # else:
353
- # initial_state = {
354
- # "dogs_info": dogs_info,
355
- # "buttons": [],
356
- # "show_back": False,
357
- # "image": annotated_image,
358
- # "is_multi_dog": len(dogs) > 1,
359
- # "html_output": html_output
360
- # }
361
- # return html_output, annotated_image, gr.update(visible=False, choices=[]), initial_state
362
-
363
-
364
- # except Exception as e:
365
- # error_msg = f"An error occurred: {str(e)}\n\nTraceback:\n{traceback.format_exc()}"
366
- # print(error_msg)
367
- # return error_msg, None, gr.update(visible=False, choices=[]), None
368
-
369
-
370
-
371
  async def process_single_dog(image):
372
  top1_prob, topk_breeds, relative_probs = await predict_single_dog(image)
373
 
@@ -536,64 +370,6 @@ def format_description_html(description, breed):
536
  return html
537
 
538
 
539
- def go_back(state):
540
- buttons = state.get("buttons", [])
541
- return (
542
- state["html_output"],
543
- state["image"],
544
- gr.update(visible=True, choices=buttons),
545
- gr.update(visible=False),
546
- state
547
- )
548
-
549
-
550
- # with gr.Blocks() as iface:
551
- # gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
552
- # gr.HTML("<p style='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>")
553
-
554
- # with gr.Row():
555
- # input_image = gr.Image(label="Upload a dog image", type="pil")
556
- # output_image = gr.Image(label="Annotated Image")
557
-
558
- # output = gr.HTML(label="Prediction Results")
559
-
560
- # breed_buttons = gr.Radio(choices=[], label="More Information", visible=False)
561
-
562
- # back_button = gr.Button("Back", visible=False)
563
-
564
- # initial_state = gr.State()
565
-
566
- # input_image.change(
567
- # predict,
568
- # inputs=input_image,
569
- # outputs=[output, output_image, breed_buttons, initial_state]
570
- # )
571
-
572
- # breed_buttons.change(
573
- # show_details_html,
574
- # inputs=[breed_buttons, output, initial_state],
575
- # outputs=[output, back_button, initial_state]
576
- # )
577
-
578
- # back_button.click(
579
- # go_back,
580
- # inputs=[initial_state],
581
- # outputs=[output, output_image, breed_buttons, back_button, initial_state]
582
- # )
583
-
584
- # gr.Examples(
585
- # examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
586
- # inputs=input_image
587
- # )
588
-
589
- # 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_Breed_Classifier">Dog Breed Classifier</a>')
590
-
591
-
592
- # if __name__ == "__main__":
593
- # iface.launch()
594
-
595
-
596
-
597
  with gr.Blocks() as iface:
598
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
599
  gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed and provide detailed information!</p>")
 
132
  return f"{base_url}{breed_url}/"
133
 
134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  async def predict_single_dog(image):
136
  image_tensor = preprocess_image(image)
137
  with torch.no_grad():
 
187
  boxes = [box for box in boxes if calculate_iou(current[0], box[0]) < iou_threshold]
188
  return keep
189
 
190
+
191
  def calculate_iou(box1, box2):
192
  x1 = max(box1[0], box2[0])
193
  y1 = max(box1[1], box2[1])
 
202
  return iou
203
 
204
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
205
  async def process_single_dog(image):
206
  top1_prob, topk_breeds, relative_probs = await predict_single_dog(image)
207
 
 
370
  return html
371
 
372
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373
  with gr.Blocks() as iface:
374
  gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
375
  gr.HTML("<p style='text-align: center;'>Upload a picture of a dog, and the model will predict its breed and provide detailed information!</p>")