How to count classes in an array to represent them in a Dashborad

image

Hello,

I have this flow and I would like to count the number of persons in each image. For each object where class ="person", I would like to count 1.

Afterwards I would like to use it for a dashboard to see it in a histogram in a dashboard.

Could you help me?

One way...

Use a function node & use the .filter function to return only the entries that have this.

Any help?

Which, of the several tfjs nodes, is this ? - My version - node-red-contrib-tfjs-coco-ssd - creates a msg.classes property that is an array of classes and counts.
(Note : - be warned that installing two similar tfjs nodes often causes conflicts withe underlying tfjs binary so you may need to uninstall one before installing another if you choose to do that)

Thanks for your answer. I am using this "https://flows.nodered.org/node/node-red-contrib-tensorflow" node-red-contrib-tensorflow, which gives as output these metadata: object, class, bounding box coordenates and score.

I am not sure if I should try yours. We have a flow for predicting with cocossd classes of a picture obtained from an streaming ip video. And our goal would be to show in a dashboard the number of persons vs time in the incoming pictures.

Do you think I should use node-red-contrib-tfjs-coco-ssd?

Thank you in advance,

Thanks for your quick answer. I am a beginner with JavaScript, I have to figure out how to implement the .filter function code.

I will try and let you know if it worked.

Many thanks again,

it'll be something like...

Function node code...

var filter = "person";   //what to find  - note this could come from msg.payload or anywhere!
var filtered = msg.details.filter(el => el.class == filter);   //filter to elements with .class equal to filter
msg.payload = filtered;   //set payload to the filtered results
return msg;  //send the payload to next node

this will give you an array of items where class == person

If you truly only want the count then change it to this...

var filter = "person";   //what to find  - note this could come from msg.payload or anywhere!
var filtered = msg.details.filter(el => el.class == filter);   //filter to elements with .class equal to filter
if(filtered &&  filtered.count)
  msg.payload = filtered.count;  //set payload to the count found
else 
  msg.payload = 0; 
return msg;  //send the payload to next node

EDIT:
these are untested - ymmv

Many thanks Steve! I will let you know the result :slight_smile:


Hello again,

I have introduced the code in a node function:

And it seems like it does not filter the elements with .class equal to filter, because I added a new node msg.details, after the function node and it still shows in the pane objects with different class type than person (boat, for example).

Regarding with the counting part. I tried as well to check it taking the information to a dashboard, but it is always showing 0, even if in the debug pane you can see that it is detecting "person".

Perhaps I am not configuring correctly the dashboard settings.

As I alluded to, it was untested code. I should have used .length not .count

Change the code to this...

var filter = "person";   //what to find  - note this could come from msg.payload or anywhere!
var filtered = msg.details.filter(el => el.class == filter);   //filter to elements with .class equal to filter
if(filtered &&  filtered.length) {
  msg.payload = filtered.length;  //set payload to the count found
} else {
  msg.payload = 0; 
}
msg.filtered = filtered; //pass the filtered array in the msg for future capabilities
return msg;  //send the msg to the next node

NOTE: I have added the property .filtered to the msg so if you want to display the score or access the bounding box - the filtered results will be already in the msg for you (just ignore this property if not needed)

Thank you very much! It wasvery helpful

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.