As June ends and July begins, garbage classification is undoubtedly the hottest topic in Shanghai recently. In Shanghai, people regularly throw away rubbish for two hours every morning and evening.

Dry and wet garbage should be separated, the wet garbage from the garbage bag into the bucket, and then the garbage bag into the dry garbage can 😂

Lobster 🦞 shell is dry garbage, lobster 🦞 meat is wet garbage, please separate them, thank 😂

Pig 🐷 can eat is wet garbage, can’t eat is dry garbage, eat will die is toxic garbage, sold can buy pig 🐷 is recyclable garbage

Are you going to fall apart? ! Don’t worry, my image-spotting spam app, which I crafted over the weekend, relies on TensorFlow Coco-SSD to identify multiple objects in photos and then finds an unnamed API to return the garbage classification. Such as:

Brief description of production process

First of all, there are already many websites on the Internet where you can query garbage classification by typing text, and I had an idea: wouldn’t it be better to sort garbage through images directly? Then I found the official guide to tensorflow.js:

<! -- Load TensorFlow.js. This is required to use coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"> </script>
<! -- Load the coco-ssd model. -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"> </script>
<img id="img" src="cat.jpg"/>
<script>
  // Notice there is no 'import' statement. 'cocoSsd' and 'tf' is
  // available on the index-page because of the script tag above.

  const img = document.getElementById('img');

  // Load the model. Fetching and loading the model into memory in the browser may take more than 1 minute
  cocoSsd.load().then(model= > {
    // detect objects in the image.
    model.detect(img).then(predictions= > {
      console.log('Predictions: ', predictions);
    });
  });
</script>

Copy the code

As you can see, Google TensorFlow has taken commonly used machine learning models out of the box and made them very convenient. Of course, this multi-object detection function returns an array containing the bbox of the object in the graph, and the classification labels in it are all in English:

[{
  bbox: [x, y, width, height],
  class: "person"Score: 0.8380282521247864}, {bbox: [x, y, width, height], class:"kite"Score: 0.74644153267145157}]Copy the code

So here’s the problem: all garbage sorting apis on the Internet require Chinese input!! The Bing Translate API immediately came to my mind to Translate English into Chinese and then query the categories. So I applied for a free Azure account. Fortunately, I had a master card and paid one DOLLAR. For details, please refer to the official documentation link at the end

After a toss and turn, finally fixed, is the recognition rate is very low. After all, there is no special model for training garbage classification, just a ready-made object detection model.

So there are a lot of funny results 😂 :

Online experience

It is suggested to experience the address online with a computer, and it takes a minute to load the model:

For those interested, see the Github project address

Also welcome to scan code experience:

The resources

tensorflowjs-models

Bing translates official documents

Bing translation official example