opencv作为开源的视觉库,其早已集成hog+svm算法。所以只需要调用api就可以简单的实现行人检测。
# -*- coding: utf-8 -*-
# 行人检测
import cv2
import numpy as np
def svmdetectperson(img):
hog=cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
person,w= hog.detectMultiScale(img)
return person
def is_inside(a,b):
x1,y1,w1,h1=a
x2,y2,w2,h2=b #judge b is not include a
return x1>x2 and y1>y2 and x1+w1and y1+h1def draw(img,a):
x,y,w,h=a
cv2.rectangle(img,(x,y),(x+w,y+h),(0,0,255),1)
if __name__=="__main__":
img=cv2.imread(r'C:\Users\Y\Desktop\person.jpg')
person=svmdetectperson(img)
filtered=[]
for i,p in enumerate(person):
for j,p1 in enumerate(person):
if i!=j and is_inside(p,p1):
break
filtered.append(p)
for p in filtered:
draw(img,p)
cv2.imshow("person",img)
cv2.waitKey(0)