Introduction
Machine learning and images have a great relationship, image classification has been one of the main roles of machine learning over the years. It has been very useful during the COVID-19 pandemic to recognize people who do not follow the rules such as wearing masks and keeping their distance.
Prerequisites
Each program has some prerequisites for solving problems related to the environment.. We are building a dataset for a machine learning project, the minimum requirement for this is a machine with python3 installed and an OpenCV module on it.
I am using Jupyter Notebook on my system. If you also want to use the same settings, you need to install Anaconda on your machine and then install OpenCV.
Instalar OpenCV
To install OpenCV, open command prompt if you are not using anaconda. On the contrary, open anaconda command prompt from windows search and type the command given below.
pip install opencv-python=3.4.2.17
Now you are all set to code and prepare your dataset.
Steps involved
Here we are going to cover all the steps involved in creating this program.
Paso 1: Import modules
First, we have to import all the required modules to the program console. We only need two modules, one is the “OpenCV” and the other is the module “you”. Opencv is used to capture and render the image using the laptop's camera and the operating system module is used to create a directory.
import cv2 as cv import os
Paso 2: create a camera object
How we have to create our own image dataset, we need the camera, and OpenCV helps us to create camera objects that can be used later for various actions.
#argument 0 is given to use the default camera of the laptop camera = cv.VideoCapture(0) #Now check if the camera object is created successfully if not camera.isOpened(): print("The Camera is not Opened....Exiting") exit()
Paso 3: create tag folders
Now, we need to create folders for each tag for the sake of differentiation. Use the code provided below to create these folders, you can add as many tags as you like. We have named our labels according to the game: stone, paper, pair of scissors. We are preparing a dataset that could classify the image if it is a stone, a paper, a scissors or just a bottom.
#creating a list of lables "You could add as many you want" Labels = ["Background","Stone","Paper","Scissors"] #Now create folders for each label to store images for label in Labels: if not os.path.exists(label): os.mkdir(label)
Paso 4: final step to capture images
This is the final and most crucial step of the program.. Online comments have been written to make it easier to understand. Here we have to capture images and store those images according to the tag folder. Read the code thoroughly, we have mentioned every little thing here.
for folder in Labels: #using count variable to name the images in the dataset. count = 0 #Taking input to start the capturing print("Press 's' to start data collection for"+folder) userinput = input() if userinput != 's': print("Wrong Input..........") exit() #clicking 200 images per label, you could change as you want. while count<200: #read returns two values one is the exit code and other is the frame status, frame = camera.read() #check if we get the frame or not if not status: print("Frame is not been captured..Exiting...") break #convert the image into gray format for fast caculation gray = cv.cvtColor(frame, cv.COLOR_BGR2GRAY) #display window with gray image cv.imshow("Video Window",gray) #resizing the image to store it gray = cv.resize(gray, (28,28)) #Store the image to specific label folder cv.imwrite('C:/Users/HP/Documents/AnacondaML/'+folder+'/img'+str(count)+'.png',gray) count=count+1 #to quite the display window press 'q' if cv.waitKey(1) == ord('q'): break # When everything done, release the capture camera.release() cv.destroyAllWindows()
Practical implementation
Now, run the program to create the dataset. We will provide the background first, then the stone, the paper and scissors. Before implementation, you should always be clear about what you have coded and how the output will help you solve the use case requirement. Let's do it ...
Run the program all at once
We are using jupyter notebook to run this program, you can use any python interpreter. First, go to the cell menu and click “Run all”, this will execute all available cells in one hit.
Now, an input message will be generated, press' s’ and hit enter to start saving images for the background.
After pressing 's', will capture 200 background images. The display window will appear and the images will begin to be captured., so step out of the frame and let the camera capture the background.
Now, will ask 's’ and will capture images of “stone”. Then, make a fist and show it to the camera in various positions.
Note: just wave your hand with your fist close, don't fix your hand in one position to produce a well-labeled data set.
Now, repeat the same process for the paper and scissors images. Don't forget to press' s’ when prompted, on the contrary, the display window will appear to be stuck, but it is not.
The program will close automatically. Now you can check by browsing whether the dataset was created or not.
Note: The image dataset will be created in the same directory where the Python program is stored. Four directories will be created according to the label assigned to them.
Yes, folders have been created successfully, now check if the images have been captured and saved. The image size will not be the same as what you were viewing during the capture process. We have reduced the size of the image so that, when used in a machine learning project to train the model, require fewer resources and time.
¡Viva! We have created our own image dataset that, what's more, can be used in machine learning projects for classification.