Easily Capture Images for Training Datasets
by Timothy Y in Circuits > Raspberry Pi
200 Views, 1 Favorites, 0 Comments
Easily Capture Images for Training Datasets
Whether you are training a machine learning model to solve recycling contamination by automatically sorting between recyclables and trash or a machine learning model to detect different types of fruits, you need a good image training dataset . However, in order to create a good image training dataset to to use on your model, you need good images to train your model on. This is where this setup is especially useful. With this setup, you will be able to efficiently capture images with one button of what you want to capture in front of a clear white background with constant lighting and have it automatically named "Class Name.Image Number" to more easily split the image into different classes (ex. apples, bananas, mango).
Supplies
- Optical mouse
- wood panel
- LED Strip
- Keyboard
- Monitor with HDMI input
- Raspberry Pi Camera Module (any)
- Scrap Wood
- Raspberry Pi 4 Model B
Setup
Before we begin, make sure to setup up your raspberry pi 4. Once your Raspberry pi is setup, connect the camera (works for most cameras) to your Raspberry pi. Then, power on the Raspberry Pi and on the left top corner select the Raspberry Pi Icon --> Programming --> Thonny.
Building
Before we start coding, we need to make a wooden box to place the plastics and cans or whatever your objects are on top of as well as a place for the camera and all the other components.
Here is the schematic of the design with measurements in inches, all labeled:
Coding
Once Thonny is opened, you will be presented with an empty python file. Write the following code and save the program.
import osimport timenumber = 0className = "OS"
#Change to your class namewhile (True):
#for i in range(0,20):
input("Press enter")
res = os.system("raspistill -ISO 100 -ss 8900 -o " + className+"."+str(number)+".jpeg")
number = number + 1print("Saved " + className+"."+str(number)+"JPEG")
print(" ")
print("Place next object")
Once you have the program saved, create a folder named after your class name, then put the python file inside that folder. Go back into the program you just wrote and set the "className" variable to the class that you named the folder and keep the "number" variable to zero. (If for any reason something happened and you want to continue where you left off, change the "number" variable to the number of the last photo, if you keep it zero, it will overwrite the existing images!)
Now, press the green start button to run the program. In the shell, underneath the editor, there will be instructions and feedback printed. When "Press Enter" is printed, the program is ready to take a picture, once you press enter, the program will take a picture. (I recommend at least a few hundred images for each class but more like a few thousand would be better, there can never be too many images!.)
Coding Continued
While it is taking a picture, a live preview of the image you will take will be shown in the middle of the screen, wait until it goes away and "press enter" is printed again. If the preview seems too dark or too bright, change the number after the "-ISO" and the number after the "-ss" on line 15 accordingly until it is adjusted properly.
Once you are done taking images of one class (ex. apples) and want to start taking picture of another class (ex. mangos), create a new folder named after the new class name, move the python script to the folder, rename the "className" variable to the new class name, and start taking pictures again!
Once you are completely done, you should have a complete image dataset with folders named after each class with at least few hundred images which you can use it to train your machine learning model. my other project
Code
Python:
Script to take the pictures
import os
import time
number = 0
className = "OS" #Change to your class name
while (True):
#for i in range(0,20):
input("Press enter")
res = os.system("raspistill -ISO 100 -ss 8900 -o " + className+"."+str(number)+".jpeg")
number = number + 1
print("Saved " + className+"."+str(number)+"JPEG")
print(" ")
print("Place next object")