TransWikia.com

Count repeating "objects" in a picture

Data Science Asked on July 14, 2021

This is my first data-science project and I would love to get some guidance to know how to get started.

My problem is the following:
I want to count objects that are in a picture. This picture has a special characteristic, it’s going to be "objects" over a white background.

For example:

  • seeds
  • coffee beans
  • painted black dots
  • etc.

The input for example would be:

enter image description here

The output:
Objects: 9

enter image description here

I’ve been doing some research already and I found many places that they recommend the use of an R-CNN to locate and classify my "items" in the picture.

This sounds like a good idea but the problem I see here is that I do not know what I’m going to have in the picture. I know there are going to be "repeated items" of the same type and class but it could be anything.

As I don’t know what I’m going to have in the picture I don’t know what to use to train my model.

How should I proceed?

One Answer

If you really do need to use a ML solution for this problem (as opposed to a strict computer vision solution), most (all?) frameworks will return a list of detections after running inference.

I'll use Tensorflow and this object detection tutorial as an example. If you run the colab and select the text cell with the headline "Visualizing the results" and run all the cells above, it will have downloaded a model and run inference on a picture when it's done.

Insert a code cell and add print(result["detection_classes"]) and run it. That should give output along the lines of:

[[38.  1.  1. 38. 38. 38.  1.  1. 38.  1.  1. 38.  1.  1.  1. 38. 42. 42.
 1. 38. 42. 38.  1.  1.  1. 31. 38. 38. 42.  1. 31. 27.  1.  1. 38.  1.
 2. 38.  1. 38. 37. 38. 38. 27. 38. 38. 38. 44. 38. 38. 27.  1.  1. 38.
 3.  1. 38. 42. 16. 38.  9.  1.  1. 27. 27. 38. 38.  1. 38. 38.  1.  1.
 4. 38. 31. 38.  1. 38. 38. 31. 42.  9. 38. 34. 40. 38. 38.  1. 38. 38.
 5.  3. 18. 27. 38. 18.  1. 31. 42. 38.]]

Each of the numbers in the list maps to one of the classes. In this case you will find the classes in the variable category_index.

Now you can use your favorite python method to count the occurrences. A simple implementation could look something like this:

def count_classes(detections: list) -> tuple:
  ret_tuple = {}
  for detection in detections:
    if detection in ret_tuple.keys():
      ret_tuple[detection] += 1
    else:
      ret_tuple[detection] = 1
  return ret_tuple

Running the above in the colab will give the following:

count_classes(result["detection_classes"][0])
...
{1.0: 30,
 3.0: 1,
 9.0: 2,
 16.0: 1,
 18.0: 3,
 27.0: 6,
 31.0: 5,
 34.0: 1,
 37.0: 1,
 38.0: 41,
 40.0: 1,
 42.0: 7,
 44.0: 1}

Answered by smedegaard on July 14, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP