From e9e5ae77496f9fa3ca70d3fff9958a4840715d65 Mon Sep 17 00:00:00 2001 From: Tushti Savarn <82764533+TushtiSavarn@users.noreply.github.com> Date: Fri, 10 May 2024 15:55:25 +0530 Subject: [PATCH] Update ObjectDetection_for_images.ipynb This code will draw bounding boxes with labels and confidence scores on the image, with improved readability and aesthetics. Adjustments have been made to dynamically scale the font size and add a filled rectangle behind the text for better visibility. Additionally, the confidence score is displayed along with the class label. --- .../ObjectDetection_for_images.ipynb | 21 ++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/Artificial Intelligence/Basic/Object_Detection/ObjectDetection_for_images.ipynb b/Artificial Intelligence/Basic/Object_Detection/ObjectDetection_for_images.ipynb index 1f0aabae0..c65c47c6b 100644 --- a/Artificial Intelligence/Basic/Object_Detection/ObjectDetection_for_images.ipynb +++ b/Artificial Intelligence/Basic/Object_Detection/ObjectDetection_for_images.ipynb @@ -231,10 +231,25 @@ } ], "source": [ - "font_scale = 3\n", + "# Initialize font scale and font", + "font_scale = 1\n", "font = cv2.FONT_HERSHEY_PLAIN\n", - "for ClassInd,conf,boxes in zip(ClassIndex.flatten(),confidence.flatten(),bbox):\n", - " cv2.rectangle(img,boxes,(255,0,0),2)\n", + "# Loop over detections and draw bounding boxes\n" + "for ClassInd, conf, boxes in zip(ClassIndex.flatten(), confidence.flatten(), bbox):\n", + " # Get class label and confidence score ", + "class_label = classLabels[ClassInd - 1]", + "confidence_score = conf * 100", + " # Draw bounding box", + " color = (0, 255, 0) # Green color for all boxes", + " cv2.rectangle(img, boxes, color, 2)", +" # Draw class label and confidence score", + " cv2.putText(img, f'{class_label}: {confidence_score:.2f}%', (boxes[0] + 10, boxes[1] + 20)", + font, fontScale=font_scale, color=(0, 255, 0), thickness=2) + +# Display the final image with detections +plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) +plt.axis('off') # Turn off axis +plt.show()\n", " cv2.putText(img,classLabels[ClassInd - 1],(boxes[0] + 10,boxes[1] + 40),font,fontScale = font_scale,color = (0,255,0),thickness = 3)\n", " print(classLabels[ClassInd - 1] + \" : \" + str(conf * 100) + \"%\")" ]