Spaces:
Running
on
Zero
Running
on
Zero
Update app.py
Browse files
app.py
CHANGED
@@ -284,7 +284,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:
|
@@ -296,118 +296,6 @@ async def detect_multiple_dogs(image, conf_threshold=0.2, iou_threshold=0.5):
|
|
296 |
return dogs
|
297 |
|
298 |
|
299 |
-
# async def predict(image):
|
300 |
-
# if image is None:
|
301 |
-
# return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
302 |
-
|
303 |
-
# try:
|
304 |
-
# if isinstance(image, np.ndarray):
|
305 |
-
# image = Image.fromarray(image)
|
306 |
-
|
307 |
-
# # 嘗試檢測多隻狗
|
308 |
-
# dogs = await detect_multiple_dogs(image)
|
309 |
-
# if len(dogs) == 0:
|
310 |
-
# # 單狗情境
|
311 |
-
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(image)
|
312 |
-
# if top1_prob < 0.2:
|
313 |
-
# return "The image is unclear or the breed is not in the dataset. Please upload a clearer image of a dog.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
314 |
-
|
315 |
-
# breed = topk_breeds[0]
|
316 |
-
# description = get_dog_description(breed)
|
317 |
-
|
318 |
-
# if top1_prob >= 0.5:
|
319 |
-
# formatted_description = format_description(description, breed)
|
320 |
-
# return formatted_description, image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
321 |
-
# else:
|
322 |
-
# explanation = (
|
323 |
-
# f"The model couldn't confidently identify the breed. Here are the top 3 possible breeds:\n\n"
|
324 |
-
# f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
325 |
-
# f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
326 |
-
# f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)\n\n"
|
327 |
-
# "Click on a button to view more information about the breed."
|
328 |
-
# )
|
329 |
-
# return explanation, image, 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]}")
|
330 |
-
|
331 |
-
# # 多狗情境
|
332 |
-
# color_list = ['#FF0000', '#00FF00', '#0000FF', '#FFFF00', '#00FFFF', '#FF00FF', '#800080', '#FFA500']
|
333 |
-
# explanations = []
|
334 |
-
# annotated_image = image.copy()
|
335 |
-
# draw = ImageDraw.Draw(annotated_image)
|
336 |
-
# font = ImageFont.load_default()
|
337 |
-
|
338 |
-
# for i, (cropped_image, _, box) in enumerate(dogs):
|
339 |
-
# top1_prob, topk_breeds, topk_probs_percent = await predict_single_dog(cropped_image)
|
340 |
-
# color = color_list[i % len(color_list)]
|
341 |
-
# draw.rectangle(box, outline=color, width=3)
|
342 |
-
# draw.text((box[0], box[1]), f"Dog {i+1}", fill=color, font=font)
|
343 |
-
|
344 |
-
# breed = topk_breeds[0]
|
345 |
-
# if top1_prob >= 0.5:
|
346 |
-
# description = get_dog_description(breed)
|
347 |
-
# formatted_description = format_description(description, breed)
|
348 |
-
# explanations.append(f"Dog {i+1}: {formatted_description}")
|
349 |
-
# elif top1_prob >= 0.2:
|
350 |
-
# explanations.append(f"Dog {i+1}: Top 3 possible breeds:\n"
|
351 |
-
# f"1. **{topk_breeds[0]}** ({topk_probs_percent[0]} confidence)\n"
|
352 |
-
# f"2. **{topk_breeds[1]}** ({topk_probs_percent[1]} confidence)\n"
|
353 |
-
# f"3. **{topk_breeds[2]}** ({topk_probs_percent[2]} confidence)")
|
354 |
-
# else:
|
355 |
-
# explanations.append(f"Dog {i+1}: The image is unclear or the breed is not in the dataset.")
|
356 |
-
|
357 |
-
# final_explanation = "\n\n".join(explanations)
|
358 |
-
# return final_explanation, annotated_image, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
359 |
-
|
360 |
-
# except Exception as e:
|
361 |
-
# return f"An error occurred: {str(e)}", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False)
|
362 |
-
|
363 |
-
# def show_details(choice):
|
364 |
-
# if not choice:
|
365 |
-
# return "Please select a breed to view details."
|
366 |
-
|
367 |
-
# try:
|
368 |
-
# breed = choice.split("More about ")[-1]
|
369 |
-
# description = get_dog_description(breed)
|
370 |
-
# return format_description(description, breed)
|
371 |
-
# except Exception as e:
|
372 |
-
# return f"An error occurred while showing details: {e}"
|
373 |
-
|
374 |
-
|
375 |
-
# with gr.Blocks() as iface:
|
376 |
-
# gr.HTML("<h1 style='text-align: center;'>🐶 Dog Breed Classifier 🔍</h1>")
|
377 |
-
# 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>")
|
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(
|
391 |
-
# predict,
|
392 |
-
# inputs=input_image,
|
393 |
-
# outputs=[output, output_image, btn1, btn2, btn3]
|
394 |
-
# )
|
395 |
-
|
396 |
-
# btn1.click(show_details, inputs=btn1, outputs=output)
|
397 |
-
# btn2.click(show_details, inputs=btn2, outputs=output)
|
398 |
-
# btn3.click(show_details, inputs=btn3, outputs=output)
|
399 |
-
|
400 |
-
# gr.Examples(
|
401 |
-
# examples=['Border_Collie.jpg', 'Golden_Retriever.jpeg', 'Saint_Bernard.jpeg', 'French_Bulldog.jpeg', 'Samoyed.jpg'],
|
402 |
-
# inputs=input_image
|
403 |
-
# )
|
404 |
-
|
405 |
-
# 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>')
|
406 |
-
|
407 |
-
# if __name__ == "__main__":
|
408 |
-
# iface.launch()
|
409 |
-
|
410 |
-
|
411 |
async def predict(image):
|
412 |
if image is None:
|
413 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
@@ -416,7 +304,7 @@ async def predict(image):
|
|
416 |
if isinstance(image, np.ndarray):
|
417 |
image = Image.fromarray(image)
|
418 |
|
419 |
-
dogs = await detect_multiple_dogs(image, conf_threshold=0.
|
420 |
|
421 |
if len(dogs) <= 1:
|
422 |
return await process_single_dog(image)
|
|
|
284 |
return top1_prob, topk_breeds, topk_probs_percent
|
285 |
|
286 |
|
287 |
+
async def detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5):
|
288 |
results = model_yolo(image, conf=conf_threshold, iou=iou_threshold)[0]
|
289 |
dogs = []
|
290 |
for box in results.boxes:
|
|
|
296 |
return dogs
|
297 |
|
298 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
299 |
async def predict(image):
|
300 |
if image is None:
|
301 |
return "Please upload an image to start.", None, gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), gr.update(visible=False), None
|
|
|
304 |
if isinstance(image, np.ndarray):
|
305 |
image = Image.fromarray(image)
|
306 |
|
307 |
+
dogs = await detect_multiple_dogs(image, conf_threshold=0.3, iou_threshold=0.5)
|
308 |
|
309 |
if len(dogs) <= 1:
|
310 |
return await process_single_dog(image)
|