Disclaimer: This article is a short version of the one originally published at LearnOpenCV, which has won the 2nd place at the LearnOpenCV Blog Olympics
With each passing day, the effect of climate change is becoming all too real. From hurricanes and wildfires to melting ice and rising sea levels, its impact keeps getting worse. We need to act urgently to prevent and reverse the damage.
In this article, we’ll look at how computer vision can be used to identify waste such as plastic bottles.
The code can be found by the end of the article as Google Colab links.
The Plastic Waste Problem
According to recent studies, 8 million tons of plastic are dumped into the ocean every year and it is estimated that by 2050 there will be more plastic in the ocean than fish [1].
The problem doesn’t end there.
Due to prolonged exposure to the sun, water, and air, the plastic is eventually broken down into microplastic which is eaten by the fish, other sea mammals, and birds. Each year, 12,000 to 24,000 tons of plastic are consumed by fish in the North Pacific, causing intestinal injury and death, as well as passing plastic up the food chain to larger fish, marine mammals, and human seafood diners [2].
Unless we take deliberate steps to mitigate the problem, the challenges listed above will undoubtedly intensify. In the next section, we will see how computer vision can help reduce them.
Data Collection for Waste Detection
Having sufficient amounts of high-quality data is probably the most important part of any machine learning project as every machine learning algorithm requires some form of data to train.
Thankfully, there is an abundance of high-quality datasets for litter detection and classification. A complete list of those datasets can be found under this GitHub repository [3].
In case you did not find a dataset to solve your specific problem then you can build your own by collecting and annotating images. In the next section, we will see how to scrape and annotate google images to build our dataset.
Google Image Scraping
Web scraping in general is a technique used to extract all kinds of data from websites. In this project, we use web scraping as a means to collect images for our dataset. The following code demonstrates how to extract photos from a Google search for “plastic bottle”.
<div class="caption">
Part of the Image Scraper code (Image by Author)
</div>
Once the scraper is done with the images, we then run a script to delete any duplicate images that may have appeared in our google search before proceeding with the annotation.
Image Annotating for Object Detection
For this project, we have annotated the images with the main 4 classes of litter: plastic, metal, paper, and glass. The following image presents the annotation tool which is used within Google Colab.
Data Preparation and Model Training
After we’re done building our dataset, we then split our data into train, validation, and test sets. We’ve done the splitting with a ratio of 70% training data and 15% validation and test data (70–15–15 for short). We could use a different ratio but the best one for a specific problem is generally found through trial and error.
For an easy-to-understand and implement solution, we will use the imageAI library to train and test a pretrained YoloV3 model.
After preparing the data and downloading our model, the training takes just 5 lines of code.
Model Testing
Finally, the most fun part of our process is testing. Once we are done training our model, we can test it using images from our test set or even run it on videos. Testing the model with imageAI takes 6 lines of code with 2 additional lines to display the image and its corresponding bounding boxes.
Running the code on a testing image produces the following image output.
Contribution to The Cause
You can still contribute to waste detection right away even if you aren’t a tech person. Head over to the TACO (Trash Annotations in Context) dataset where you can upload pictures of trash for others to annotate, or even annotate pictures yourself!
Google Colab Links
Image Web ScraperModel Trainer
References
[1] Jambeck, Jenna R., et al. “Plastic waste inputs from land into the ocean.” Science 347.6223 (2015): 768–771.
[2] “Ocean Plastics Pollution.” Ocean Plastics Pollution, https://www.biologicaldiversity.org/campaigns/ocean_plastics
[3] https://github.com/AgaMiko/waste-datasets-review
Tools
https://github.com/gereleth/jupyter-bbox-widget https://github.com/jfilter/split-folders https://github.com/OlafenwaMoses/ImageAI https://github.com/AndrewCarterUK/pascal-voc-writer https://github.com/elisemercury/Duplicate-Image-Finder