Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Saving Operated Video from a webcam using OpenCV
Next article icon

OpenCV is an open-source library, which is aimed at real-time computer vision. This library is developed by Intel and is cross-platform - it can support Python, C++, Java, etc. Computer Vision is a cutting edge field of Computer Science that aims to enable computers to understand what is being seen in an image. OpenCV is one of the most widely used libraries for Computer Vision tasks like face recognition, motion detection, object detection, etc. In this tutorial, we are going to build a basicPedestrian Detector for images and videos using OpenCV. Pedestrian detection is a very important area of research because it can enhance the functionality of a pedestrian protection system in Self Driving Cars. We can extract features like head, two arms, two legs, etc, from an image of a human body and pass them to train a machine learning model. After training, the model can be used to detect and track humans in images and video streams. However, OpenCV has a built-in method to detect pedestrians. It has a pre-trainedHOG(Histogram of Oriented Gradients) + Linear SVM model to detect pedestrians in images and video streams.

Histogram of Oriented Gradients

This algorithm checks directly surrounding pixels of every single pixel. The goal is to check how darker is the current pixel compared to the surrounding pixels. The algorithm draws and arrows showing the direction of the image getting darker. It repeats the process for each and every pixel in the image. At last, every pixel would be replaced by an arrow, these arrows are calledGradients. These gradients show the flow of light from light to dark. By using these gradients algorithms perform further analysis. To learn more about HOG, read Navneet Dalal and Bill Triggs research paper onHOG for Human Detection..

Requirements

opencv-python 3.4.2imutils 0.5.3

To install the above modules type the below command in the terminal.

pip install opencv-python 3.4.2pip install imutils 0.5.3

Example 1:

Lets make the program to detect pedestrians in an Image:

Image Used:python-opncv 

Python3
importcv2importimutils# Initializing the HOG person# detectorhog=cv2.HOGDescriptor()hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())# Reading the Imageimage=cv2.imread('img.png')# Resizing the Imageimage=imutils.resize(image,width=min(400,image.shape[1]))# Detecting all the regions in the# Image that has a pedestrians inside it(regions,_)=hog.detectMultiScale(image,winStride=(4,4),padding=(4,4),scale=1.05)# Drawing the regions in the Imagefor(x,y,w,h)inregions:cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)# Showing the output Imagecv2.imshow("Image",image)cv2.waitKey(0)cv2.destroyAllWindows()

Output:python-opnecv-1Example 2: Lets make the program to detect pedestrians in a video: 

Python3
importcv2importimutils# Initializing the HOG person# detectorhog=cv2.HOGDescriptor()hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())cap=cv2.VideoCapture('vid.mp4')whilecap.isOpened():# Reading the video streamret,image=cap.read()ifret:image=imutils.resize(image,width=min(400,image.shape[1]))# Detecting all the regions# in the Image that has a# pedestrians inside it(regions,_)=hog.detectMultiScale(image,winStride=(4,4),padding=(4,4),scale=1.05)# Drawing the regions in the# Imagefor(x,y,w,h)inregions:cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),2)# Showing the output Imagecv2.imshow("Image",image)ifcv2.waitKey(25)&0xFF==ord('q'):breakelse:breakcap.release()cv2.destroyAllWindows()

Output:


Improve
Practice Tags :

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp