Computer Vision — Object Detection in Images with just a few lines of code

PixelLib, a powerful deep learning model helps you with object detection and much more in just a few lines of Python code.

Samiran Banerjee
3 min readJul 4, 2021
Photo by Iulian Silviu Din from Pexels

Being a technology enthusiast, I often find myself venturing into unknown territories to learn and explore my way to pacification. Through a similar process, today, I stumbled into a Python library called, PixelLib which helps to detect objects in Images, Videos, and Real-Time feeds with just a few lines of code.

I am providing a minuscule demo of what the library can do by sharing the code I have used to detect objects in images.

PixelLib supports 80+ image object classes that we can identify and extract from a given image.

Let’s consider the below image and detect the human figures in it,

Photo by Jacek Dylag on Unsplash

The 6 lines of code given below will help to solve our purpose.

import pixellib

from pixellib.instance import instance_segmentation

segment_image = instance_segmentation()

segment_image.load_model(“mask_rcnn_coco.h5”) #”mask_rcnn_coco.h5" is one of the deep learning models that powers this library

target_classes = segment_image.select_target_classes(person=True) #setting the “person” object class as True

segment_image.segmentImage(“people.jpg”, segment_target_classes=target_classes, extract_segmented_objects=True, save_extracted_objects=False, show_bboxes=True, output_image_name=”output.jpg”, text_size=1, text_thickness=2) #if you want to save the extracted objects from the image then set “save_extracted_objects” as True

The output image will be as given below,

Let us consider another scenario where we want to detect and extract the buses from the below frame.

Photo by chan lee on Unsplash

All we have to do is make a small change in the above code for the target_classes.

target_classes = segment_image.select_target_classes(bus=True)

The output image will be as given below,

As I have mentioned earlier, we can fulfill many more objectives using this library, but to discuss them all is beyond the scope of this article.

I would highly recommend you to check it out by visiting the Github link given below,

https://github.com/ayoolaolafenwa/PixelLib

Please do let me know your thoughts about this fantastic library, and let me know if you are aware of any other libraries that offer similar functionalities. Until then, Happy Learning! :)

--

--

Samiran Banerjee

A technology enthusiast and an avid reader. I find writing as the best medium to articulate my thoughts and share my knowledge.