Object detection techniques are a trend nowadays in the field of computer vision. There are many techniques which are used to detect objects in the scene images and videos. Each of these techniques has its own advantages and limitations in terms of resources and execution time. Detecting objects in videos also requires a lot of technical knowledge and resources.
So, there is always a search for an easy and quick method for object detection. In this article, we will show how to detect objects seen in the videos in only 5 steps. We will use the pixellib library in this task which detects objects using instance segmentation. We will also use the pre-trained Mask R-CNN model to identify the objects seen in the videos. In this implementation, we will detect vehicle objects in traffic video.
Instance segmentation is a technique in computer vision that is used in object detection using image segmentation method. It identifies each instance of the objects present in the images or videos at the pixel level. In image segmentation, the visual input is splitted into segments to represent the object or part of objects by forming a collection of pixels. Instance segmentation identifies each instance of each object featured in the image instead of categorizing each pixel like in semantic segmentation.
Mask R-CNN is a variant of Deep Neural Network proposed by Kaiming He et al at Facebook AI Research. This model is used to solve the object instance segmentation problem in computer vision. It detects objects in an image while simultaneously generating a high-quality segmentation mask for each instance. It is an extension of Faster R-CNN by adding a branch for predicting an object mask in parallel with the existing branch for bounding box recognition. The Mask R-CNN framework for instance segmentation is given below.
(Image Source: Original Research Paper)
Now, we will discuss the steps through which we will detect the objects in a video.
1. Install library and dependencies
In the first step, we need to install the pixellib library and its dependencies.
! pip install pixellib
2. Load pre-trained Mask-RCNN weights
As we are going to use the Mas R-CNN model to detect objects, we will download its pre-trained weights.
!wget --quiet https://github.com/matterport/Mask_RCNN/releases/download/v2.0/mask_rcnn_coco.h5
3. Import libraries
Now, we will import the pixellib library that has been installed. We will also import the instance_segmentation class because we are going to detect objects using instance segmentation method.
from pixellib.instance import instance_segmentation
4. Instantiate instance segmentation model and load Mask-RCNN weights
In this step, we will instantiate the instance segmentation class that is provided by pixellib and load the Mask R-CNN model with its pre-trained weights.
segment_video = instance_segmentation()
5. Detect objects
In this step, we will process the object detection task by Mask R-CNN in a video. A random traffic video is used in which we want to detect vehicle objects.
In this method, we set the frames per second that are the number of frames per second output video will have.
segment_video.process_video("traffic_vid2.mp4", show_bboxes = True, frames_per_second= 15, output_video_name="object_detect.mp4")
Finally, we will get the output video in the working directory. The time in this process depends on the lengths and size of the video. You should use a GPU for faster processing speed. For the above traffic video, we have got the following video in the out having detected objects.
You can define a function to get the videos from YouTube and pass them directly to the function above. So, using the above steps, we could discuss a very easy way to implement the task of object detection in videos. A person with very less knowledge of deep learning and computer vision can be able to detect objects using this way.
Provide your comments below
If you loved this story, do join our Telegram Community.
Also, you can write for us and be one of the 500+ experts who have contributed stories at AIM. Share your nominations here.
Vaibhav Kumar has experience in the field of Data Science and Machine Learning, including research and development. He holds a PhD degree in which he has worked in the area of Deep Learning for Stock Market Prediction. He has published/presented more than 15 research papers in international journals and conferences. He has an interest in writing articles related to data science, machine learning and artificial intelligence.