Home > Blockchain >  How to display the number of objects in an image for single class?
How to display the number of objects in an image for single class?

Time:10-17

I am new to programming and been learning the tutorial in google colab for algorithm in object detection. Below is the code used in single shot detector to output images. I added a global variable "count" and for loop to count the number of objects with score more than 0.5. This will work in the case for single class detection. Checked the result with several images and it's printed the count value correctly as in this Sample

CodePudding user response:

 def show_inference(model, image_path):
 global count
 count=0
 # the array based representation of the image will be used later in order 
 #to prepare the result image with boxes and labels on it.
 image_np = np.array(Image.open(image_path))
 # Actual detection.

 output_dict = run_inference_for_single_image(model, image_np)
 # Visualization of the results of a detection.
 vis_util.visualize_boxes_and_labels_on_image_array(
  image_np,
  output_dict['detection_boxes'],
  output_dict['detection_classes'],
  output_dict['detection_scores'],
  category_index,
  instance_masks=output_dict.get('detection_masks_reframed', None),
  use_normalized_coordinates=True,
  line_thickness=8)

 img=Image.fromarray(image_np)

 img.save('/content/my_pig.png')

 for o in output_dict['detection_scores']:
 if o > 0.5:
 count=count 1  


 im=cv2.imread("/content/my_pig.png")
 im = cv2.putText(im, 'Number of people' str(count), (50, 50), 
 cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 0, 0), 2, cv2.LINE_AA)
 cv2_imshow(im)
  • Related