OpenCV object detection (2023)

  1. Use Cascading Classifier for Object Detection in OpenCV
  2. Use YOLO for object detection in OpenCV
OpenCV object detection (1)

This tutorial explains how to detect objects in an image or video stream using Cascade Classifier and YOLO in OpenCV.

Use Cascading Classifier for Object Detection in OpenCV

We can recognize objects that are present in an image, such as B. human faces, animal faces, eyes, etc.Cascade ClassifierOpenCV class to detect objects in an image.

The cascade classifier usestheir trainsto recognize objects based on a cascade of features. We need to use a trained model that contains the feature of the object that we want to detect in an image.

OpenCV is based on many pre-trained models.buenoCharacteristics. The algorithm creates windows from the input image and compares them to feature sets.

A single pretrained model contains around 160,000 functions and it takes a long time to compare each function in the window.

The algorithm then generates cascades from the features, and if a window matches the first cascade, it is compared to the second cascade; otherwise, it is discarded.

In this way, the algorithm takes less time to recognize objects. For example, let's use an image containing a cat and a human and use the cascade classifier to detect the eyes present in the image.

See the code below.

Object CV2src_img=CV2.I read('tier.jpg')gris_img=CV2.cvtColor(src_img, cv2.COLOR_BGR2GREY)c_classifier=CV2.Cascade Classifier(F"{CV2.Data.cascades of hair}haarcascade_eye.xml")d_objects=c_classifier.detectMultiScale(gray_img, minSize=(50,50))con Len(d_objects)!= 0: for(x,y,h,w)theyd_objects:CV2.rectangle(src_img, (x, y), ((x+h), (y+C)), (0,255,255),5)CV2.imshow('Objects detected', src_img)CV2.waitkey(0)CV2.destroy all windows()
(Video) Object Detection OpenCV Python | Easy and Fast (2020)

Salida:

OpenCV object detection (2)

We use the pre-trained modelhaarcascade_eye.xmlfor eye detection in the code above, but we can use many other pre-trained models like face, smile and body detection. Pretrained models are stored and can be found in the OpenCV data foldershortcut.

we use thatdetectarMultiEscala()Cascade classifier function to detect objects. For each object, the function returns a vector containing the x and y coordinates, as well as the width and height of the detected object.

We can use this output to draw a shape around the detected object, e.g. B. a rectangle or a circle.

The first argument ofdetectarMultiEscala()The function is a grayscale input image. the second argumentminimum size, is used to define the minimum size of the object to detect.

There are also other optional arguments that we can define withindetectarMultiEscala()Function. The first optional argument isscale factor, and is used to set the scale of the image, and by default its value is set to1.1.

The second optional argument ismy neighbors, and is used to set the minimum number of neighbors used for object detection, and by default its value is set to3. The third optional argument ismaximum size, which defines the maximum size of the object to detect.

we use thatRectangle()OpenCV function to draw a rectangle around detected objects. The first argument is the image on which we want to draw the rectangle.

(Video) Build your OBJECT DETECTION SOFTWARE - Crash course | with Opencv and Python (2022)

The second and third arguments are the starting and ending positions of the rectangle. The fourth argument is used to set the color in the BGR color scale, and the fifth argument is used to set the line width of the rectangle.

The pre-trained models mostly contain facial features, but we can also create our model for object detection. Look at thisshortcutMore information on training the cascade classifier model can be found here.

Use YOLO for object detection in OpenCV

Various detectors are used for object detection, e.g. B. Single shot, RNN and fast RNN detectors. The single shot detector is fast compared to other detectors, but has poor accuracy.

Like a single shot detector, YOLO is fast compared to a single shot detector, but has the same accuracy as a single shot detector. YOLO passes the entire image through a deep neural network to detect objects present in an image or video.

The algorithm findsbounding boxaround an object present in an image along with its confidence and filters the boxes based on the confidence. If the confidence of a checkbox is less than a certain value, the checkbox is discarded.

YOLO also uses a non-maximal suppression technique to remove overlapping bounding boxes around a single object. This way we just get a bounding box around an object.

YOLO has pretrained weights and settings for the deep neural network that we can load withdnn.readNetFromDarknet()OpenCV function. We can also get the class names for various objects present in the COCO dataset.

we have to download themWeight,IdeasmiCOCOFilenames to use in OpenCV. We can use COCO names to include the name of the object in the bounding box.

After loading the data, we need to read the image and create a blob with it.dnn.blobFromImage()Function which we can then pass to the deep neural network usingset input()Function.

(Video) Tensorflow Object Detection in 5 Hours with Python | Full Course with 3 Projects

We can define the preferred neural network backend for OpenCV usingsetPreferibleBackend()Function. We can also set the preferred target for CPU or GPU usingestablecerObjetivoPreferible()Function.

YOLO will work faster if we have GPU than CPU. We need to run the network down to the last layer and we can use it.getLayerNames()Function to find the names of the layers and thegetUnconnectedOutLayers()Function to get the last level.

Now we use a loop to find the bounding boxes and their confidence, and if the confidence is less than a certain value, the box is discarded and the other boxes are saved.

After that we use thednn.NMSBoxes()to filter the boxes using the non-maximal suppression technique.

odnn.NMSBoxes()The function returns the x and y coordinates and the width and height of the bounding boxes, and we can pass these values ​​inside theRectangle()Function to draw a rectangle around each detected object.

we can useOpenCV putText() functionto place the name of the object named COCO on top of the rectangle.

For example, let's take an image and search for the existing object using YOLO. See the code below.

Object CV2Object aturdir and public notaryimg_src=CV2.I read('tier.jpg')CV2.imshow('Ventana', img_src)CV2.waitkey(1)class_names= Open('coco.names').ler().Area().to share('\NORTE')public notary.arbitrarily.Together(42)Core_rnd=public notary.arbitrarily.casually (0,255, Size=(Len(class names),3), type='uint8')net_yolo=CV2.dnn.readNetFromDarknet('yolov3.cfg','yolov3.pesos')net_yolo.setPreferibleBackend(cv2.dnn.DNN_BACKEND_OPENCV)net_yolo.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)in=net_yolo.getLayerNames()in=[ln[i- 1]forUEtheynet_yolo.getUnconnectedOutLayers()]blob_img=CV2.dnn.blobFromImage(img_src,1/255,0, (416,416), Trade RB=TRUE, cut=INCORRECT)r_blob=blob_img[0,0, :, :]CV2.imshow('Drop', r_blob)Text= F'bubble shape ={blob_img.form}'net_yolo.set input(blob_img)Departures=net_yolo.forward (ln)boxing=[]Trust=[]class id=[]h, w=img_src.form[:2]forsalidatheyDepartures: forrecognitiontheySalida:scores_yolo=Recognition[5:]The class identifier=public notary.argmax(scores_yolo)Trust=punctuations_yolo[Klassen-ID] conTrust> 0,5:box_rect=recognition[:4]*public notary.array([width, height, width, height])(CenterX, CenterY, Width, Height)=box_rect.type("and t")x_c= And t(centroX-(Broad/ 2))y_c= And t(centroY-(Height/ 2))box_rect=[x_c, y_c,And t(Broad),And t(Height)]boxing.add(box_rect)Trust.add(float(Trust))class id.add(class id)Indizes_yolo=CV2.dnn.NMSBoxes(boxes, confidences,0,5,0,4)con Len(yolo_indexes)> 0: forUEtheyIndizes_yolo.eben():(x,y)=(box[i][0], Box[i][1])(b, h)=(box[i][2], Box[i][3])Kor=[And t(C)forCtheycolors_rnd[ID de clase[i]]]CV2.Rectangle(img_src, (x, y), (x+w, y+h), Age,3)Text= "{}:{:.4f}".format(class_names[classIDs[i]], trust[i])CV2.putText(img_src, Text, (x, y- 5), CV2.FONT_HERSHEY_SIMPLEX,0,6, Kor,2)CV2.imshow('Ventana', img_src)CV2.waitkey(0)CV2.destroy all windows()

Salida:

(Video) Object Detection using OpenCV python in 15 Minutes! Coding Tutorial for beginners course!

OpenCV object detection (3)

As we can see, five objects have been detected in the image above and their accuracy or reliability is also placed over the rectangles. We can also apply the same code to a video; We need to read each frame, apply the above code to each frame, and save the frame to a video again.

In the above code, thenp.random.randint()The function is used to create random colors. The first argument is the starting color value, and the second argument is the ending color value.

the third argumentSize, is used to define the size of each color and the fourth argument,type, is used to define the data type of the output. EITHERadd()The function adds the value to the specified array.

oRectangle()The OpenCV function is used to draw rectangles around the detected objects. The first argument is the image on which we want to draw the rectangle.

The second argument is the starting point or the location of the upper left corner of the rectangle, and the third argument is the ending point or the location of the right corner of the rectangle. The fourth argument is the color, and the fifth argument is the line width of the rectangle.

oputText()The function is used to place text on the image. The first argument is the image we want to place the text on and the second is the text we want to place on the image.

The third argument is the starting position of the text and the fourth argument is the font of the text. The fifth argument is used to set the scale of the font, and the sixth argument is used to set the line width of the text.

Videos

1. Object Tracking with Opencv and Python
(Pysource)
2. OpenCV Course - Full Tutorial with Python
(freeCodeCamp.org)
3. Object Identification & Animal Recognition With Raspberry Pi + OpenCV + Python
(Core Electronics)
4. Object Tracking from scratch with OpenCV and Python
(Pysource)
5. Live Object Detection in Python
(NeuralNine)
6. Object Detection using OpenCV | Python | Tutorial for beginners 2020
(DeepLearning_by_PhDScholar)
Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated: 01/21/2023

Views: 6690

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.