您的位置:首页 > 编程语言 > Python开发

python3.6.3+opencv3.3.0学习笔记十三--基于HOG的动态人体捕获

2017-10-29 14:44 886 查看
说明

和静态图片的处理方法基本一致,只是将静态图片更换为摄像头即时捕获的图像帧。

再次感觉,人体捕获受到的影响因素太多,虚警和错捕的概率很高,必须和其它的捕获和跟踪模式相结合,采用多模的方案才可行,并且不能作为主要的捕获和跟踪手段。

'''
python3.6.3+opencv3.3.0
video_capture_HOG_people_detection

'''
import cv2
import numpy as np

video="xxxxxxxxxxxxxxxxxxxxx"#需要更改为摄像头的地址
cap = cv2.VideoCapture(video)

def inside(r, q):#大方框嵌套小方框时将小方框移除
rx, ry, rw, rh = r#大方框矩形
qx, qy, qw, qh = q#小方框矩形
return rx > qx and ry > qy and rx + rw < qx + qw and ry + rh < qy + qh

def draw_detections(img, rects, thickness = 1):#绘矩形
for x, y, w, h in rects:
# the HOG detector returns slightly larger rectangles than the real objects.
# so we slightly shrink the rectangles to get a nicer output.
#原版HOG中绘出的矩形和实际比例不符,这里缩小一下
pad_w, pad_h = int(0.15*w), int(0.05*h)
cv2.rectangle(img, (x+pad_w, y+pad_h), (x+w-pad_w, y+h-pad_h), (0, 255, 0), thickness)

if __name__ == '__main__':

hog = cv2.HOGDescriptor()
hog.setSVMDetector( cv2.HOGDescriptor_getDefaultPeopleDetector() )

while(1):
ret, img =cap.read()
found, w = hog.detectMultiScale(img, winStride=(8,8), padding=(35,35), scale=1.1)
found_filtered = []
#上述参数运行根据实际的应用场景进行调节
'''
HOGDescriptor::detectMultiScale(const GpuMat& img, vector<Rect>& found_locations,
doublehit_threshold=0, Size win_stride=Size(), Size padding=Size(),
double scale0=1.05, int group_threshold=2)

            该函数表示对输入的图片img进行多尺度行人检测 img为输入待检测的图片;
found_locations为检测到目标区域列表;参数3为程序内部计算为行人目标的阈值,
也就是检测到的特征到SVM分类超平面的距离;参数4为滑动窗口每次移动的距离。
它必须是块移动的整数倍;参数5为图像扩充的大小;参数6为比例系数,
即滑动窗口每次增加的比例;参数7为组阈值,即校正系数,当一个目标被多个窗口检测出来时,
该参数此时就起了调节作用,为0时表示不起调节作用。

void detectMultiScale(constGpuMat& img, vector<Rect>& found_locations, double hit_threshold= 0,

Sizewin_stride = Size(), Size padding = Size(), double scale0=1.05,intgroup_threshold=2 );

img:待检测的图像,支持CV_8UC1或CV_8UC4类型

found_locations:检测到的目标的包围框数组

hit_threshold:检测到的特征到SVM分类超平面的距离,一般是设为0,在分类器参数中指明。

win_stride:检测窗口每次移动的距离,必须是块移动的整数倍

af64
padding:保持CPU接口兼容性的虚参数,网上下载的例子里是(32,32)

scale0:滑动窗口每次增加的比例

group_threshold:组阈值,即校正系数,当一个目标被多个窗口检测出来时,该参数此时就起了调节作用, 为0时表示不起调节作用。

'''

for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and inside(r, q):
break
else:
found_filtered.append(r)
draw_detections(img, found)
draw_detections(img, found_filtered, 3)
print('%d (%d) found' % (len(found_filtered), len(found)))
cv2.imshow('img', img)

if cv2.waitKey(1000//30)==27 or cv2.waitKey(1000//30)==ord(' '):
print('退出')
break

cv2.destroyAllWindows()
cap.release()




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: