OpenCV Pedestrian Detection _opencv

Source: Internet
Author: User
Tags svm vars

Note: This article is translated from: pedestrian detection OpenCV.

Do you know the built-in pedestrian detection method inside the OpenCV? In OpenCV, there is a hog+ linear SVM model that can detect pedestrians in images and videos. If you are not familiar with the directional gradient histogram hog and linear SVM method, I suggest you read the direction gradient histogram and object detection this article, in this article, I have 6 steps to discuss the framework.

If you're already familiar with the process, or if you just want to see the code for OPENCV pedestrian detection, open a new file now and name it detect.py, and start our programming Journey:

1 # Import the necessary packages
 2 from __future__ import print_function
 3 from imutils.object_detection import n On_max_suppression
 4 from imutils import paths
 5 import numpy as NP
 6 import argparse
 7 import imutils
  
   8 Import Cv2
 9 
# Construct the argument parse and parse the arguments
-one AP = Argparse. Argumentparser ()
ap.add_argument ("I", "--images", Required=true, help= "path to Images directory")
13 args = VARs (Ap.parse_args ()) 
# Initialize HOG-Descriptor/person Detector
= HOG. Hogdescriptor ()
hog.setsvmdetector (Cv2. Hogdescriptor_getdefaultpeopledetector ())
  

第2-8 line to import some of our necessary packages, we import Print_ function ensures that our code is compatible with both Python2.7 and Python3, so that our code can work on both opencv2.4.x and OPenCV3, and then from my imutils package we import non_max_ suppression function.

If you have not installed Imutils, you can install it via PIP:

$ pip Install Imutils

If you have installed imutils, you need to update it to the latest version (v0.3.1), which contains the implementation of the Non_max_suppression function, and some other minor updates:

$ pip Install--upgrade imutils

I've talked about the non-max suppression (Non-maxima suppression) method on my Pyimagesearch blog two times, one at a time when Python is not greatly suppressed for object detection, one is to achieve faster non extreme suppression in Python, In either case, the purpose of the suppression is to get multiple overlapping borders (bounding box) and reduce them to just one border.

Figure 1: There are many error-detecting borders on the left, and the right image is suppressed so that we can suppress those overlapping areas and leave the correct border.

The method of non maximal inhibition can reduce the false positive rate in the process of pedestrian detection.

The 第11-13 line handles the arguments passed in by our command line, where we only need a toggle--images, which is used to pass the image directory of the pedestrian to be detected.

Finally, line 16th and line 17 initialize our pedestrian detectors. First, we call hog = Cv2. Hogdescriptor () to initialize the directional gradient histogram descriptor, we then call Setsvmdetector to set up the support vector machine (Support vector Machine) to make it a trained pedestrian detector.

Here, our OPENCV pedestrian detector is fully loaded, and we just need to apply it to some images:

 1 # import the necessary packages 2 from __future__ import print_function 3 from imutils.object_detection import Non_ma  X_suppression 4 from Imutils import paths 5 import NumPy as NP 6 import argparse 7 import imutils 8 Import CV2 9 10 # construct the argument parse and parse the arguments ap = argparse. Argumentparser () ap.add_argument ("I", "--images", Required=true, help= "path to images directory") args = VARs (AP.PA Rse_args ()) # Initialize the HOG descriptor/person detector HOG = Cv2. Hogdescriptor () Hog.setsvmdetector (Cv2. Hogdescriptor_getdefaultpeopledetector ()) # Loop over the image paths to ImagePath in Paths.list_images (args["I Mages "]): # Load the image and resize it to (1) Reduce detection time # and (2) Improve detection accuracy IMA GE = cv2.imread (imagepath) image = Imutils.resize (image, Width=min (image.shape[1)) orig = Image.copy () 26 2 7 # Detect people in the image (rects, weights) = Hog.detectmultiscaLe (Image, Winstride= (4, 4), padding= (8, 8), scale=1.05) # Draw the original bounding boxes for (x, Y,  W, h) in rects:33 Cv2.rectangle (orig, (x, y), (x + W, y + h), (0, 0, 255), 2) Apply Non-maxima suppression To the bounding boxes a * fairly large overlap threshold to try to maintain overlapping Panax boxes that are St Ill people rects = Np.array ([[X, Y, x + W, y + h] for (x, Y, W, h) in rects]) pick = Non_max_suppression (rects, PR Obs=none, overlapthresh=0.65) # Draw the final bounding boxes (XA, YA, XB, YB) in pick:43 Cv2.rectan GLE (image, (XA, YA), (XB, YB), (0, 255, 0), 2) # show some information on the number of bounding boxes-Filenam E = Imagepath[imagepath.rfind ("/") + 1:] Print ("[INFO] {}: {} original boxes, {} after Suppression". Format (+ fi Lename, Len (rects), Len (pick))) # show the output images-cv2.imshow ("Before NMS", orig) cv2.imshow ("After NMS ", image) Cv2.waiTKey (0) 

On line 20th we loop through the images in our--images directory, and the example samples used in this blog are extracted from the very popular library of Inria person dataset, and more specifically, from the GRAZ-01 set, These pictures are stored under the source directory.

第23-25 the image on the disk that is loaded into the and cutting the image to a maximum width of 400 pixels, the reason for lowering our image dimension (in fact, cutting our image size) is two reasons: reducing the size of the image can reduce the number of sliding windows in the image pyramid, This can reduce the time of detection, thereby increasing the throughput of overall detection. Adjust the size of the image can improve the accuracy of pedestrian detection, that is, false positive rate.

The code that really detects pedestrians in the image is on line 28th and line 29, by calling Detectmultiscale's Hog description child method. The Detectmultiscale method constructs a scale scale=1.05 image pyramid and a sliding window with a pixel size of (4,4) in the X and Y directions respectively.

The size of the window is fixed to the 32*128 pixel size, which is set according to the seminal Dalal and Triggs paper. The Detectmultiscale function returns the rects of a 2-tuple, or the border coordinates (X,Y) of each pedestrian in the image, and the weights confidence value that is returned by the SVM in each test (we are generally also a fraction, translator).

The larger the scale of the scale, in the image pyramid of the number of middle-level, the corresponding detection speed is faster, but the scale of the General Assembly causes pedestrians to be missed; Similarly, if the scale is set too small, it will dramatically increase the number of layers of the image pyramid, which not only consumes computational resources, It also dramatically increases the number of false-positive numbers that occur during testing (that is, not pedestrians are detected as pedestrians). This indicates that the scale is an important parameter in the pedestrian detection process and requires the scale to be adjusted. I'll do some research on each of the parameters in the Detectmultiscale in a later article.

Lines 32nd and 33 get our initial borders and frame them on the image.

However, you will see a box of pedestrian frames on some images, with many overlapping borders, as shown in the 1 figure above.

In this case, we have two options. One option is to detect if a border completely contains another border (you can look at some of the implementation examples in openv). Another option is to apply the non-maximum inhibition method by setting a threshold to suppress those overlapping borders, which is what line 38th and line 39 do.

Note: If you want to learn more about hog frame and non great suppression, I recommend you to read the directional gradient histogram and object detection. In that blog post, you can look at Python's Malisiewicz method for object detection and subsequent updates.

After applying a great deal of inhibition, we draw the final border in lines 42nd and 43, and in the 第46-48 line we show some basic information about the image, as well as the number of borders detected, and in the 第51-53 line, we finally display the image we entered. Pedestrian test Results

To see the actual effect of the pedestrian detection script we wrote, we just need to execute the following command:

$ python detect.py--images images

The following figure is a result of a pedestrian test:

Figure 2: Detection effect

Above we detected a single pedestrian standing beside a police car.

Figure 3:1 people were detected in the foreground and the background

Above we can see that the man in the foreground is detected, and the woman in the background who is pushing the baby car is also detected.

Figure 4: An example that shows why it is important to use non-extreme inhibition

The example in Figure 4 shows why it is important to use non extreme inhibition. In addition to detecting the correct border, the Detectmultiscale function also detects two border borders, and these two error borders cover pedestrians in the image. By using non extreme suppression, we can suppress the wrong border, leaving only the correct detected border.

Figure 5: Another example showing a non-extreme suppression effect

Once again we can see that there are a lot of wrong borders detected, and by using non extreme suppression we can suppress the wrong border, leaving only the correct detected border.

Figure 6: Detecting pedestrians in a shopping center

Figure 6 Pedestrian detection in a shopping center, two people are walking towards the camera, another person is away from the camera, in either case, our hog detection method can accurately detect the traveler. The larger overlapthresh in the Non_maxima_suppression function ensures that those partially overlapping borders are not suppressed.

Figure 7: Detecting pedestrians in blurred images

To be honest, I was a little surprised by the test results of the image above, because in general the hog descriptor was not very well detected in the motion blur picture, but in this image we detected the pedestrian.

Figure 8: Detecting pedestrians on outdoor streets

There is another example of overlapping borders, but since our Overlapthresh are set to a larger size, these borders are not suppressed so that the correct test results can be left behind.

Figure 9: Testing a picture of a family with 4 members

The example in Figure 9 shows the versatility of the HOG+SVM pedestrian detector, and we can detect not only adult men but also those three children (note: The detector does not detect children hiding behind his father).

Figure 10: Pedestrian detection of signpost markings

I put figure 10 in the end because I found it very interesting and we can see clearly that this is just a signpost logo that identifies the crosswalk, however, the HOG+SVM detectors have them framed in the diagram, but actually they are not pedestrians. Summarize

In this blog post, we've learned how to use OpenCV's library and python for pedestrian detection.

In fact, the OPENCV library has built a hog+ linear SVM detector, which is based on Dalal and Triggs paper to automatically detect pedestrian in the image.

Although the Hog method is more accurate than the Haar counter-part, it still needs to be set up reasonably for Detectmultiscale. In a later blog post, I'll do a survey of each of the parameters in the Detectmultiscale, how I've tuned the details, and state the tradeoff between accuracy and performance.

Anyway, I hope you like this blog post. I'm going to give you more tutorials on object detection in the back, and if you want these tutorials to come out and get timely notice, you can consider subscribing to my blog.

I have included the hog+ linear SVM object detection method in Pyimagesearch Gurus course, you can see.

From:http://yongyuan.name/blog/pedestrian-detection-opencv.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.