Pedestrian Detection OpenCV


I’ve met a lot of amazing, uplifting people over the years. My PhD advisor who helped get me through graduate school. My father who was always there for me as a kid — and still is now. And my girlfriend who has always been positive, helpful, and supportive (even when I probably didn’t deserve it).

I’ve also met some demoralizing, discouraging ones. Family members who have gone out of their way to deter me from being an entrepreneur and working for myself. Colleagues who either disliked me or my work and chose to express their disdain in a public fashion. And then there are those who have said some pretty disheartening things over email, Twitter, and other internet outlets.

We’re all familiar with these types of people. Yet regardless of their demeanor (whether positive or negative), we’re all built from the same genetic material of four nucleobases: cytosine, guanine, adenine, and thymine.

These base pairs are combined in such a way that our bodies all have the same basic structure regardless of gender, race, or ethnicity. At the most structural level we all have a head, two arms, a torso, and two legs.

We can use computer vision to exploit this semi-rigid structure and extract features to quantify the human body. These features can be passed on to machine learning models that when trained can be used to detect and track humans in images and video streams. This is especially useful for the task of pedestrian detection, which is the topic we’ll be talking about in today’s blog post.

Read on to find out how you can use OpenCV and Python to perform pedestrian detection.

Looking for the source code to this post?
Jump right to the downloads section.

Pedestrian Detection OpenCV

Did you know that OpenCV has built-in methods to perform pedestrian detection?

OpenCV ships with a pre-trained HOG + Linear SVM model that can be used to perform pedestrian detection in both images and video streams. If you’re not familiar with the Histogram of Oriented Gradients and Linear SVM method, I suggest you read this blog post where I discuss the 6 step framework.

If you’re already familiar with the process (or if you just want to see some code on how pedestrian detection with OpenCV is done), just open up a new file, name it , and we’ll get coding:

Lines 2-8 start by importing our necessary packages. We’ll import print_function  to ensure our code is compatible with both Python 2.7 and Python 3 (this code will also work for OpenCV 2.4.X and OpenCV 3). From there, we’ll import the non_max_suppression  function from my imutils package.

If you do not have imutils  installed, let pip  install it for you:

If you do have imutils  installed, you’ll need to upgrade to the latest version (v0.3.1) which includes the implementation of the non_max_suppression  function, along with a few other minor updates:

I’ve talked about non-maxima suppression twice on the PyImageSearch blog, once in this introductory post, and again in this post on implementing a faster NMS algorithm. In either case, the gist of the non-maxima suppression algorithm is to take multiple, overlapping bounding boxes and reduce them to only a single bounding box:

Figure 3: (Left) Multiple bounding boxes are falsely detected for the person in the image. (Right) Apply non-maxima suppression allows us to suppress overlapping bounding boxes, leaving us with the correct final detection.

Figure 1: (Left) Multiple bounding boxes are falsely detected for the person in the image. (Right) Applying non-maxima suppression allows us to suppress overlapping bounding boxes, leaving us with the correct final detection.

This helps reduce the number of false-positives reported by the final object detector.

Lines 11-13 handle parsing our command line arguments. We only need a single switch here, --images , which is the path to the directory that contains the list of images we are going to perform pedestrian detection on.

Finally, Lines 16 and 17 initialize our pedestrian detector. First, we make a call to hog = cv2.HOGDescriptor()  which initializes the Histogram of Oriented Gradients descriptor. Then, we call the setSVMDetector  to set the Support Vector Machine to be pre-trained pedestrian detector, loaded via the cv2.HOGDescriptor_getDefaultPeopleDetector()  function.

At this point our OpenCV pedestrian detector is fully loaded, we just need to apply it to some images:

On Line 20 we start looping over the images in our --images  directory.  The examples in this blog post (and the additional images included in the source code download of this article) are samples form the popular INRIA Person Dataset (specifically, from the GRAZ-01 subset).

From there, Lines 23-25 handle loading our image off disk and resizing it to have a maximum width of 400 pixels. The reason we attempt to reduce our image dimensions is two-fold:

  1. Reducing image size ensures that less sliding windows in the image pyramid need to be evaluated (i.e., have HOG features extracted from and then passed on to the Linear SVM), thus reducing detection time (and increasing overall detection throughput).
  2. Resizing our image also improves the overall accuracy of our pedestrian detection (i.e., less false-positives).

Actually detecting pedestrians in images is handled by Lines 28 and 29 by making a call to the detectMultiScale  method of the hog  descriptor. The detectMultiScale  method constructs an image pyramid with scale=1.05  and a sliding window step size of (4, 4)  pixels in both the x and y direction, respectively.

The size of the sliding window is fixed at 64 x 128 pixels, as suggested by the seminal Dalal and Triggs paper, Histograms of Oriented Gradients for Human DetectionThe detectMultiScale  function returns a 2-tuple of rects , or the bounding box (x, y)-coordinates of each person in the image, and weights , the confidence value returned by the SVM for each detection.

A larger scale  size will evaluate less layers in the image pyramid which can make the algorithm faster to run. However, having too large of a scale (i.e., less layers in the image pyramid) can lead to pedestrians not being detected. Similarly, having too small of a scale  size dramatically increases the number of image pyramid layers that need to be evaluated. Not only can this be computationally wasteful, it can also dramatically increase the number of false-positives detected by the pedestrian detector. That said, the scale  is one of the most important parameters to tune when performing pedestrian detection. I’ll perform a more thorough review of each of the parameters to detectMultiScale  in a future blog post.

Lines 32 and 33 take our initial bounding boxes and draw them on our image.

However, for some images you’ll notice that there are multiple, overlapping bounding boxes detected for each person (as demonstrated by Figure 1 above).

In this case, we have two options. We can detect if one bounding box is fully contained within another (as one of the OpenCV examples implements). Or we can apply non-maxima suppression and suppress bounding boxes that overlap with a significant threshold — and that’s exactly what Lines 38 and 39 do.

Note: If you’re interested in learning more about the HOG framework and non-maxima suppression, I would start by reading this introductory post on the 6-step framework. From there, check out this post on simple non-maxima suppression followed by an updated post that implements the optimized Malisiewicz method.

After applying non-maxima suppression, we draw the finalized bounding boxes on Lines 42 and 43, display some basic information about the image and number of bounding boxes on Lines 46-48, and finally display our output images to our screen on Lines 51-53.

Results of pedestrian detection in images

To see our pedestrian detection script in action, just issue the following command:

Below I have provided a sample of results from the detection script:

Figure 1: The first result of our pedestrian detection script.

Figure 2: The first result of our pedestrian detection script.

Here we have detected a single person standing next to a police car.

Figure 2: Detecting a single person in the foreground and another person in the background.

Figure 3: Detecting a single person in the foreground and another person in the background.

In the above example we can see a man detected in the foreground of the image, while a woman pushing a baby stroller is detected in the background.

Figure 3: An example of why applying non-maxima suppression is important.

Figure 4: An example of why applying non-maxima suppression is important.

The above image serves an example of why applying non-maxima suppression is important. The detectMultiScale  function falsely detected two bounding boxes (along with the correct bounding box), both overlapping the true person in the image. By applying non-maxima suppression we were able to suppress the extraneous bounding boxes, leaving us with the true detection

Figure 4: A second example demonstrating non-maxima suppression in action.

Figure 5: A second example demonstrating non-maxima suppression in action.

Again, we see that multiple false bounding boxes are detected, but by applying NMS we can remove them, leaving us with the true detection in the image.

Figure 5: Detecting pedestrians in a shopping mall.

Figure 6: Detecting pedestrians in a shopping mall.

Here we are detecting pedestrians in a shopping mall. Notice two people are walking away from the camera while another is walking towards the camera. In either case, our HOG method is able to detect the people. The larger overlapThresh  in the non_maxima_suppression  function ensures that the bounding boxes are not suppressed, even though they do partially overlap.

Figure 6: Detecting people in a blurred image.

Figure 7: Detecting people in a blurred image.

I was particularly surprised by the results of the above image. Normally the HOG descriptor does not perform well in the presence of motion blur, yet we are still able to detect the pedestrians in this image.

Figure 7: Detecting pedestrians outdoors, walking along the street.

Figure 8: Detecting pedestrians outdoors, walking along the street.

This is another example of multiple, overlapping bounding boxes, but due to the larger overlapThresh  they are not suppressed, leaving us with the correct person detections.

Figure 8: Detecting four members of a family.

Figure 9: Detecting four members of a family.

The above image shows the versatility of our HOG + SVM pedestrian detector. We are not only able to detect the adult male, but also the three small children as well. (Note that the detector is not able to find the other child hiding behind his [presumed to be] father).

Figure 9: Detecting a depiction of pedestrians.

Figure 10: Detecting a depiction of pedestrians.

I include this image last simply because I find it amusing. We are clearly viewing a road sign, likely used to indicate a pedestrian crossing. However, our HOG + SVM detector marks the two people in this image as positive classifications!


In this blog post we learned how to perform pedestrian detection using the OpenCV library and the Python programming language.

The OpenCV library actually ships with a pre-trained HOG + Linear SVM detector based on the Dalal and Triggs method to automatically detect pedestrians in images.

While the HOG method tends to be more accurate than its Haar counter-part, it still requires that the parameters to detectMultiScale  be set properly. In future blog posts, I’ll review each of the parameters to detectMultiScale , detail how to tune each of them, and describe the trade-offs between accuracy and performance.

Anyway, I hope you enjoyed this article! I’m planning on doing more object detection tutorials in the future, so if you want to be notified when these posts go live, please consider subscribing to the newsletter using the form below.

I also cover object detection using the HOG + Linear SVM method in detail inside the PyImageSearch Gurus course, so be sure to take a look!


If you would like to download the code and images used in this post, please enter your email address in the form below. Not only will you get a .zip of the code, I’ll also send you a FREE 17-page Resource Guide on Computer Vision, OpenCV, and Deep Learning. Inside you'll find my hand-picked tutorials, books, courses, and libraries to help you master CV and DL! Sound good? If so, enter your email address and I’ll send you the code immediately!

, , , , ,

322 Responses to Pedestrian Detection OpenCV

  1. Eugenio November 9, 2015 at 11:40 am #

    Dude, you´re an inspiration. Aside from that, what a magnificient blog this is, keep up the good work man!

    • Adrian Rosebrock November 9, 2015 at 12:20 pm #

      Thanks Eugenio! 😀

  2. Dan November 9, 2015 at 1:39 pm #

    Hey Adrian,

    Love the blog, and the books! Question for you: I haven’t tried this out yet, but I was curious how long it takes for this algorithm to run on the example images for a given platform. I’d like to get it running on a Pi2, but wasn’t sure if it’d run in a ‘reasonable’ time (maybe a couple Hz).


    • Adrian Rosebrock November 9, 2015 at 4:14 pm #

      The time it takes per image is really dependent on how fast your system is. I’ll be doing a full blog post on the tradeoffs between speed and accuracy next week, but for reasonably size images (in the range of 400-600px) it can take anywhere between 0.1s and 1.5s to process the image. But again, this is highly dependent on your choice of scale and window stride.

      • Dan November 10, 2015 at 10:06 am #

        Got it, thanks! Hoping to try this out in the next few days.

      • Ahsan Manzoor November 13, 2017 at 7:20 am #

        sir do you have a code for any object detection and tracking , or user defined object detection and tracking , excluding color detection

        • Adrian Rosebrock November 13, 2017 at 1:56 pm #

          Hi Ahsan — I have a few tutorials on object detection. Take a look at this one which includes the HOG + Linear SVM detector. This one on deep learning-based object detection.

          • Bhargav Kanakiya November 14, 2017 at 10:37 pm #

            Do you plan to do any tutorial on Multiple Object tracking?

            Your other tutorials are very good and helped me a lot. One for tracking would be such a value-add.


          • Adrian Rosebrock November 15, 2017 at 12:55 pm #

            Sure, I will add multi-object tracking to my queue. For what it’s worth, I have some basic multi-object trackers inside the PyImageSearch Gurus course.

  3. Carlos November 9, 2015 at 2:46 pm #

    So cool!

  4. Gadget/Steve November 9, 2015 at 3:45 pm #

    Erratic results for this one – some images it did very well but it didn’t cope with rotated photographs, (i.e. taken with the camera rotated), well, thought an A board was a pedestrian but none of the pedestrians were and best of all thought that a church spire & gravestone were 2 people.
    Can I suggest a couple of minor items, add the hash bang at the beginning of each example, preferably with the encoding i.e.:

    #!/usr/bin/env python

    and use the if __name__ == “__main__”: construct – just to get people who are new to python into the habit.

    An interesting blog which I am sure will get more people using python and OpenCV – keep it up, always look forward to the next.

    • Adrian Rosebrock November 9, 2015 at 4:19 pm #

      The Histogram of Oriented Gradients descriptor is not rotation invariant, hence it not detecting people in a rotated image (and why it was also confused by other objects in the images). You can read more about HOG in this post.

  5. Federico November 9, 2015 at 6:34 pm #

    Many thanks Adrian. Do you thing that is posible to use it with video from Pi camera?

    • Adrian Rosebrock November 10, 2015 at 6:22 am #

      Absolutely, just realize that the HOG + Linear SVM method is a bit slow of the parameters are tuned right so it likely won’t run in full real-time on teh Raspberry Pi.

  6. Filip November 12, 2015 at 2:39 pm #

    Could not find a version that satisfies the requirement imutils (from versions: ) No matching distribution found for imutils

    • Adrian Rosebrock November 13, 2015 at 5:41 am #

      Hey Filip — Are you sure you’re installing with:

      $ pip install imutils

  7. liuhengli November 22, 2015 at 12:11 pm #

    what to do can use it with video and real-time detection pedestrian

  8. Charles TRESSENS November 23, 2015 at 3:44 am #

    I have an issue with this code :
    I downloaded it and tried to execute it with the VM and a Raspberry Pi but I still got the same issue : it goes back to the shell without doing anything.

    I tried with “python -i images/person_010.bmp”.


    • Adrian Rosebrock November 23, 2015 at 6:41 am #

      Please see the example “USAGE” at the top of the script. You need to specify the path to the directory of images, not just the path to a single image. If you execute the script using this command, it will work:

      $ python --images images

      • Charles TRESSENS November 23, 2015 at 6:46 am #

        Thank you, it works perfectly,

        Have a great day,

        • Adrian Rosebrock November 23, 2015 at 11:27 am #

          No problem Charles!

      • pranith March 9, 2018 at 11:51 pm #

        I have executed the above command using windows cmd but there’s no output. It returns to command prompt

        • Adrian Rosebrock March 14, 2018 at 1:28 pm #

          Double-check that you are correctly supplying the path to the directory that contains your input images.

      • nijesh January 31, 2019 at 1:50 am #

        usage: [-h] -i IMAGES error: the following arguments are required: -i/–images

        this is the error I get. wht should I do now??

        • Adrian Rosebrock February 1, 2019 at 6:56 am #

          You need to supply the command line argument to the script. You should read this tutorial if you are new to command line arguments.

  9. JJ November 29, 2015 at 12:08 pm #

    Thanks for this! really interesting. I am wondering if this can be applied to real time webcam data? Thanks!

    • Adrian Rosebrock November 30, 2015 at 6:32 am #

      It definitely can, but you’ll want to make sure you have the parameters tuned properly for real-time performance. You’ll want want to look into using the cv2.VideoCapture function to read frames from your camera/video file, like I do in this post.

      • JJ November 30, 2015 at 6:45 pm #

        Thanks a lot! Will research a bit more! thanks for all the info!

  10. Jonathan Marcelino December 4, 2015 at 10:43 pm #

    Thank you for your hard work. I’m in high school and you tutorials inspire me to work on my python programming skills.

    • Adrian Rosebrock December 5, 2015 at 6:22 am #

      Hey Jonathan! Thanks for the comment — I’m glad the tutorials are helpful to you. Have an awesome day 🙂

  11. zhuxuekui December 14, 2015 at 12:51 am #


  12. Erick Dominguez December 14, 2015 at 12:35 pm #

    Hi Adrian, May I find differences between 2.4.9 or 3.0.0 opencv when I try out this code examples (code differences)? Won’t affect the version of openCV when I try your example?

    And by the way…. thank your for keeping posting amazing stuff on this blog! greetings!

    • Adrian Rosebrock December 14, 2015 at 5:31 pm #

      Hey Erick — the main difference you’ll run into is with the cv2.normalize function and the cv2.findContours method. You can read more about this here.

  13. Sainandan December 15, 2015 at 11:28 pm #

    Hey Adrian,just so you know,you’re doing a great job for us IP/CV noobs out here.Keep it up.

    My doubt was regarding the ready-made LSVM Detector in OpenCV used for ped-detection.

    Can you explain using what type of images and how many images,approximately was it pre-trained behind the scenes ?

    • Adrian Rosebrock December 16, 2015 at 6:36 am #

      The implementation itself is based on Dalal and Triggs paper, Histogram of oriented gradients for human detection. The documentation is not clear on what dataset they used to train the detector. They mention the publication multiple times which gives me the impression they used the same dataset as Dalal and Triggs, but that could easily be wrong.

  14. Qew7 January 28, 2016 at 7:52 am #

    Hi, im new to opencv
    I used your example to detect people on video, now i need to know is there a way how to tracke them, i know about camshift and meanshift, but i dk how to implement them an implement them to multiple people just detected, can you please help me?

    • Adrian Rosebrock January 28, 2016 at 3:38 pm #

      CamShift and MeanShift can get quite complicated for multiple objects. It’s honestly too much to try to explain in a single comment. I’ll try to do a blog post on this topic in the near future. In the meantime, you might want to look at correlation tracking which is part of the dlib library.

  15. Dan John February 24, 2016 at 5:02 am #

    Hi Adrian

    May i know if the same can be done for a video input from webcam?
    If so what are the necessary changes to be made? If you have already done this, would you mind sharing the code here.

    Thanks in advance. 🙂

    • Adrian Rosebrock February 24, 2016 at 4:34 pm #

      Yes, this code can certainly be modified to run via a webcam. I don’t have any code pre-updated, but you can update it yourself by using either the VideoStream class or the cv2.VideoCapture function. If you’re just getting started with OpenCV, you should check out Practical Python and OpenCV to get you started reading frames from a video stream.

  16. suresh February 25, 2016 at 8:30 am #

    can i get c++ opencv code for pedestrain counter and detection

    • Adrian Rosebrock February 25, 2016 at 4:40 pm #

      Sorry, I only have Python code at the moment.

  17. FilipeNunes March 4, 2016 at 1:04 pm #


    When i run python –images images it simple did nothing. any clue about the problem?

    • Adrian Rosebrock March 6, 2016 at 9:20 am #

      Make sure that (1) you have downloaded the source code + images to this post using the “Downloads” form and (2) ensure that the script is in the same directory as images.

  18. Sattra Rattanopas March 17, 2016 at 6:52 am #

    Dear Mr.Adrian Rosebrock

    Is it possible to count passing cars (back, left and right) by putting an action camera on the car’s roof?
    I’m doing a research on “How effective is car wrap advertising?” and i’m looking for a method to count the number of views.

    Thank you 🙂

    • Adrian Rosebrock March 17, 2016 at 10:35 am #

      So if I understand your question correctly, you would like to mount a camera on top of the roof of car, then take this car for a drive, and then count the number of cars that pass it as it’s driving around?

      That is indeed possible with computer vision — but it’s also a very challenging project since you’ll need to train a machine learning classifier to recognize cars at various viewing angles.

  19. Kju March 22, 2016 at 1:23 am #

    @Adrian: Could you do pedestrian detection with KCF (Kernel correlation filters) tracking?

    • Adrian Rosebrock March 22, 2016 at 4:23 pm #

      Yes, this is something that I plan on doing in the future 🙂

      • Kju March 24, 2016 at 10:58 pm #

        Yep, awesome.

  20. RIJUL PAUL March 30, 2016 at 1:12 am #

    Hi Adrian, could you just suggest me what I have to do to run this python script on Android Device/Platform

    • Adrian Rosebrock March 30, 2016 at 12:50 pm #

      I personally haven’t done any Android development, but you won’t be able to get a Python + OpenCV script to run on an Android device. You’ll need to rewrite the program to use Java + OpenCV.

  21. Namith April 1, 2016 at 1:21 pm #

    When I run the program, I get an error as follows

    Traceback (most recent call last):
    ImportError: No module named imutils.object_detection

    Pls help

    • Adrian Rosebrock April 1, 2016 at 3:11 pm #

      Hey Namtih, as the blog post mentions, make sure that you have imutils installed:

      $ pip install imutils

      If you do already have imutils installed, make sure you have upgraded to the latest version:

      $ pip install --upgrade imutils

      • Namith April 3, 2016 at 3:37 am #

        Hello. I installed imutils correctly.
        Now I have a problem with the –images part, which is the path to the directory that contains the list of images we are going to perform pedestrian detection on. The images I want to test on are in a a folder on the desktop. So how to do that?

        • Adrian Rosebrock April 3, 2016 at 10:21 am #

          You need to open up a command line and execute your script, supplying the path to the folder on your desktop:

          $ python --images /path/to/your/desktop

  22. baddrudge April 14, 2016 at 3:35 pm #

    I tried using this concept to build a porn detector by first using the pedestrian detector code to find rectangular regions that contain people then searching those rectangular regions to see if there’s a high concentration of pixels that correspond to skin hues.

    Unfortunately, this library doesn’t seem to detect people in porn images very well.

    • Adrian Rosebrock April 14, 2016 at 4:37 pm #

      In general, it’s going to be very challenging to train a HOG detector to detect pornographic images. Even relying on skin hues will be prone to errors. I would instead suggest utilizing deep learning; specifically, Convolutional Neural Networks for this task.

      • Majed September 20, 2019 at 6:14 pm #

        Thanks dude for your valuable efforts, is it possible to use the same code a video stored on my PC?

  23. RIJUL PAUL April 26, 2016 at 1:17 am #

    Hey Adrian, I want to stop VideoCapture and cv2.imshow window without using cv2.waitKey(). Is there any way you could suggest??

    • Adrian Rosebrock April 26, 2016 at 5:14 pm #

      Calling cv2.destroyAllWindows will close any windows. Then, you just need to call .release() on the VideoCapture object.

  24. RIJUL PAUL April 26, 2016 at 2:00 pm #

    Hello Adrian,Can this code be used for Real time captured Images? What are factors I have to tune for that??

    • Adrian Rosebrock April 26, 2016 at 5:07 pm #

      It certainly can. I cover how to tune the parameters for real-time video in the followup blog post.

      • RIJUL PAUL April 27, 2016 at 9:19 am #

        Thanks man.It really helped a lot :).

      • RIJUL PAUL May 3, 2016 at 2:29 am #

        Thank you for your previous advice.It really worked for me. Can you please give me a way out how to write/rewrite video in append mode??

  25. Robert May 4, 2016 at 2:58 pm #

    Great post! Im wondering how hard it is to track pedestrians? What I mean is: you have a camera stream with pedestrians. Assuming the above script works fine, I’m able to mark pedestrians.
    The question is how can I track individual personas? I want to mark them as numbers and track them as long as they’re visible. Once they’re gone, they’re gone. When a new pedestrian appears it bumps the counter and starts tracking that person…

    How to tackle that?

    • Adrian Rosebrock May 4, 2016 at 3:17 pm #

      Tracking is a bit more challenging, but absolutely doable. I would suggest starting with basic CamShift. However, the biggest downside is that CamShift (by definition) utilizes the histogram of the ROI (in this case, the histogram of the person region). This can easily fail. So, a better approach would be to use MOSSE or correlation trackers — I’ve been meaning to do a blog post on them, but just haven’t had the time.

      Finally, the easiest method that I suggest doing is computing the centroid of each ROI between frames. Compute the Euclidean distances between the centroid sets between frames. The centroid that minimizes the distance between the consecutive frames is thus your “match”. You can use this method to “track” objects as they enter and leave view of the camera.

      • Robert May 7, 2016 at 12:37 pm #

        Thanks a bunch 🙂 This sound a little bit like magic to me (a the moment 🙂 ).
        I’m new in the CV and very enyoing your blog. I’m buying your book very shortly as well and will start going deeper and deeper.

        My plan is to have an app that counts how many people enter a place… Like shop for example, or a house. The stretch goal is to detect faces and to check if that person Has visited the place before. It will be challenging but I hope that goal will let me enter the CV world. This is why I’m asking about tracking as I’ll have to know the “object” path and be sure that objects are counted correctly.

        But starting with your book, then applying a simple face recognition should give me confidence that I can achieve my personal goal.

        Looking forward to see more posts and to be selected to join search Gurus!

        • Adrian Rosebrock May 7, 2016 at 12:39 pm #

          That’s a great project Robert! Enjoy Practical Python and OpenCV. And I’ll be sure to let you know when I do object tracking tutorials in the future.

        • Samriz July 4, 2018 at 6:48 am #

          HI Robert, your application sounds really fascinating and Ive been trying to do something similar. Were you successful in creating it?

  26. Markus Reuter May 10, 2016 at 8:21 am #

    Hello Adrian!

    First of all, i´m a huge fan of your blog! Really helped me a lot in understanding computer vision ! 🙂

    I have a problem, i´m trying to do a pedestrian detection in realtime on a raspberry pi2 + picamera .

    I tried to use your code and change it in a way that it is possible to do live-detection of pedestrians with the picamera.

    But i get a lot of errors in my code that i dont understand, would it be possible to explain which part of the code one hast to change to get a real-time Pedestrian Detection? 🙂

    I wil say thank you in advance! 😉

    • Adrian Rosebrock May 10, 2016 at 8:24 am #

      In order to modify the pedestrian detection code to run on video streams (rather than single images), you first need to access your webcam/Raspberry Pi camera module. I would start by going through this blog post so you can access your camera.

      From there, you need to move the detectMultiScale and bounding box code to within your main loop that polls for new frames from the camera sensor. This can be accomplished by removing the code that loops over the paths of images on disk with the code that reads new frames. You’ll also need to change the cv2.imread function so that it reads the frame from the camera (again, discussed in the post linked to above). Other than that, not much else has to change.

  27. parkjungki May 26, 2016 at 2:01 pm #

    i’m very happy to study python with opencv .

    i saw that pi camera can’t use to this method but , i really hope to using pi camera.

    are there any way to use it?

    • Adrian Rosebrock May 27, 2016 at 1:31 pm #

      You can certainly use the Pi Camera for this code. All you need to do is access the video stream and then apply the pedestrian detector to each frame.

  28. Siva Krishna May 29, 2016 at 4:35 am #

    Hello Adrian,
    I am trying to implement and accelerate a detector for fallen people using HOG. So, as a part of that I have implemented HOG as in Dalal and Triggs paper. I used INRIA dataset and got good results. So, for fallen people I have taken the detection window as 128×64 and remaining parameters same as of standard HOG. I have collected some data set and trained. I have done hard mining also. The problem comes here. When I test it for a image(i.e. HOG is applied as a whole image) there were no detection’s at all. But, when I cut and save each possible(128×64) window of that image and applied HOG on those 128×64 sized images. Surprisingly there are many detection’s on that image. I don’t understand why this is happening. Any suggestions thanks in advance.

    • Adrian Rosebrock May 29, 2016 at 1:53 pm #

      So if I understand your question correctly, you cannot detect people in the original image? But if you crop out the person from the original image, all of a sudden you’re getting your detections? If so, you need to tune your detectMultiScale parameters. Specifically, your image pyramid size seems off.

  29. Nathasha May 31, 2016 at 4:49 am #

    Hello Adrian,

    First of all, thank you for your great post!

    As you mentioned in your post, we may have false-positives reported by the final object detector. Is it possible to extract the degree of uncertainty of detection from the HogDescriptor? or the probability that the detected object is a person? (i. e. the detected object is a person with x%)

    Thank you in advance.


    • Adrian Rosebrock May 31, 2016 at 3:41 pm #

      OpenCV won’t give you raw probabilities of the bounding boxes, but you can try checking the weights variable and thresholding them to ensure they are sufficiently large (where “sufficiently large” needs to be manually defined by you).

      • Nathasha June 1, 2016 at 6:10 am #

        Thank you very much for answering my question.

        Since “weights” gives the confidence value returned by the SVM for each detection, it can be considered as the degree of uncertainty of detection, right?

        Could you please kindly explain to me what do you mean by thresholding weights to ensure they are sufficiently large?
        Do I need to modify hitThreshold like this for example:
        (rects, weights) = hog.detectMultiScale(image, hitThreshold=1)


        • Adrian Rosebrock June 1, 2016 at 3:20 pm #

          Yes, you are correct — you can consider it as a degree of uncertainty. I would actually loop over the rects and weights individually and then check that way:

          You’ll need to manually define the YOUR_DEFINED_THRESHOLD

          • Nathasha June 2, 2016 at 5:20 am #

            Thank you very much for your help!


          • Adrian Rosebrock June 3, 2016 at 3:10 pm #

            No problem, happy I could help 🙂

  30. Bastronaut June 3, 2016 at 3:45 am #

    Thanks a lot Adrian, your posts are incredibly helpful to get started with opencv. I’ve hooked up the code to a video feed and intend to do a detection every n-th frame and experiment with the performance of real-time tracking I can obtain.

  31. Romanzo June 9, 2016 at 11:47 pm #

    Hey Adrian, I was wondering if you have pushed your HOG study a bit further and measure some accuracies using the INRIA dataset (as the images in used in your zip file are pretty easy)? And if yes which kinda of results you got at the end.

    I’ve computed some accuracies using DET curve with the INRIA dataset. The pyramid detection parameters in the paper and in default OpenCV are different.
    Using the HOG INRIA parameters (scale 1.2, window strides 8×8) gives poor results on the INRIA dataset.
    Using HOG OpenCV default parameters (scale 1.05, window strides 4×4) gives better results but not that great. I don’t really understand how they managed to get good results with a scale 1.2 which is really high. Maybe their code is different but this is just a scale.

    Anyway the the best i could get with OpenCV default value was (for FNR = FPPW) 0.35.

    I’ll appreciate if you could share any of your results! Thanks!

    • Adrian Rosebrock June 12, 2016 at 9:42 am #

      Great question, thanks for asking Romanzo. I personally have not benchmarked the OpenCV pedestrian detector against the INRIA dataset. This would make for a great experiment, as you suggested.

      • Romanzo June 13, 2016 at 9:01 pm #

        Ok no worries. If anyone else wants to share his results please let me know!

  32. tommy June 19, 2016 at 9:06 pm #

    How about the a disable human or an old whom bend over?

    • Adrian Rosebrock June 20, 2016 at 5:28 pm #

      You would need to train your own custom detector to detect someone who has fallen over or who has bent over. The default OpenCV pedestrian detector does not handle that.

  33. Chris June 27, 2016 at 12:18 am #

    Hi Adrian,
    First of all, thanks your great post. I used very source code from your blog to debug on my raspberry pi.
    I try to do a pedestrian detection in real time on raspberry pi 2 and picamera. but I have a problem:
    i use this code ( to view picamera, when i use image = cv2.imread(frame) it return error -> so i change to image = np.uint8(frame) it run but can’t detect any pedestrian in my frame.
    hope you explain why it can’t detect any pedestrian?
    thank you so much!!

    • Adrian Rosebrock June 28, 2016 at 10:58 am #

      If you’re accessing frames via the Picamera module, you don’t need to use the cv2.imread function as the image itself is stored in image.array read from the capture_continuous function.

    • Arsalan November 10, 2018 at 6:57 am #

      Hi Chris,

      hope you are doing well. I want to know have you implemented real-time pedestrian detection using Pi camera? if so can you help me ?

  34. Mark P June 30, 2016 at 2:37 pm #

    Your idea to reduce the size of the image to reduce false positives really helped! Thanks!

    • Adrian Rosebrock July 1, 2016 at 2:59 pm #

      Fantastic, glad to hear it Mark!

  35. Manish August 5, 2016 at 11:02 pm #


    Can I use it in combination with Arduino UNO to detect number of people in a room?
    What hardware would I need?

    • Adrian Rosebrock August 7, 2016 at 8:16 am #

      I personally have not used the Arduino Uno before, so I’m not sure about the required hardware. I will leave this comment here in hopes that another PyImageSearch reader can help you out.

  36. Alishba August 12, 2016 at 3:58 am #

    I downloaded the datasets and applied this code some of the results were inaccurate what’s the reason??

    • Adrian Rosebrock August 12, 2016 at 10:46 am #

      Hey Alishba — I’m honestly not sure why that may be. If you used the exact code and images from this post, you should be getting the same results. Which version of OpenCV are you using?

  37. Mona Jalal August 15, 2016 at 5:21 pm #

    Hi Adrian,

    Do you know how can I find an approximately large UAV/drone dataset for both low flying drones as well as high-flying drones?

    Thanks for the tutorial!!

    • Adrian Rosebrock August 16, 2016 at 1:00 pm #

      Hmmm, I’m not sure of such a dataset. If I ever run into any, I’ll be sure to let you know.

  38. Mona Jalal August 16, 2016 at 5:23 pm #

    Hi Adrian,
    I ran your algorithm on this photo and it just detects two of the people. What are some of your suggestions for improving it to detect all of the people? Also I’d like to know why HOG+SVM can’t detect everyone here if you could share any insight.
    This is the output of the algorithm:

  39. Henry August 22, 2016 at 10:25 pm #

    Is it possible to create a cheap system based on Arduino or similar to get the x- axis coordinates of pedestrians walking by?
    With or without OpenCV (have no experience in OpenCV)

    The camera resolution can be very low, resulting in about 20 coordinate positions in the field of view. Frame rate can be as low as 2 per seconds.

    Where could I get code in C for that?

  40. Chris August 26, 2016 at 2:05 am #

    Hi Adrian. first of all, i very thanks your post.
    i have applied your detection to run my script. but i have an error while detecting people lower than 100 with height. do you have any idea to fix this issue. i very very thanks so much your response.

    • Adrian Rosebrock August 29, 2016 at 2:10 pm #

      Keep in mind that the HOG detector was trained on 64 x 128 images, so if your image is smaller than 100 pixels, you’ll need to resize it to make it larger.

  41. Wanderson September 1, 2016 at 12:10 am #

    Hi Adrian,

    How do I evaluate the true positive rate (TP), TN, FP and FN segmentation. I’ve read quite a lot, but could not get something practical, that is, the programming aspect. Can you help me with some idea?

    • Adrian Rosebrock September 1, 2016 at 10:59 am #

      To evaluate these criteria you would need a training set and a testing set to evaluate against. Most object detection challenges provide testing datasets to evaluate this criteria — they also normally include scripts that you can use to measure TP, TN, FP, and FN using the “intersection over union” criteria.

      • Wanderson Souza September 8, 2016 at 9:49 pm #

        Thank you!

  42. lesly September 15, 2016 at 10:02 am #

    hello! thanks for the posts, they are really great! I am doing a project with computer vision and opencv, right now! and these post are great inspiration!
    actually, I am using external library such as: librealsense and coding in C++, trying to detect people in a scene.

    • Adrian Rosebrock September 16, 2016 at 8:26 am #

      Very cool! How are you liking the RealSense so far? I’ve given some thought to purchasing one.

  43. Martin October 4, 2016 at 7:53 am #

    Thank you so much for your great code!

    • Adrian Rosebrock October 6, 2016 at 7:02 am #

      No problem Margin, I glad you found it helpful 🙂

  44. Rencita October 19, 2016 at 1:54 am #

    Hi Adrian,
    Thanks for the great post. I m new to opencv, how to detect whether a person is wearing helmet or not? I’ve read quite a lot about it, but couldnt find any programming aspect. Can u help me with some idea?

    • Adrian Rosebrock October 20, 2016 at 8:47 am #

      Detecting whether or not a person is wearing a helmet can be broken down into two steps: (1) Detecting the person and the head region followed by (2) running an actual “helmet detector”. I would likely treat this is an object detection problem.

  45. FenghuiWang October 19, 2016 at 10:13 pm #

    Thanks for your great posts. Now I want use HOGDescriptor to detect human body. But it is slow because of large scale picture. I want to know how to use gpu accelerate calculation. Can you give me some idea

    • Adrian Rosebrock October 20, 2016 at 8:40 am #

      Unfortunately, Python cannot access the GPU + OpenCV functionality — that is C++ only. If you want to utilize your GPU for HOG then you’ll need to recode the program using C++.

  46. APURV SARVE October 23, 2016 at 2:15 am #

    hey Adrian,
    Will u please provide modified program for video streams by picamera or webcam .
    I have try this many times but it does not showing any output.
    thank u in advance.

    • Adrian Rosebrock October 23, 2016 at 10:08 am #

      While I’m certainly happy to help provide direction, suggestions, and tips I cannot write custom code outside the examples I’ve included in these tutorials. If you are having struggles modifying the script, that’s totally okay — but I would suggest using this tutorial as a starting point. I’m also not sure what you mean by “not showing any output”. What isn’t showing output? The frame on your screen? The pedestrian detector?

  47. James Brown October 24, 2016 at 12:02 am #

    You are a hero of digital image processing.
    I worked opencv with java a years ago so I have some experience in opencv, but not good enough.
    Now I am working with the project of recognizing the person from the crowd and I think your tutorial would give me a good idea.
    Thank you.

  48. James Brown October 24, 2016 at 12:05 am #

    Are there any ways to reduce the image size smaller than 64 X 128 ?

    • Adrian Rosebrock October 24, 2016 at 8:28 am #

      Are you referring to the HOG descriptor? If so, no. The HOG descriptor is defined in terms of an image ROI (width and height). This is a fixed value. Changing the spatial dimensions would alter the HOG descriptor and you would ultimately have to retrain the detector. My suggestion is that if you want to alter the dimensions, you should train your own detector.

  49. Aamer November 7, 2016 at 1:52 pm #

    The post is really awesome. I have a request. Can you please train an svm model using the external data set rather than using the inbuild and pretrained svm for HOG descriptors. By that we can get a very generalized idea of how we can build our own detection models. Thanks.

  50. Sapan Bhatt November 29, 2016 at 7:16 am #

    Hi Adrian thanks for the post. I juts want to ask whether can i make a bird detection code usign OPen CV on raspberry PI.. ?

    • Adrian Rosebrock November 29, 2016 at 8:03 am #

      Detecting a bird in an image can likely be accomplished a few ways. In extremely controlled conditions you might be able to get away with background subtraction. Since that is unlikely I would suggest training your own custom object detector.

      I would likely start with HOG + Linear SVM. More advanced methods would entail using deep learning to localize the bird in an image/video stream.

  51. Nor December 6, 2016 at 5:27 am #

    hey adrian,
    i’ve tried the pedestrian detection. i save each of the bounding box separatly in a folder for more processing.if the image is save by number(1.bmp,2.bmp ect). how can i access all the image in numerical order for processing?

    • Adrian Rosebrock December 7, 2016 at 9:44 am #

      I would sort the images by their filename, for example:

      paths = sorted(list(paths.list_images("path/to/directory")))

      You could also name both your bounding box files and .bmp files the same (only with different file extensions). Then, loop over the images, and then construct the path to your bounding box file.

  52. Siva Prasad Raju December 12, 2016 at 1:39 am #

    Hi Adrian,

    I have tried the pedestrian detection. I need to call this pedestrian code from another c++ code. How to call this python pedestrian code from c++ code. Please send me the c++ code for pedestrian detection.

    • Adrian Rosebrock December 12, 2016 at 10:27 am #

      Sorry Siva, I only support Python on this blog, not C++. Best of luck with your project!

  53. quang December 22, 2016 at 3:51 am #

    Hi Adrian, thanks for this tutorial. If i use this code in raspberry with camera pi, how can i do?

  54. Doudz December 24, 2016 at 9:55 am #

    Looks amazing but where is the source code?

    • Adrian Rosebrock December 31, 2016 at 1:47 pm #

      You can download the source code to this post by using the “Downloads” section just below the “Summary” section.

  55. Abhishek Kumar December 27, 2016 at 4:38 am #

    Hey Adrian .. I am using this HOG descriptor to detect people in room in real time ….. but there is a lot of false detection in the video but when i do it with the same parameters for an image it is fairly good …….. what should I do ???

    • Adrian Rosebrock December 31, 2016 at 1:37 pm #

      Are your test images from still frames of the video? Or from an entirely different source? Also, I would suggest you read this post on tuning .detectMultiScale parameters.

  56. w.salhi January 24, 2017 at 1:57 pm #

    how to cut out a someone in a picture using python or java or c#

  57. Taimur Bilal February 6, 2017 at 1:42 pm #

    Hi there.
    I have been reading your tutorials and they have been a lot of help for me in understanding OpenCV motion detection and pedestrian detection. Your tutorials on both these topics are comprehensive and well explained, Every step and every 2-3 lines of codes you logically explain and justify in your tutorials, which builds a lot of understanding for a student like me who is new to Computer Vision.

    I basically read your tutorials on real-time motion detection and the one on pedestrian detection. I combined them both for an embedded platform Odroid XU4 with Linux running on it. I wrote the code in OpenCV C++, using the approach you used in your tutorials. I’m trying to currently classify moving objects as persons in real time from a live camera feed and it works.

    I want to discuss more about this project I’m doing for which I combined realtime motion detection and person detection. May I discuss it with you on email?

    Hoping to have the opportunity to discuss it with you.

    • Adrian Rosebrock February 7, 2017 at 9:06 am #

      Hey Taimur, it’s great to hear that the PyImageSearch tutorials have been helpful, awesome!

      As for your project, I do my best to answer any emails and questions I receive. However, please keep in mind that I do prioritize my inbox based on who is/is not a PyImageSearch customer.

      • Taimur Bilal February 19, 2017 at 5:17 am #

        Hey Adrian. I was revisiting and studying this tutorial and I just had a question in my mind. You’ve resized the image to 400 pixels width and then give it as as an input to the “hog.detectMutliScale()” method.

        What if instead of resizing to 400 pixels width, we feed the detector an image that is resized to 64×128 pixels? how about using a size double i.e 128×256? Will this affect the performance or reduce false positive in any way?

        • Adrian Rosebrock February 20, 2017 at 7:47 am #

          I think it would be helpful to understand how the HOG + Linear SVM detector works. If you resize your input image to 64×128 pixels then you’re essentially making the assumption that there is (or is not) a person at the center of your image. We pass in larger images to the detector so we can apply an image pyramid + sliding window and detect people at multiple locations and scales of the image. If you pass in an image that is the exact same size as your HOG filter, then you can’t achieve multiple scales and multiple locations.

  58. Lucas Santana February 12, 2017 at 9:26 am #

    Hi, great tutorial!
    I’m running the code in the cv enviroment, but nothing is hapenning. The images should have the boxes, should open othen window?
    Nothing is hapenning.

    • Adrian Rosebrock February 13, 2017 at 1:43 pm #

      Hey Lucas — check your task manager and ensure the Python task is running and consuming CPU. If you’re trying to detect pedestrians in a large image it will take a long time. Try resizing your image such that the maximum dimension is 600 pixels.

  59. Dave February 17, 2017 at 3:11 am #


    Is there a possibility that you might be able to do an article on people detection in situations where a person’s legs are obscured, for example they are standing behind a wall, and all that you can see is their upper body?

    Thank you,

    • Adrian Rosebrock February 20, 2017 at 8:01 am #

      Hey Dave — I will certainly consider this as a future blog post topic, although a person detector that robust would likely require deep learning techniques.

  60. David Halland February 25, 2017 at 2:13 am #

    Hi, when i’m trying to run this program. It pass the line 20 and jump to the end. Can’t you help me with it.
    for imagePath in paths.list_images(args[“images”]):

    • Adrian Rosebrock February 27, 2017 at 11:20 am #

      Hey David — it sounds like you might not be passing in the --images command line argument. You can read more about how to use command line arguments here.

  61. Tom February 27, 2017 at 6:43 am #

    Hi Adrian,
    How does one train a model to use in ‘setSVMDetector’?
    In previous comments you mention that it’s covered in the gurus course, there we put the car model into cpickle, can this be used for the multi-scale detector?

    • Adrian Rosebrock February 27, 2017 at 11:04 am #

      If you want to use the setSVMDetector method you would need to train your detector using OpenCV’s built-in tools. This is the only way to create a model compatible with setSVMDetector. Otherwise, you should be training your model using other machine learning libraries such as scikit-learn, etc. (highly recommended).

      • Tom February 27, 2017 at 11:35 am #

        By your experience with OpenCV, dlib and scikit, how does the scikit detector perform in terms of speed? From my few experiments it appears that OpenCV’s default pedestrian detector is much faster than dlib’s but don’t know much about scikit.

        • Adrian Rosebrock February 27, 2017 at 12:01 pm #

          Using scikit-learn’s method is slower in most cases (Python versus compiled C/C++). I’ve found dlib’s detector to be much more accurate and just as speedy as OpenCV’s though.

  62. Qavir March 7, 2017 at 11:52 pm #

    Hi Adrian,
    The tutorial is very good, i followed the steps which u told in the tutorial. But when i run “python –images images” on the terminal, it is just exiting showing nothing.


    • Adrian Rosebrock March 8, 2017 at 1:04 pm #

      Make sure you download the source code + example images to this blog post using the “Downloads” section of the tutorial. Then, make sure you supply a valid path to the images directory. From there you should see the output.

      • Qavir March 8, 2017 at 11:39 pm #

        Hi Adrian,
        Thank you so much for the solution, i am able to see the output now.
        I have one more question, what changes do i need to do in the code to see the output using camera, actually i did some changes in the code but getting so many errors.
        So can u please help me out in this


        • Adrian Rosebrock March 10, 2017 at 3:54 pm #

          If you want to use the camera you should use cv2.VideoCapture or my VideoStream class. I also discuss accessing webcams in detail inside Practical Python and OpenCV. If you are new to OpenCV, I would highly recommend that you start there.

  63. Qazi Tehmas March 8, 2017 at 3:04 am #

    hey Adrian when i run this code i got error
    “usage: [-h] -i IMAGES error: argument -i/–images is required”
    please tell what is this?

    • Adrian Rosebrock March 8, 2017 at 1:02 pm #

      You need to read up on command line arguments before proceeding.

      • Qazi Tehmas March 14, 2017 at 2:19 pm #

        i have read this but unable to solve the problem… will you please guide me more?

        • maria March 16, 2017 at 11:31 am #

          have the same problem. please guide us more, forgive me, new in python 🙁

          • Adrian Rosebrock March 17, 2017 at 9:28 am #

            It is okay if you are new to Python, but you need to educate yourself. Don’t try to run before you walk. Take the time to read up on command line arguments and how to use them — basic command line usage is a requirement when you start to get into the more advanced areas of computer science.

  64. Parth March 20, 2017 at 12:05 pm #

    Hey Adrian,
    Thanks a lot for the blog. Super helpful stuff!
    I tried working on the Pedestrian Detection using HoG + SVM. Despite it running smoothly for the images in your dataset, it did not give very accurate results to the images that I used. I am attaching the link to the output screenshots. If there is any reason you can find as to why I am getting such results, it would be really helpful. I have tried tweaking scale, winstride and padding but to no avail.
    This is the link to the images:

    • Adrian Rosebrock March 28, 2017 at 1:08 pm #

      The pedestrian detectors provided by OpenCV are not used to detect people in dense crowds. For that you should consider applying face detection; specifically methods that are tuned to dense crowds. Deep learning has had some success here.

      • Biranchi April 14, 2017 at 11:37 am #

        Hi Adrian,

        Isn’t the number of bounding boxes after suppression (green color) gives the count of the people in the image?

        Is it correct to say len(pick) gives the number of people present in the image ?


        • Adrian Rosebrock April 16, 2017 at 8:56 am #

          Provided that the corrections are not false-positives, then yes, calling len(pick) would give you the total number of people in an image.

  65. Nitin April 13, 2017 at 2:49 pm #

    Can I run this code using webcam instead of using pi camera?

  66. Lamia April 17, 2017 at 1:02 pm #

    Hi Adrian …
    how to detect face with python and opencv one time in video

    • Adrian Rosebrock April 19, 2017 at 12:58 pm #

      You would need to access your video and then apply the .detectMultiScale method to each individual frame.

  67. Eric April 26, 2017 at 11:21 am #

    I am using the cv2.HOGDescriptor, almost verbatim to your code, with some attempts at tweaks. However, I keep getting false positives on a ceiling fan with dangling chain (like this I’ve tried tweaking the scale in line 29, or image width in line 24. But too much and I don’t get a real human in the background of the foreground ceiling fan. Any ideas on other tweaks? Sorry if I’m missing the obvious? How about an automatic exclusion for the fan, since it will always be there?

    • Adrian Rosebrock April 28, 2017 at 9:50 am #

      Hi Eric — if the ceiling fan will always be there, simply crop it from the image using NumPy array slicing. Another option would be to simply hardcode your script to ignore people detections that overlap with the ceiling fan (x, y)-coordinates.

  68. Eduardo Lopes May 4, 2017 at 4:12 pm #

    Hi, I got the code and tried it out with a video instead of a set of images, but unfortunately no person were detected. Is there any limitation when using video as input instead of images? Thanks!

    • Adrian Rosebrock May 8, 2017 at 12:43 pm #

      It highly depends on the parameters to detectMultiScale as well as the video contents themselves. I would suggest reading reading this post on how to tune detectMultiScale parameters.

  69. Alyaa May 11, 2017 at 12:39 pm #

    Hi Adrian. I cant running the coding. Here what it said “Gtk-WARNING **: cannot open display: :1.0”. Why is it? and what should i do? Thanks

    • Adrian Rosebrock May 15, 2017 at 9:05 am #

      What system are you using? Laptop/desktop/Raspberry Pi? What operating system? Are you SSH’ing into your system before running the command?

  70. hazem May 13, 2017 at 7:43 pm #

    hi adrian could you tell me how to load any people dataset to python to make HOG on it

    • Adrian Rosebrock May 15, 2017 at 8:43 am #

      I’m not sure what you mean by “load any people dataset”, but if your goal is to train a custom HOG + Linear SVM detector, I cover how to do so inside the PyImageSearch Gurus course.

  71. Marc May 16, 2017 at 4:43 am #

    Can this detect a sitting person? I’ve read Dalal and Triggs paper and it was stated that the HOG was trained in a dataset containing mostly upright people. Also, how much obstruction can this handle? coz i am hoping that this can detect a person sitting in a monoblock chair even if the camera is placed behind him and sees only the back view.

    P.S. the monoblock chair would look like this

    P.P.S. Thank you so much coz i have noticed that you’ve been responding to comments for over a year already. That’s a lot of time you’ve shared 🙂

    • Adrian Rosebrock May 17, 2017 at 10:01 am #

      No, the pedestrian detector provided by OpenCV is meant to detect people who are standing. More specifically, people who are either walking towards or away from the camera. Instead, you should consider training your own custom object detector for people who are sitting.

      • Marc May 17, 2017 at 12:14 pm #

        okay thank you so much! I hope you can do a detailed tutorial for training a detector in the future 🙂

        • Adrian Rosebrock May 18, 2017 at 11:55 am #

          I actually cover how to build custom object detectors in detail (with lots of code) inside the PyImageSearch Gurus course.

          • Marc May 19, 2017 at 2:20 am #

            okay thanks! 🙂

  72. Ben May 20, 2017 at 5:12 pm #

    Hi, how can I detect just the upper body of a person

    • Ben May 20, 2017 at 5:12 pm #

      Oh and thank you very much of this tutorial, it really helps us newbies get an understanding of opencv and HOG

  73. Santhiya May 29, 2017 at 2:02 pm #

    I have to call script from another script. For this when “ –images images” is called, I get an error stating invalid syntax. I am a novice in python. I tried suppressing and removing one argument, but in vain. I want to reduce the two arguments – “–images images ” to one argument or either with no other argument, may I know how to do this

    • Adrian Rosebrock May 31, 2017 at 1:20 pm #

      I wouldn’t suggest calling from another script. Why not incorporate the code from into your current script?

  74. Putri May 31, 2017 at 9:17 pm #

    Hi Adrian
    i have a qestion..

    How to detect car in image??
    I’ve tried to change the existing scale, can not.
    What should I do ??

    • Adrian Rosebrock June 4, 2017 at 6:30 am #

      It really depends on the types of cars you are trying to detect and the viewpoint of the camera. A good starting point would be training a HOG + Linear SVM detector. I cover the implementation of the HOG + Linear SVM detector inside the PyImageSearch Gurus course.

  75. Giacomo Randazzo June 14, 2017 at 8:01 am #

    Hi, thanks for the great tutorial!

    I would like to ask you if a HOG Classifier is still the best solution for human detection. Have faster technique been implemented in OpenCV or dlib yet? I could find Deformable Part Model but it is said to be slow. I’ve also found Waldboost in the OpenCV source code but it lacks documentation.

    Would you still use HOG + Linear SVM detector for running human body recognition on smartphones?

    Thanks in advance,

    • Adrian Rosebrock June 16, 2017 at 11:31 am #

      It really depends on your application. HOG + Linear SVM is a linear filter, meaning that it doesn’t handle rotation and changes in viewpoint angle well. If you are working on a project where you know the viewing angle of the person will be (relatively) fixed, HOG + Linear SVM is the best way to go. If you want to detect people at various viewing angles and rotations, something like SSD, YOLO, or Faster R-CNN would be a better approach.

  76. Michael June 15, 2017 at 3:31 am #

    Hi Adrian, I’ve found that on pictures that have around 5,6 people, this detection doesn’t catch all people. Can I ask what’s the correct percentage for HOG? And further more, can I customize it to train it to recognize better? Thanks.

    • Adrian Rosebrock June 16, 2017 at 11:22 am #

      I’m not sure what you mean by the “correct percentage” for HOG. Typically you measure the true-positive and false-positive detection rate on the training set the HOG + Linear SVM detector was trained on. If you want to customize and train your own HOG + Linear SVM detector, I review how to do so (with lots of code) Inside the PyImageSearch Gurus course.

  77. Srihari June 20, 2017 at 9:13 am #

    Hello Adrian,
    I have tried your code.I works well with the images you have attached.But not with any other images.
    If I have to work with any other images
    “Do I have to do any preprocessing with the image ?”

    • Adrian Rosebrock June 20, 2017 at 10:42 am #

      The HOG + Linear SVM detector works based on inputs similar on what it was trained on (frontal views of people). You might be able to obtain higher detection accuracy by tuning your detectMultiScale parameters.

  78. Ganesh June 26, 2017 at 8:36 pm #

    Hello Adrian,
    usage: [-h] -i IMAGES error: argument -i/–images is required
    it is the error that I am getting when I run it.
    could you help me solve this issue.

    • Adrian Rosebrock June 27, 2017 at 6:10 am #

      Please read up on command line arguments, how they work, and how to use them before continuing.

  79. Irum July 3, 2017 at 4:40 am #

    Hey Adrian is there anyway other than OpenCV to detect upper body cause this people detector works good for full body but detects only 20-30% times when persons half body is visible. I want to detect both full and half body. Opencv gives a lot of false positives.

  80. upendra July 7, 2017 at 3:13 am #

    Great tutorial
    can we detect a particular object like if i want to detect a car in image??

  81. abdul muteeb July 22, 2017 at 8:11 am #

    hiii i like your work seriously but im getting some error please guide me about those errors
    ImportError: No module named imutils.object_detection

    • Adrian Rosebrock July 24, 2017 at 3:42 pm #

      Make sure you install the imutils library on your system:

      $ pip install --upgrade imutils

  82. abdul muteeb July 22, 2017 at 8:14 am #

    i am working on people detection in my final year project please can you help me 🙂 :)…
    please can you make a tutorial video for me for live streaming people detection

    i seriously very thankful to you
    please help me i really need you its about my studies

    • Adrian Rosebrock July 24, 2017 at 3:42 pm #

      Hi Abdul — congrats on working on your final year project, that’s fantastic. I am sure you are excited to graduate. I cover how to access video streams and detect objects inside Practical Python and OpenCV. I would suggest you start there and learn how to access video streams. You can even use the boilerplate code I provide — all you need to do is swap in the pedestrian detection Haar cascade and you’ll be all set.

  83. Alex July 25, 2017 at 1:38 am #

    Hi… when I key in “python –images image” in the command prompt console, nothing happened. Please advise.

    Thank you.

    • Adrian Rosebrock July 28, 2017 at 10:09 am #

      You are missing the “s” in images”:

      $ python --images images

      Always double-check your image paths.

      • ts March 5, 2018 at 10:17 am #

        it does not give any output it finishes by saying

        Initialize OpenCL runtime…

        • Adrian Rosebrock March 7, 2018 at 9:25 am #

          In that case it sounds like an issue with OpenCL. Can you try recompiling and reinstalling OpenCV without OpenCL support?

          • Aron April 20, 2018 at 4:17 pm #

            Hi what exactly dooes it mean to recompile? Are there certain program files you have to get rid of in the opencv package before installation?

          • Adrian Rosebrock April 25, 2018 at 6:21 am #

            By “recompile” I mean follow one of my tutorials on OpenCV. You should not need to uninstall OpenCV. The new install will overwrite it.

  84. Radu Cimpian August 5, 2017 at 6:03 pm #

    Hi Adrian!
    First I would like to thank you for this great tutorial. I have a small problem… I’m using a raspberry pi 3, and I want to detect pedestrian in real time from a webcam video input. My problem is that I have a BIG delay in my video…I mean, if I rise my rand, on screen only after about 10 seconds it will appear. I would like to ask your opinion, should I try with another camera? is my raspberry the problem, the processing power is not good enough? (this is like a general question, is it possible to accomplish real time pedestrian detection with a webcam and a raspberry pi? using this tutorial, of course)

    Many thanks 🙂

    • Adrian Rosebrock August 10, 2017 at 9:07 am #

      Two things:

      1. Reduce your input frame dimensions. The smaller your frame is, the less data there is to be processed, and therefore your processing pipeline will run faster.

      2. Use threading to help increase your FPS throughput.

  85. erik August 18, 2017 at 4:29 am #

    How to crop the after NMS output

    • Adrian Rosebrock August 21, 2017 at 3:52 pm #

      What do you mean by “crop” after the NMS output? Extract the ROI? You accomplish that using simple NumPy array slicing:

      roi = image[startY:endY, startX:endX

      I discuss the basics of computer vision and OpenCV inside Practical Python and OpenCV. I would highly suggest starting there if you’re just getting started.

  86. Vishal September 3, 2017 at 3:03 am #

    I am getting error:the following arguments are required: -i/–images
    How to solve this error please help me

    • Adrian Rosebrock September 5, 2017 at 9:29 am #

      Please read the comments before posting. I have addressed this question multiple times in the comments section. In particular, take a look at my reply to “Qazi Tehmas” above.

  87. Vinuta K September 12, 2017 at 3:25 am #


    We are getting the following error while executing the code.

    usage: [-h] -i I error: argument -i/-C:\Users\vinuta\Downloads\pedestrian-detection\images is required

    Please help us with it.

    • Adrian Rosebrock September 12, 2017 at 7:12 am #

      Please read the other comments before posting. I have addressed this question multiple times. In particular you should read my response to “Ganesh”.

  88. Ankit Kwatra October 5, 2017 at 3:16 pm #

    Its really great comprehensive blog thanks a lot..
    Could you please let me know that if we want to get the output of number of person detected as count in each image in excel or csv it possible? If yes can you share some thaughts on that.


    • Adrian Rosebrock October 6, 2017 at 4:59 pm #

      A CSV file is simply a comma separated file. Open a file pointer and use the .write method to write the image filename and len(rects) to the file.

  89. Jigs October 12, 2017 at 4:34 am #

    You are Man of the Century , Great work and keep Rocking


    • Adrian Rosebrock October 13, 2017 at 8:46 am #

      Thank you Jigs! 🙂

  90. Ying October 13, 2017 at 9:01 am #

    Hi Adrian,

    How can I use opencv to generate heatmap for pedestrian detection? for example, if one area has more pedestrians passed by, then it shows red colour, and other areas show lighter colour? Is that possible to apply the heatmap to real-time live view?

    • Adrian Rosebrock October 13, 2017 at 9:19 am #

      There are a number of ways to generate these types of heatmaps. You could use the probabilities associated with a set of predictions to construct your heatmap as the sliding window + image pyramid will generate higher probability bounding boxes surrounding the pedestrian. I would suggest reading up on HOG + Linear SVM detector works.

      • Ying October 13, 2017 at 9:29 am #

        thank you so much Adrian for your quick response!

  91. Jaddoa November 30, 2017 at 11:45 pm #

    Hi I’m wondering if possible to use same method HOG + SVM with Infrared thermal image

    • Adrian Rosebrock December 2, 2017 at 7:30 am #

      Yes, it is. Please see this paper.

  92. Patrick Duffy December 6, 2017 at 2:11 pm #

    Any thoughts on how to associate a specific pedestrian with a given detection frame, and track the movement of each person? For example, if 3 persons were in a video stream, each one has an initial detection frame, labled 1,2,3, and as they move the results would keep these differentiated according to their initial detection sequence, keeping pedestrian 1 associated with frame 1, etc…

    The goal is to have the ability to track a specific pedestrian through the images as they move. I need some idea on how to do this, but the current algorithm in this blog seems to reset the detection output for each video frame.

    • Adrian Rosebrock December 8, 2017 at 5:05 pm #

      Yes, take a look at object tracking algorithms, in particular “correlation trackers”. The dlib library has an implementation as well.

      • Red December 15, 2017 at 5:16 am #

        can you tested your model cascade to detect human from top view camera!

  93. Red December 14, 2017 at 3:40 am #

    I am very interested in your site and I take you to the handle of which you explained the codes.

    I have a question, I tested your code to detect people from a top view camera, but it does not detect me anything. Have you ever treated this problem? or trained a basis of persons with this type of view?
    thank you in advance!

  94. Sarah December 21, 2017 at 7:14 am #

    Very interesting post!
    I wondered if there is a simple way to output the coordinates of the final bounding box as I am looking at trying to detect the depth of a detected object using a stereo camera? I am also curious if there are other similar detection packages for things such as cars etc.

    • Adrian Rosebrock December 22, 2017 at 6:51 am #

      Can you elaborate on the “final bounding box”? Are you speaking in terms of the real-world coordinates? You would need to calibrate your camera first and then convert the detected coordinates to real-world coordinates.

      As for your section question, yes, that is possible. Take a look at this post on object detection to familiarize yourself. I go into detail on how to train your own custom object detectors inside my book, Deep Learning for Computer Vision with Python.

      • Sarah January 7, 2018 at 11:20 am #

        I would only require the bounding boxes coordinates in regards to the image. So how many pixels to the right and up it is. The camera I have works by returning the distance at a queried pixel.

        Thanks for the help, I’ll get to reading!

  95. William Terceiro December 30, 2017 at 1:43 am #

    Hi Adrian,

    Is it possible to combine the Pedestrian Detection code explained in this blog with the other blog that you did about Object Tracking ?

    I am working in a project where I need to detect people entering and going out from a room. I was thinking to use the both examples to acomplish this feature in this project.

    Thank you very much for this great tutorial.


    William Terceiro

    • Adrian Rosebrock December 31, 2017 at 9:44 am #

      The object tracking blog post you are referring to used color-based tracking which may or may not work for your particular project. For tracking that is not dependent on color I would instead suggest correlation tracking.

  96. Jane January 8, 2018 at 12:11 pm #


    Thank you for this amazing tutorial. I am using Anaconda Spyder for python. If I’m not mistaken conda doesn’t allow command line arguments. In that case, how should I give the path?

  97. Nithin January 15, 2018 at 1:45 am #

    hey Adrian,
    I was wondering if we can detect pedestrian in the first few frames in a video and then track those detected pedestrians in the following frames rather than detecting in all frames. (i am a noobie, i am working on a project that track the pedestrians and detect any suspicious events) thanks in advance 🙂

    • Adrian Rosebrock January 15, 2018 at 9:10 am #

      Absolutely. Look into dedicated tracking algorithms such as “centroid tracking” and “correlation tracking”. This will help with the tracking component. From there you’ll want to research “activity recognition”. This will likely involve training a machine learning model to recognize what is “suspicious”.

  98. Chris January 18, 2018 at 7:30 am #

    Hi Sir, I’m a student and I’ve been doing a research for object recognition in python and opencv using haarcascade but to be honest I really have a hard time on making the codes since I am newbie. I am making a simple hair detection and recognize it whether it is curl or straight. I’ve already done the training of my datasets and have this xml file but I don’t really have an idea on how to start my codes. Can I ask for your help Sir? Thank you and God bless

    • Adrian Rosebrock January 18, 2018 at 8:41 am #

      Hey Chris — if you have already trained your Haar cascade you should be able to load it via cv2.CascadeClassifier as I discuss in in this blog post. If you’re new to OpenCV and need to learn the fundamentals (where I also discuss Haar cascades), be sure to take a look at my book, Practical Python and OpenCV.

  99. Gaya Jay January 25, 2018 at 2:41 am #

    I have run above code in PyChram IDE with python 3.5.o. Following error is occurred

    usage: [-h] -i IMAGES error: the following arguments are required: -i/–images

    Can you please tell me, how should I correct it?

    • Adrian Rosebrock January 25, 2018 at 3:48 pm #

      You can either:

      1. Delete the argument parsing code and define an args dictionary
      2. Set your command line arguments via your PyCharm’s project preferences

      I personally prefer executing the script via the command line but that’s just my preference.

      • ts March 5, 2018 at 10:05 am #

        Sir i have tried the second way but it says invalid syntax while passing the arguments

  100. Athul February 6, 2018 at 1:58 pm #

    Great work Sir. Can you provide code for pedestrian/human detection from a video file input such as in formats like .mp4 and also for detection from live web cam streaming

    • Adrian Rosebrock February 8, 2018 at 8:45 am #

      You can use other tutorials here on the blog to build this project. Take a look at this tutorial to get you started.

  101. Jason February 12, 2018 at 3:02 am #

    Hi Adrian,

    Many thanks for the pyimage series.

    I’m trying to implement HOG+SVM myself before using more mature implementations in OpenCV or skimage, just to get a better understanding of these. I’m still bit confused about the SVM part, in particular, suppose I’ve got N number of training samples and I used a Gaussian kernel with SVM to train the model, that would give me a parameter vector W of R^N, am I right? Then to predict on a new sample, I still need to compute the Gaussian similarity between the new data and all N of training samples, to get the feature vector (F of R^N), and then compute the dot product of F with my parameter (W), right?

    That means a trained SVM model needs to carry with it the whole training set, so the larger the training set, the bigger the data size it needs to carry?

    Any help would be much appreciated.

    • Adrian Rosebrock February 12, 2018 at 6:15 pm #

      A few things here:

      1. You nearly always use a linear SVM for HOG + SVM detectors. Anything but a linear SMV will be too slow.

      2. The SVM technically doesn’t have to carry the entire training set, just the support vectors.

      3. The features here come from the HOG algorithm.

      If you’re interested in learning more about HOG + Linear SVM, including how to implement the algorithm from scratch, I demonstrate how to implement step-by-step, with detailed code and explanations, inside the PyImageSearch Gurus course.

  102. Abhishek February 15, 2018 at 12:47 am #

    Hi Adrian,
    Can it be possible for me to run this program in Raspberry Pi 3 Module B ? or Banana Pi?
    Please guide me through this.

    • Adrian Rosebrock February 18, 2018 at 10:04 am #

      This script will run just fine on the Raspberry Pi 3 once you have OpenCV installed. I have a number of OpenCV install guides available here.

  103. ts March 5, 2018 at 9:57 am #

    Hi Adrian

    Please provide the code for videos instead for images in HOG using svm

    • Adrian Rosebrock March 7, 2018 at 9:25 am #

      You can access video via the cv2.VideoCapture function or through my VideoStream class. I am confident you can make the HOG detection code work with videos.

  104. cuixiaoyang March 9, 2018 at 9:19 am #

    Hi Adrian

    I have run above code. Following error is occurred

    File “”, line 30, in
    image = imutils.resize(image, width=min(400, image.shape[1]))
    AttributeError: ‘NoneType’ object has no attribute ‘shape’

    Can you please tell me, how should I correct it?

    • Adrian Rosebrock March 9, 2018 at 9:23 am #

      Typically you will see NoneType errors such as this one when your path to the input image and cv2.imread is incorrect. Double-check the path to your input image. Additionally, I would recommend you read through this post on NoneType errors and how to resolve them.

  105. Ishaan March 18, 2018 at 2:45 pm #

    Hey Adrian ,
    I have properly followed the syntax for executing the program but it doesn’t run anything nor does it give any error in the CMD , Just returns to the next line Could you just anything ?

    • Adrian Rosebrock March 19, 2018 at 5:06 pm #

      Hey Ishaan — what is your len(paths.list_images(args["images"]))? My guess here is that the length of the list is zero, implying that your path to the input directories of images is invalid. Be sure to double-check your input path.

      • juvy amor maravillas galindo April 7, 2018 at 12:10 pm #


        i found this error “no module named imutils.object_detection

        • Adrian Rosebrock April 10, 2018 at 12:42 pm #

          Make sure you install “imutils” on your system:

          $ pip install --upgrade imutils

  106. ravi April 13, 2018 at 3:21 pm #


    I am able to run the code now. But I have trouble when I use real time images for pedestrian detection. Now i am running the source code with an image input from Index of /~pinz/data/GRAZ_01 . But if i use any other images it is not working. Can you please provide me if any documentation is there what if I wanted to use any other image source for this code?

    Would be really helpful since I am working on a project on this.


    • Adrian Rosebrock April 16, 2018 at 2:37 pm #

      Hi Ravi — can you clarify what you mean by “is not working” in this context? Are you getting an error message of any kind? Is the script exiting gracefully but the pedestrians are not being detected?

  107. dani May 6, 2018 at 3:35 pm #

    Hi Adrian,

    I can’t get this to work with my own pics and videos. Which is probably down to the size. 160 x 120. What would be the ideal settings for these. Or even smaller – say you wanted to check a roi that was retrieved from motion detection.


    • Adrian Rosebrock May 9, 2018 at 10:15 am #

      In the absolute worst case you would need to train your own model to recognize pedestrians on your own image size and ideally from the same camera sensor. Can you tell me a bit more about this project first?

  108. Shreyans Sharma May 11, 2018 at 2:10 am #

    Hi Adrian,

    This code works well for people who are fully visible as in the entire body is visible, how can we tweak it to detect even torsos or partially visible body?

    Thanks, amazing work man.

    • Adrian Rosebrock May 14, 2018 at 12:10 pm #

      There are a few ways to approach this but you should consider training your own custom object detector on body regions you would like to detect. Most importantly, these images should contain partial views/occlusions.

  109. nooler May 20, 2018 at 11:39 am #

    Can someone leave his code example with changed paths, because my program is not running, and I don’t know why, I`ve changed path to my images, but its still not working.

    • Adrian Rosebrock May 22, 2018 at 6:09 am #

      Make sure you have updated your paths correctly. Additionally, I would recommend reading this guide on Python command line arguments.

  110. Caíque Destro June 5, 2018 at 10:00 pm #

    Hello, Adrian

    I’m tryin to use your code using de output of a camera stream. For some reason, it only works with the images that I took with my phone camera. I tryed with many webcams and with the one that I’m really intend to use, the PiCamera (raspberry PI). Can you help me with that?

    Thank you

    • Adrian Rosebrock June 7, 2018 at 3:14 pm #

      Hey Caíque — that is indeed odd behavior but my best guess is that you did not install OpenCV with the video codec support for your other video files. Make sure you follow one of my install tutorials.

  111. Muhammad Abdullah June 6, 2018 at 5:07 pm #

    Hi Adrian,

    I have followed your OpenCV blogs and they are amazing and really thanks for sharing your knowledge with community.
    I want to use your pedestrian-detection for video but i am unable to make it happen can you help me in this regard how can i use it for a video.
    Your help will be appreciated.

  112. jatinpal singh June 12, 2018 at 5:07 am #

    hi adrian tried ur code but I am getting the error as I am unable to resolve even after searching on google. error: the following arguments are required: -i/–image

    I have given correct input

  113. David Chabalala June 16, 2018 at 9:23 am #

    Hi i ran the code and i get nothing
    But an exit code -1073740791 and my python version is 3.6.2 and OpenCV version 3.4.1 on Windows 8
    I really tried to scan through the comments hoping someone might have come across this and solve it

    • Adrian Rosebrock June 19, 2018 at 8:57 am #

      Hey David — can you try to insert some “print” statements to determine if a particular line is throwing that error? Or does the script start and immediately exit?

  114. rukku June 28, 2018 at 4:25 am #

    Hi Adrain,
    First of all thank you for this tutorial.
    Currently I’m doing human detection and tracking.Can you please help me how can I perform this tutorial in a video file.
    Hope you will reply ..
    Thank you

    • Adrian Rosebrock June 28, 2018 at 7:57 am #

      In a video file? Or a video stream?

      • rukku July 2, 2018 at 12:18 am #

        Thank you for your reply.Currently I need to perform it on video file.And I will be happy if you can say something about how to perform it on video stream as well.

        • rukku July 2, 2018 at 12:24 am #

          Hi Adrian.
          can you also tell me whether there is anyway to use argument parser in jupyter notebook. As Im using jupyter notebook Im unable to use Argument parser.

          • Adrian Rosebrock July 3, 2018 at 8:29 am #

            Make sure you read this blog post on argument parsing. Inside the post I include a simple approach to modify the code to work with Jupyter Notebooks.

        • Adrian Rosebrock July 3, 2018 at 8:28 am #

          See my previous reply to your comment.

      • rukku July 3, 2018 at 12:14 am #

        In a video file.Can you explain it to me how?

        • Adrian Rosebrock July 3, 2018 at 7:13 am #

          You’ll want to take a look at the cv2.VideoCapture function. You can see an example of it in this blog post. I also discuss how to use it inside Practical Python and OpenCV.

          • rukku July 5, 2018 at 2:19 am #

            Hi Adrain, First of all thank you for your reply. But Ididn’t understand how to use argument parser in jupyter notebook
            Thank you

          • Adrian Rosebrock July 5, 2018 at 6:14 am #

            See this article, including my reply to the very first comment on the post.

  115. Anders Weile July 2, 2018 at 2:06 pm #

    Very impressive work and well written book – not done yet, but working on it.
    I have a question, that is somewhat semi-related to OpenCV.
    I use OpenCV on a RPi Zero and my script works as expected. However, I would like to make it launch automatically at startup.
    I have tested multiple methods – rc.local, .bashrc and more, but yet unsuccesful. The programs doesn’t start.

    . /home/pi/.profile
    workon cv
    python3 /home/pi/recognizer.pi &
    exit 0

    When running rc.local manually, it starts up, alright – but when booting, nothing happens.
    I have tried to output each line to a logfile, but they are never created.

    Have you been though this? And if so, would you care to share your knowledge, please?

    • Adrian Rosebrock July 3, 2018 at 7:22 am #

      I have an entire tutorial dedicated to running scripts on reboot. Be sure to read the post and then read through the comments as well as other solutions are presented. I hope that helps!

      • Anders Weile July 3, 2018 at 2:41 pm #

        Thank you for your answer – I must have missed that. However; I just found out, it was due to a permission problem. I’ll post my solution there, since it doesn’t quite look like some of the other solutions, mentioned in the comments.

  116. Aissatou July 12, 2018 at 5:35 pm #

    Hello, thank you for your tutorials, they are always great. But I wanted to know if this code is applicable on videos? Thank you

    • Adrian Rosebrock July 13, 2018 at 4:54 am #

      Yes, you can still use this code on videos, you would just apply the pedestrian detector to every frame of the video.

      • Aissatou July 15, 2018 at 10:02 am #

        Thank you

  117. Ken Adams July 19, 2018 at 12:56 pm #

    Thanks a ton!

  118. Abdessamad gori July 29, 2018 at 8:43 pm #

    hi Adrian thank you for your tutorials. How can I make a model setSVMDetector to set the Support Vector Machine to be trained َANIMAL such as horse , camel , cow

    • Adrian Rosebrock July 31, 2018 at 9:54 am #

      The first step would be to actually train a model to detect and recognize animals. I discuss how to train your own image classifiers and detectors inside the PyImageSearch Gurus course — I suggest you start there.

  119. Sahil Makandar August 11, 2018 at 12:10 pm #

    Hello Adrian sir ,

    Can you please share any pipeline to detect and track pedestrians in low light or at night time.

  120. rabah August 28, 2018 at 5:10 am #

    Hi Adrain,
    First of all thank you for this tutorial. I am doing a project of human detection using 3D Lidar (Velodyne 32), I could create depth images (rayscale images) from the sensor, and I could detect human using CNN.
    I was wondering if I can apply your tutorial on my input data, i have these two questions:
    -Can you please help me how can I perform this tutorial on the depth images.
    -How can I track the human detected on the image.

    thanks in advance

    • Adrian Rosebrock August 28, 2018 at 3:10 pm #

      Hey Rabah — this tutorial won’t work out of the box with depth images. I don’t have any tutorials on depth imagery. I can try to cover the topic in the future but I’m honestly not sure if/when that may be.

  121. Yadnyesh October 5, 2018 at 11:13 pm #

    Hey Adrian,
    Great work man, I do have some questions?
    What all changes have to be made if I don’t use argparse?

    • Adrian Rosebrock October 8, 2018 at 9:47 am #

      You could hardcode the arguments into a Python dictionary named “args” or you could create a configuration file and update the code to load the configuration file. It’s pretty much your choice.

  122. Kasra Mokhtari October 19, 2018 at 12:24 am #

    Hello everyone! I really appreciate if if you can help me how to use this code! I am very new to python and image processing. Thank you so much for your help!

  123. Sam T February 1, 2019 at 10:34 am #

    how to make it work for real time systems?

  124. Sam February 4, 2019 at 1:30 am #

    Hi Adrian,

    I have tried this script for vedio files , but it detects other objects as Humans and draws boxes over them . Could you please suggest what parameters are to be modified or any updation is needed elsewhere in the code?

  125. Jacob April 19, 2019 at 4:05 pm #

    Howdi Adrian,

    I am trying to run this script on Jetson Nano. When I try to run it no error is given but no windows are shown with the image before/after as it should.

    I used this script:
    Is there something missing in that script when it runned cmake that might have done so it doesn’t work?

    PS: I have checked so all packages is able to be imported (except print_function) with the python console without any errors.

    • Adrian Rosebrock April 25, 2019 at 9:24 am #

      Hey Jacob — I’ll be covering how to configure and use your Jetson Nano in a separate tutorial, but to be honest, it sounds like your path to the input video is incorrect, thus there are no frames to process so OpenCV exits early.

  126. Veco April 23, 2019 at 11:07 am #

    Hi, I am trying to apply this method to an video stream pedestrian detection. But this error appears :

    AttributeError: ‘cv2.HOGDescriptor’ object has no attribute ‘dedetectMultiScale’

    Can you help me?

    • Adrian Rosebrock April 25, 2019 at 8:47 am #

      The name of the function is “detectMultiScale”, not “dedetectMultiScale”.

  127. Jacob April 24, 2019 at 2:52 pm #


    I have copied the code and try to run it and it successfully execute without any error but no windows are shown with the images and the bounding box. Have something changed in the latest updates of CV or some other library dependencies lately so the code needs to be updated?

    • Adrian Rosebrock April 25, 2019 at 8:39 am #

      Try to use the “Downloads” section of the post to download the source code instead — you may have introduced an error when copying and pasting.

  128. Fran Sola July 16, 2019 at 11:40 am #

    Hi Adrian, thanks a lot for the tutorial,

    I’m currently trying to make an application to detect people but from above, so full-bodies detectors may fail. The question is, how can I use this method to detect just heads? is it possible? or I should use another method/detector?

    Thank you!

  129. Tales October 24, 2019 at 9:48 pm #

    Is there any way to get confidence in the form of probability?

  130. Emerson Souza October 31, 2019 at 10:44 am #

    Could we detect people and do a crosswalk count and send the red light a longer time commensurate with the number of people waiting for the crossing?

  131. David O January 5, 2020 at 10:09 pm #

    Hi Adrian,

    First of all: congrats for such a great series of posts you have made… you are always very technical but at the same time clear, making CV something that can be understood (as much as possible) in a easy way … awesome material !
    And i’m always learning a lot from every post you made.

    And this one (Pedestrian Detection) in special… is very aligned to my research line … (people detection inside buildings…)
    One thing i would like to understand more is how is the comparison (in terms of accuracy and performance …) of usage of HOG +SVM as you show here… and the usage of detection with ANNs (Mobile Net… etc…). Is this approach faster ? Can we use CUDA in the HOG processing ?

    This is very central to my research because i’m working on video instead of images… so the speed of detection is crucial… do i really appreciate your comments…

    Again thanks for you brilliant work !

    • Adrian Rosebrock January 16, 2020 at 10:54 am #

      Hey David — I would suggest you join the PyImageSearch Gurus course to learn more about object detection algorithms.

      Deep learning-based object detectors will be more accurate than Haar cascades/HOG + Linear SVM, but slower. Haar is faster than HOG + Linear SVM but less accurate. It’s all about balancing speed vs. accuracy for your specific project.

  132. Adrian Rosebrock October 12, 2018 at 9:16 am #

    I’m happy to help, Weng! 🙂


  1. HOG detectMultiScale parameters explained - PyImageSearch - November 16, 2015

    […] Last week we discussed how to use OpenCV and Python to perform pedestrian detection. […]

  2. Best Pedestrian Crossing In The World | Cross Roadsc center - October 10, 2018

    […] Pedestrian Detection OpenCV […]

Before you leave a comment...

Hey, Adrian here, author of the PyImageSearch blog. I'd love to hear from you, but before you submit a comment, please follow these guidelines:

  1. If you have a question, read the comments first. You should also search this page (i.e., ctrl + f) for keywords related to your question. It's likely that I have already addressed your question in the comments.
  2. If you are copying and pasting code/terminal output, please don't. Reviewing another programmers’ code is a very time consuming and tedious task, and due to the volume of emails and contact requests I receive, I simply cannot do it.
  3. Be respectful of the space. I put a lot of my own personal time into creating these free weekly tutorials. On average, each tutorial takes me 15-20 hours to put together. I love offering these guides to you and I take pride in the content I create. Therefore, I will not approve comments that include large code blocks/terminal output as it destroys the formatting of the page. Kindly be respectful of this space.
  4. Be patient. I receive 200+ comments and emails per day. Due to spam, and my desire to personally answer as many questions as I can, I hand moderate all new comments (typically once per week). I try to answer as many questions as I can, but I'm only one person. Please don't be offended if I cannot get to your question
  5. Do you need priority support? Consider purchasing one of my books and courses. I place customer questions and emails in a separate, special priority queue and answer them first. If you are a customer of mine you will receive a guaranteed response from me. If there's any time left over, I focus on the community at large and attempt to answer as many of those questions as I possibly can.

Thank you for keeping these guidelines in mind before submitting your comment.

Leave a Reply