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 SectionPedestrian 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 detect.py
, and we’ll get coding:
# import the necessary packages from __future__ import print_function from imutils.object_detection import non_max_suppression from imutils import paths import numpy as np import argparse import imutils import cv2 # construct the argument parse and parse the arguments ap = argparse.ArgumentParser() ap.add_argument("-i", "--images", required=True, help="path to images directory") args = vars(ap.parse_args()) # initialize the HOG descriptor/person detector hog = cv2.HOGDescriptor() hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
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:
$ pip install imutils
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:
$ pip install --upgrade imutils
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:
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:
# loop over the image paths for imagePath in paths.list_images(args["images"]): # load the image and resize it to (1) reduce detection time # and (2) improve detection accuracy image = cv2.imread(imagePath) image = imutils.resize(image, width=min(400, image.shape[1])) orig = image.copy() # detect people in the image (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(8, 8), scale=1.05) # draw the original bounding boxes for (x, y, w, h) in rects: cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a # fairly large overlap threshold to try to maintain overlapping # boxes that are still people rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) pick = non_max_suppression(rects, probs=None, overlapThresh=0.65) # draw the final bounding boxes for (xA, yA, xB, yB) in pick: cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2) # show some information on the number of bounding boxes filename = imagePath[imagePath.rfind("/") + 1:] print("[INFO] {}: {} original boxes, {} after suppression".format( filename, len(rects), len(pick))) # show the output images cv2.imshow("Before NMS", orig) cv2.imshow("After NMS", image) cv2.waitKey(0)
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:
- 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).
- 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 Detection. The 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:
$ python detect.py --images images
Below I have provided a sample of results from the detection script:
Here we have detected a single person standing next to a police car.
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.
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
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.
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.
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.
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.
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).
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!
What's next? We recommend PyImageSearch University.
86 total classes ā¢ 115+ hours of on-demand code walkthrough videos ā¢ Last updated: October 2024
ā ā ā ā ā 4.84 (128 Ratings) ā¢ 16,000+ Students Enrolled
I strongly believe that if you had the right teacher you could master computer vision and deep learning.
Do you think learning computer vision and deep learning has to be time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a degree in computer science?
Thatās not the case.
All you need to master computer vision and deep learning is for someone to explain things to you in simple, intuitive terms. And thatās exactly what I do. My mission is to change education and how complex Artificial Intelligence topics are taught.
If you're serious about learning computer vision, your next stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here youāll learn how to successfully and confidently apply computer vision to your work, research, and projects. Join me in computer vision mastery.
Inside PyImageSearch University you'll find:
- ✓ 86 courses on essential computer vision, deep learning, and OpenCV topics
- ✓ 86 Certificates of Completion
- ✓ 115+ hours of on-demand video
- ✓ Brand new courses released regularly, ensuring you can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all code examples in your web browser ā works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 540+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Summary
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!
Download the Source Code and FREE 17-page Resource Guide
Enter your email address below to get a .zip of the code and 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!
Eugenio
Dude, youĀ“re an inspiration. Aside from that, what a magnificient blog this is, keep up the good work man!
Adrian Rosebrock
Thanks Eugenio! š
Dan
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).
Thanks!
Dan
Adrian Rosebrock
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
Got it, thanks! Hoping to try this out in the next few days.
Ahsan Manzoor
sir do you have a code for any object detection and tracking , or user defined object detection and tracking , excluding color detection
Adrian Rosebrock
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
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.
Thanks.
Adrian Rosebrock
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.
Carlos
So cool!
Gadget/Steve
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
#coding:utf-8
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
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.
Federico
Many thanks Adrian. Do you thing that is posible to use it with video from Pi camera?
Adrian Rosebrock
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.
Filip
Could not find a version that satisfies the requirement imutils (from versions: ) No matching distribution found for imutils
š
š
š
Adrian Rosebrock
Hey Filip — Are you sure you’re installing with:
$ pip install imutils
liuhengli
what to do can use it with video and real-time detection pedestrian
Adrian Rosebrock
Please see my followup post on tuning the parameters to detectMultiScale.
Charles TRESSENS
Hey,
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 detect.py -i images/person_010.bmp”.
Thanks,
Adrian Rosebrock
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 detect.py --images images
Charles TRESSENS
Thank you, it works perfectly,
Have a great day,
Adrian Rosebrock
No problem Charles!
pranith
I have executed the above command using windows cmd but there’s no output. It returns to command prompt
Adrian Rosebrock
Double-check that you are correctly supplying the path to the directory that contains your input images.
nijesh
usage: detect.py [-h] -i IMAGES
detect.py: error: the following arguments are required: -i/–images
this is the error I get. wht should I do now??
Adrian Rosebrock
You need to supply the command line argument to the script. You should read this tutorial if you are new to command line arguments.
JJ
Thanks for this! really interesting. I am wondering if this can be applied to real time webcam data? Thanks!
Adrian Rosebrock
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
Thanks a lot! Will research a bit more! thanks for all the info!
Jonathan Marcelino
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
Hey Jonathan! Thanks for the comment — I’m glad the tutorials are helpful to you. Have an awesome day š
zhuxuekui
thks
Erick Dominguez
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
Hey Erick — the main difference you’ll run into is with the
cv2.normalize
function and thecv2.findContours
method. You can read more about this here.Sainandan
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
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.
Qew7
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
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.
Dan John
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
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.suresh
can i get c++ opencv code for pedestrain counter and detection
Adrian Rosebrock
Sorry, I only have Python code at the moment.
FilipeNunes
Hello,
When i run python detect.py –images images it simple did nothing. any clue about the problem?
Adrian Rosebrock
Make sure that (1) you have downloaded the source code + images to this post using the “Downloads” form and (2) ensure that the
detect.py
script is in the same directory asimages
.Sattra Rattanopas
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
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.
Kju
@Adrian: Could you do pedestrian detection with KCF (Kernel correlation filters) tracking?
Adrian Rosebrock
Yes, this is something that I plan on doing in the future š
Kju
Yep, awesome.
RIJUL PAUL
Hi Adrian, could you just suggest me what I have to do to run this python script on Android Device/Platform
Adrian Rosebrock
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.
Namith
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
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
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
You need to open up a command line and execute your script, supplying the path to the folder on your desktop:
$ python detect.py --images /path/to/your/desktop
baddrudge
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
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
Hi,
Thanks dude for your valuable efforts, is it possible to use the same code a video stored on my PC?
RIJUL PAUL
Hey Adrian, I want to stop VideoCapture and cv2.imshow window without using cv2.waitKey(). Is there any way you could suggest??
Adrian Rosebrock
Calling
cv2.destroyAllWindows
will close any windows. Then, you just need to call.release()
on theVideoCapture
object.RIJUL PAUL
Hello Adrian,Can this code be used for Real time captured Images? What are factors I have to tune for that??
Adrian Rosebrock
It certainly can. I cover how to tune the parameters for real-time video in the followup blog post.
RIJUL PAUL
Thanks man.It really helped a lot :).
RIJUL PAUL
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??
Adrian Rosebrock
I’m not sure how to append frames to a pre-existing video, but I would start with this blog post on writing video to file.
Robert
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
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
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
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
HI Robert, your application sounds really fascinating and Ive been trying to do something similar. Were you successful in creating it?
Markus Reuter
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
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 thecv2.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.parkjungki
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
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.
Siva Krishna
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
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.
Nathasha
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.
Regards,
Adrian Rosebrock
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
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)
Regards,
Adrian Rosebrock
Yes, you are correct — you can consider it as a degree of uncertainty. I would actually loop over the
rects
andweights
individually and then check that way:You’ll need to manually define the
YOUR_DEFINED_THRESHOLD
Nathasha
Great!
Thank you very much for your help!
Regards
Adrian Rosebrock
No problem, happy I could help š
Bastronaut
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.
Romanzo
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
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
Ok no worries. If anyone else wants to share his results please let me know!
tommy
How about the a disable human or an old whom bend over?
Adrian Rosebrock
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.
Chris
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 (https://pyimagesearch.com/2015/03/30/accessing-the-raspberry-pi-camera-with-opencv-and-python/) 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
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 inimage.array
read from thecapture_continuous
function.Arsalan
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 ?
Mark P
Your idea to reduce the size of the image to reduce false positives really helped! Thanks!
Adrian Rosebrock
Fantastic, glad to hear it Mark!
Manish
Hi,
Can I use it in combination with Arduino UNO to detect number of people in a room?
What hardware would I need?
Adrian Rosebrock
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.
Alishba
I downloaded the datasets and applied this code some of the results were inaccurate what’s the reason??
Adrian Rosebrock
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?
Mona Jalal
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
Hmmm, I’m not sure of such a dataset. If I ever run into any, I’ll be sure to let you know.
Mona Jalal
Hi Adrian,
I ran your algorithm on this photo http://imgur.com/a/Z7vL7 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: http://imgur.com/a/KVyr5
Thanks,
Mona
Adrian Rosebrock
See my followup blog post on detectMultiScale parameters.
Henry
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?
Chris
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
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.
Wanderson
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
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
Thank you!
lesly
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
Very cool! How are you liking the RealSense so far? I’ve given some thought to purchasing one.
Martin
Thank you so much for your great code!
Adrian Rosebrock
No problem Margin, I glad you found it helpful š
Rencita
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
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.
FenghuiWang
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
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++.
APURV SARVE
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
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?
James Brown
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.
James Brown
Are there any ways to reduce the image size smaller than 64 X 128 ?
Adrian Rosebrock
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.
Aamer
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.
Adrian Rosebrock
I detail the general steps required to train a HOG + Linear SVM detector inside this post. For a detailed review (plus lots of code) on how to implement the detector, please see the PyImageSearch Gurus course.
Sapan Bhatt
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
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.
Nor
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
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.
Siva Prasad Raju
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
Sorry Siva, I only support Python on this blog, not C++. Best of luck with your project!
quang
Hi Adrian, thanks for this tutorial. If i use this code in raspberry with camera pi, how can i do?
Adrian Rosebrock
You would need to combine the code from this blog post along with the code from accessing the Raspberry Pi camera.
Doudz
Looks amazing but where is the source code?
Adrian Rosebrock
You can download the source code to this post by using the “Downloads” section just below the “Summary” section.
Abhishek Kumar
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
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.w.salhi
how to cut out a someone in a picture using python or java or c#
Taimur Bilal
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.
Regards
Adrian Rosebrock
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
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
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.
Lucas Santana
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
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.
Dave
Adrian,
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
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.
David Halland
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
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.Tom
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?
Thanks,
Tom
Adrian Rosebrock
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 withsetSVMDetector
. Otherwise, you should be training your model using other machine learning libraries such as scikit-learn, etc. (highly recommended).Tom
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
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.
Qavir
Hi Adrian,
The tutorial is very good, i followed the steps which u told in the tutorial. But when i run “python detect.py –images images” on the terminal, it is just exiting showing nothing.
Thanks
Adrian Rosebrock
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
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
Thanks
Adrian Rosebrock
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.Qazi Tehmas
hey Adrian when i run this code i got error
“usage: hog.py [-h] -i IMAGES
hog.py: error: argument -i/–images is required”
please tell what is this?
Adrian Rosebrock
You need to read up on command line arguments before proceeding.
Qazi Tehmas
i have read this but unable to solve the problem… will you please guide me more?
maria
have the same problem. please guide us more, forgive me, new in python š
Adrian Rosebrock
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.
Parth
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:
https://drive.google.com/folderview?id=0BzhHeiPBf-EFYzVHSVBLd193ZEk
Parth
Hello,
I want to build a project which counts number of people present in crowd. So what should I do for this?
When I run this code(pedestrian detection) on crowd image it detects only one person.
link to image is :
https://www.google.co.in/search?q=crowd+images&espv=2&tbm=isch&imgil=JtYf1xncEYEwXM%253A%253Byhp76JVFUOR1UM%253Bhttp%25253A%25252F%25252Fwww.psychedforbusiness.com%25252F2016%25252F11%25252F07%25252Fimprove-hiring-psychology-crowd%25252F&source=iu&pf=m&fir=JtYf1xncEYEwXM%253A%252Cyhp76JVFUOR1UM%252C_&usg=__q_pSsyneUYLcIHzweCuF2Ppx7cQ%3D&biw=1301&bih=654&ved=0ahUKEwjj_qyJ_vPSAhXBuY8KHaPlCHQQyjcIMg&ei=BZvXWKPoCMHzvgSjy6OgBw#imgrc=JtYf1xncEYEwXM:
So please help with this.
Adrian Rosebrock
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
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 ?
Thanks
Adrian Rosebrock
Provided that the corrections are not false-positives, then yes, calling
len(pick)
would give you the total number of people in an image.Nitin
Can I run this code using webcam instead of using pi camera?
Adrian Rosebrock
Yes, absolutely. If you would like to access webcam or Raspberry Pi video streams, please refer to this blog post.
Lamia
Hi Adrian …
how to detect face with python and opencv one time in video
Adrian Rosebrock
You would need to access your video and then apply the
.detectMultiScale
method to each individual frame.Eric
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 http://www.homedepot.com/p/Hunter-Caraway-44-in-Indoor-Brushed-Nickel-Ceiling-Fan-with-Light-52081/204468778). 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
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.
Eduardo Lopes
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
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 tunedetectMultiScale
parameters.Alyaa
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
What system are you using? Laptop/desktop/Raspberry Pi? What operating system? Are you SSH’ing into your system before running the command?
hazem
hi adrian could you tell me how to load any people dataset to python to make HOG on it
Adrian Rosebrock
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.
Marc
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 http://lorenzfurniture.com/wp-content/uploads/2013/04/Classic-Monoblock-Chair.jpg
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
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
okay thank you so much! I hope you can do a detailed tutorial for training a detector in the future š
Adrian Rosebrock
I actually cover how to build custom object detectors in detail (with lots of code) inside the PyImageSearch Gurus course.
Marc
okay thanks! š
Ben
Hi, how can I detect just the upper body of a person
Ben
Oh and thank you very much of this tutorial, it really helps us newbies get an understanding of opencv and HOG
Adrian Rosebrock
I would likely suggest training a custom HOG + Linear SVM detector just for upper bodies. I detail how to train custom object detectors inside the PyImageSearch Gurus course.
Santhiya
I have to call detect.py script from another script. For this when “detect.py –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
I wouldn’t suggest calling detect.py from another script. Why not incorporate the code from detect.py into your current script?
Putri
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
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.
Giacomo Randazzo
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,
Giacomo
Adrian Rosebrock
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.
Michael
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
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.
Srihari
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
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.
Ganesh
Hello Adrian,
usage: detect.py [-h] -i IMAGES
detect.py: 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
Please read up on command line arguments, how they work, and how to use them before continuing.
Irum
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.
Adrian Rosebrock
You would want to train your own custom object detector to only detect upper body regions. I detail how to train your own custom object detectors (with lots of Python code) inside the PyImageSearch Gurus course.
upendra
Great tutorial
can we detect a particular object like if i want to detect a car in image??
Adrian Rosebrock
I would suggest training your own custom object detector. I discuss training your custom object detectors in detail PyImageSearch Gurus course.
upendra
Thank you Adrian you are great
Adrian Rosebrock
No problem, I’m happy I could help! š
abdul muteeb
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
Make sure you install the imutils library on your system:
$ pip install --upgrade imutils
abdul muteeb
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
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.
Alex
Hi… when I key in “python detect.py –images image” in the command prompt console, nothing happened. Please advise.
Thank you.
Adrian Rosebrock
You are missing the “s” in images”:
$ python detect.py --images images
Always double-check your image paths.
ts
it does not give any output it finishes by saying
Initialize OpenCL runtime…
Adrian Rosebrock
In that case it sounds like an issue with OpenCL. Can you try recompiling and reinstalling OpenCV without OpenCL support?
Aron
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
By “recompile” I mean follow one of my tutorials on OpenCV. You should not need to uninstall OpenCV. The new install will overwrite it.
Radu Cimpian
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
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.
erik
How to crop the after NMS output
Adrian Rosebrock
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.
Vishal
I am getting error:the following arguments are required: -i/–images
How to solve this error please help me
Adrian Rosebrock
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.
Vinuta K
Sir,
We are getting the following error while executing the code.
usage: detect.py [-h] -i I
detect.py: error: argument -i/-C:\Users\vinuta\Downloads\pedestrian-detection\images is required
Please help us with it.
Adrian Rosebrock
Please read the other comments before posting. I have addressed this question multiple times. In particular you should read my response to “Ganesh”.
Ankit Kwatra
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 format..is it possible? If yes can you share some thaughts on that.
Thanks!!!
Adrian Rosebrock
A CSV file is simply a comma separated file. Open a file pointer and use the
.write
method to write the image filename andlen(rects)
to the file.Jigs
You are Man of the Century , Great work and keep Rocking
Thanks
Adrian Rosebrock
Thank you Jigs! š
Ying
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
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
thank you so much Adrian for your quick response!
Jaddoa
Hi I’m wondering if possible to use same method HOG + SVM with Infrared thermal image
Adrian Rosebrock
Yes, it is. Please see this paper.
Patrick Duffy
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
Yes, take a look at object tracking algorithms, in particular “correlation trackers”. The dlib library has an implementation as well.
Red
can you tested your model cascade to detect human from top view camera!
Red
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!
Adrian Rosebrock
The HOG + Linear SVM detector provided by OpenCV was trained on frontal views of people. It was not trained for overhead views of people. I would suggest training your own HOG + Linear SVM detector which I cover inside the PyImageSearch Gurus course.
Sarah
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.
Thanks
Adrian Rosebrock
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
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!
William Terceiro
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.
Regards,
William Terceiro
Adrian Rosebrock
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.
Jane
Hi,
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?
Adrian Rosebrock
I don’t use Spyder, but a quick Google search shows that can use Spyder with command line arguments — you just need to configure the project settings.
Nithin
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
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”.
Chris
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
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.Gaya Jay
Hi,
I have run above code in PyChram IDE with python 3.5.o. Following error is occurred
usage: detect.py [-h] -i IMAGES
detect.py: error: the following arguments are required: -i/–images
Can you please tell me, how should I correct it?
Adrian Rosebrock
You can either:
1. Delete the argument parsing code and define an
args
dictionary2. 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
Sir i have tried the second way but it says invalid syntax while passing the arguments
Athul
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
You can use other tutorials here on the blog to build this project. Take a look at this tutorial to get you started.
Jason
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
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.
Abhishek
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
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.
ts
Hi Adrian
Please provide the code for videos instead for images in HOG using svm
Adrian Rosebrock
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.
cuixiaoyang
Hi Adrian
I have run above code. Following error is occurred
File “pedestrain_detection01.py”, 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
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.
Ishaan
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
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
sir,
i found this error “no module named imutils.object_detection
Adrian Rosebrock
Make sure you install “imutils” on your system:
$ pip install --upgrade imutils
ravi
Hi,
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.
Regards
Ravi.
Adrian Rosebrock
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?
dani
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.
Dani
Adrian Rosebrock
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?
Shreyans Sharma
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
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.
nooler
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
Make sure you have updated your paths correctly. Additionally, I would recommend reading this guide on Python command line arguments.
CaĆque Destro
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
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.
Muhammad Abdullah
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.
Adrian Rosebrock
I would suggest starting by reading this post on the VideoStream class and then combining the two scripts.
jatinpal singh
hi adrian tried ur code but I am getting the error as I am unable to resolve even after searching on google.
detect2.py: error: the following arguments are required: -i/–image
I have given correct input
Adrian Rosebrock
Reading this post on command line arguments will solve your error.
David Chabalala
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
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?
rukku
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
In a video file? Or a video stream?
rukku
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
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
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
See my previous reply to your comment.
rukku
In a video file.Can you explain it to me how?
Adrian Rosebrock
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
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
See this article, including my reply to the very first comment on the post.
Anders Weile
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
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
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.
Aissatou
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
Yes, you can still use this code on videos, you would just apply the pedestrian detector to every frame of the video.
Aissatou
Thank you
Ken Adams
Thanks a ton!
Abdessamad gori
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
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.
Sahil Makandar
Hello Adrian sir ,
Can you please share any pipeline to detect and track pedestrians in low light or at night time.
rabah
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
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.
Yadnyesh
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
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.
Kasra Mokhtari
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!
Sam T
how to make it work for real time systems?
Adrian Rosebrock
Take a look at this tutorial on accessing your webcam. From there you would apply the Haar cascade to every frame of the input video stream.
Sam
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?
Adrian Rosebrock
Try this more accurate object detector which can be used for person detection.
Jacob
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: https://github.com/AastaNV/JEP/blob/master/script/install_opencv4.0.0_Nano.sh
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
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.
Veco
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
The name of the function is “detectMultiScale”, not “dedetectMultiScale”.
Jacob
Hello,
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
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.
Fran Sola
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!
Adrian Rosebrock
You should follow this tutorial on face detection.
Tales
Is there any way to get confidence in the form of probability?
Adrian Rosebrock
I would recommend using a deep learning-based object detector.
Emerson Souza
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?
David O
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
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.
Adrian Rosebrock
I’m happy to help, Weng! š