Skip to content

Commit

Permalink
Update ObjectDetection_for_images.ipynb
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
TushtiSavarn authored May 10, 2024
1 parent ceee3db commit e9e5ae7
Showing 1 changed file with 18 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) + \"%\")"
]
Expand Down

0 comments on commit e9e5ae7

Please sign in to comment.