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

**URL:** https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253
**Category:** Dashboard
**Created:** [7 August 2020 05:59 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253 "2020-08-07T05:59:26Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [7 August 2020 05:59 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/1 "2020-08-07T05:59:26Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/4/d/4d3be06315bbe65dcbacc9523544ac0aa2fda6a2.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/0/9018ed5ce3b40b2457e07844e100cf08e8fc0117.png)

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?

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 August 2020 06:28 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/2 "2020-08-07T06:28:39Z")

</div>

One way...

Use a function node & use the `.filter` [function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) to return only the entries that have this.

Any help?

---

<div class="post-metadata">

### Author: ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)
#### Post date: [7 August 2020 09:12 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/3 "2020-08-07T09:12:18Z")

</div>

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)

---

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [7 August 2020 12:20 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/4 "2020-08-07T12:20:38Z")

</div>

Thanks for your answer. I am using this "[https://flows.nodered.org/node/node-red-contrib-tensorflow](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,

---

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [7 August 2020 12:22 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/5 "2020-08-07T12:22:39Z")

</div>

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,

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [7 August 2020 12:28 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/6 "2020-08-07T12:28:26Z")

</div>

it'll be something like...

Function node code...

```auto
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...

```auto
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

---

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [8 August 2020 07:40 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/7 "2020-08-08T07:40:36Z")

</div>

Many thanks Steve! I will let you know the result 🙂

---

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [8 August 2020 08:56 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/8 "2020-08-08T08:56:21Z")

</div>

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/b/d/bd8c49f1d97cd745b049a7fb54718e55e189dd35.png)  
Hello again,

I have introduced the code in a node function:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a187cabd97b8cd2d11df40de5d77138b815673f9.png)

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.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/5/1/51d8bf54a9177e0fd9548fb09e298f7d9f9a69de.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/8/98567ef4819e48c3b6efea84ef09e47919aae08e.jpeg)

---

<div class="post-metadata">

### Author: ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)
#### Post date: [8 August 2020 10:46 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/9 "2020-08-08T10:46:14Z")

</div>

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

Change the code to this...

```auto
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)

---

<div class="post-metadata">

### Author: ![CesarLeiton](https://avatars.discourse-cdn.com/v4/letter/c/90ced4/32.png) [@CesarLeiton](https://discourse.nodered.org/u/CesarLeiton)
#### Post date: [8 August 2020 11:17 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/10 "2020-08-08T11:17:00Z")

</div>

Thank you very much! It wasvery helpful

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [7 September 2020 11:17 UTC](https://discourse.nodered.org/t/how-to-count-classes-in-an-array-to-represent-them-in-a-dashborad/31253/11 "2020-09-07T11:17:03Z")

</div>

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