DawnC commited on
Commit
e9cbeff
1 Parent(s): 01ae78a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -23
app.py CHANGED
@@ -277,7 +277,7 @@ def predict_breed(cropped_image):
277
 
278
  def predict(image):
279
  if image is None:
280
- return "請上傳一張圖片來開始。", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
281
 
282
  try:
283
  if isinstance(image, np.ndarray):
@@ -285,7 +285,7 @@ def predict(image):
285
 
286
  dogs = detect_dogs(image)
287
  if len(dogs) == 0:
288
- return "未檢測到狗或圖片不清晰。請上傳一張更清晰的狗的圖片。", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
289
 
290
  explanations = []
291
  visible_buttons = []
@@ -296,29 +296,29 @@ def predict(image):
296
  top1_prob, topk_breeds, topk_probs_percent = predict_breed(cropped_image)
297
 
298
  draw.rectangle(box, outline="red", width=3)
299
- draw.text((box[0], box[1]), f" {i+1}", fill="red")
300
 
301
  if top1_prob >= 0.5:
302
  breed = topk_breeds[0]
303
  description = get_dog_description(breed)
304
- explanations.append(f" {i+1}: **{breed}**\n{format_description(description, breed)}")
305
  elif 0.2 <= top1_prob < 0.5:
306
  explanation = (
307
- f" {i+1}: 中等置信度檢測。以下是前3個可能的品種:\n"
308
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]})\n"
309
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]})\n"
310
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]})\n"
311
  )
312
  explanations.append(explanation)
313
- visible_buttons.extend([f"更多關於 {topk_breeds[0]}", f"更多關於 {topk_breeds[1]}", f"更多關於 {topk_breeds[2]}"])
314
  else:
315
- explanations.append(f" {i+1}: 圖片不清晰或品種不在數據集中。")
316
 
317
  final_explanation = "\n\n".join(explanations)
318
  return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=len(visible_buttons) >= 2, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=len(visible_buttons) >= 3, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
319
 
320
  except Exception as e:
321
- return f"發生錯誤:{e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
322
 
323
  def format_description(description, breed):
324
  if isinstance(description, dict):
@@ -327,18 +327,18 @@ def format_description(description, breed):
327
  formatted_description = description
328
 
329
  akc_link = get_akc_breeds_link()
330
- formatted_description += f"\n\n**想了解更多狗品種資訊?** [訪問 AKC 狗品種頁面]({akc_link})並搜尋 {breed} 以獲取詳細資訊。"
331
 
332
- disclaimer = ("\n\n*免責聲明:提供的外部連結指向美國養犬俱樂部(AKC)的狗品種頁面。"
333
- "您可能需要在該頁面上搜索特定品種。"
334
- "我對外部網站的內容不負責任。"
335
- "請參閱 AKC 的使用條款和隱私政策。*")
336
  formatted_description += disclaimer
337
 
338
  return formatted_description
339
 
340
  def show_details(breed):
341
- breed_name = breed.split("更多關於 ")[-1]
342
  description = get_dog_description(breed_name)
343
  return format_description(description, breed_name)
344
 
@@ -373,19 +373,19 @@ with gr.Blocks(css="""
373
  }
374
  """) as iface:
375
 
376
- gr.HTML("<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 狗狗品種分類器 🔍</h1>")
377
- gr.HTML("<p style='font-family:Open Sans; color:#34495E; text-align:center;'>上傳一張狗狗的照片,模型將預測其品種,提供詳細資訊,並包含額外的資訊連結!</p>")
378
 
379
  with gr.Row():
380
- input_image = gr.Image(label="上傳狗狗圖片", type="pil")
381
- output_image = gr.Image(label="標註後的圖片")
382
 
383
- output = gr.Markdown(label="預測結果")
384
 
385
  with gr.Row():
386
- btn1 = gr.Button("查看更多 1", visible=False)
387
- btn2 = gr.Button("查看更多 2", visible=False)
388
- btn3 = gr.Button("查看更多 3", visible=False)
389
 
390
  input_image.change(predict, inputs=input_image, outputs=[output, output_image, btn1, btn2, btn3])
391
 
@@ -398,7 +398,7 @@ with gr.Blocks(css="""
398
  inputs=input_image
399
  )
400
 
401
- gr.HTML('如需了解本項目的更多詳情和其他作品,歡迎訪問我的 GitHub <a href="https://github.com/Eric-Chung-0511/Learning-Record/tree/main/Data%20Science%20Projects/Dog%20Breed%20Classifier">狗狗品種分類器</a>')
402
 
403
  if __name__ == "__main__":
404
  iface.launch()
 
277
 
278
  def predict(image):
279
  if image is None:
280
+ return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
281
 
282
  try:
283
  if isinstance(image, np.ndarray):
 
285
 
286
  dogs = detect_dogs(image)
287
  if len(dogs) == 0:
288
+ return "No dogs detected or the image is unclear. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
289
 
290
  explanations = []
291
  visible_buttons = []
 
296
  top1_prob, topk_breeds, topk_probs_percent = predict_breed(cropped_image)
297
 
298
  draw.rectangle(box, outline="red", width=3)
299
+ draw.text((box[0], box[1]), f"Dog {i+1}", fill="red")
300
 
301
  if top1_prob >= 0.5:
302
  breed = topk_breeds[0]
303
  description = get_dog_description(breed)
304
+ explanations.append(f"Dog {i+1}: **{breed}**\n{format_description(description, breed)}")
305
  elif 0.2 <= top1_prob < 0.5:
306
  explanation = (
307
+ f"Dog {i+1}: Detected with moderate confidence. Here are the top 3 possible breeds:\n"
308
  f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]})\n"
309
  f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]})\n"
310
  f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]})\n"
311
  )
312
  explanations.append(explanation)
313
+ visible_buttons.extend([f"More about {topk_breeds[0]}", f"More about {topk_breeds[1]}", f"More about {topk_breeds[2]}"])
314
  else:
315
+ explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
316
 
317
  final_explanation = "\n\n".join(explanations)
318
  return final_explanation, annotated_image, gr.update(visible=len(visible_buttons) >= 1, value=visible_buttons[0] if visible_buttons else ""), gr.update(visible=len(visible_buttons) >= 2, value=visible_buttons[1] if len(visible_buttons) >= 2 else ""), gr.update(visible=len(visible_buttons) >= 3, value=visible_buttons[2] if len(visible_buttons) >= 3 else "")
319
 
320
  except Exception as e:
321
+ return f"An error occurred: {e}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
322
 
323
  def format_description(description, breed):
324
  if isinstance(description, dict):
 
327
  formatted_description = description
328
 
329
  akc_link = get_akc_breeds_link()
330
+ 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."
331
 
332
+ disclaimer = ("\n\n*Disclaimer: The external link provided leads to the American Kennel Club (AKC) dog breeds page. "
333
+ "You may need to search for the specific breed on that page. "
334
+ "I am not responsible for the content on external sites. "
335
+ "Please refer to the AKC's terms of use and privacy policy.*")
336
  formatted_description += disclaimer
337
 
338
  return formatted_description
339
 
340
  def show_details(breed):
341
+ breed_name = breed.split("More about ")[-1]
342
  description = get_dog_description(breed_name)
343
  return format_description(description, breed_name)
344
 
 
373
  }
374
  """) as iface:
375
 
376
+ gr.HTML("<h1 style='font-family:Roboto; font-weight:bold; color:#2C3E50; text-align:center;'>🐶 Dog Breed Classifier 🔍</h1>")
377
+ 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>")
378
 
379
  with gr.Row():
380
+ input_image = gr.Image(label="Upload a dog image", type="pil")
381
+ output_image = gr.Image(label="Annotated Image")
382
 
383
+ output = gr.Markdown(label="Prediction Results")
384
 
385
  with gr.Row():
386
+ btn1 = gr.Button("View More 1", visible=False)
387
+ btn2 = gr.Button("View More 2", visible=False)
388
+ btn3 = gr.Button("View More 3", visible=False)
389
 
390
  input_image.change(predict, inputs=input_image, outputs=[output, output_image, btn1, btn2, btn3])
391
 
 
398
  inputs=input_image
399
  )
400
 
401
+ 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>')
402
 
403
  if __name__ == "__main__":
404
  iface.launch()