标签:运行 port master 计算机视觉 imshow show bre read esc
调用内部函数进行人体检测,实际效果并不好。民工三连:
hog = cv2.HOGDescriptor() # 启动检测器对象 hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # 指定检测器类型 found, w = hog.detectMultiScale(img) # 加载&&监测图像
完整程序:
import cv2
import numpy as np
def is_inside(o, i):
ox, oy, ow, oh = o
ix, iy, iw, ih = i
return (ox > ix) and (oy > iy) and ((ox+ow) < (ix+iw)) and ((oy+oh) < (iy+ih))
def draw_persion(image, person):
x, y, w, h = person
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 255), 2)
img = cv2.imread(‘./pycv-master/images/people.jpg‘)
hog = cv2.HOGDescriptor() # 启动检测器对象
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) # 指定检测器类型
found, w = hog.detectMultiScale(img) # 加载&&监测图像
found_filtered = []
for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and is_inside(r, q): # 如果r的外面不存在框的话,记录之
print(‘r‘,r)
print(‘q‘,q)
break
else: # 这个放在for外面的else异常的飘逸(骚气)啊
found_filtered.append(r)
for person in found_filtered:
draw_persion(img, person)
cv2.imshow(‘people delection‘, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
其他的地方没什么好讲的,只是有一处写法,实在是太骚气了,厉害:
for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and is_inside(r, q):
print(‘r‘,r)
print(‘q‘,q)
break
else:
found_filtered.append(r)
使用一个else承接所有的for中的if,这个写法很新奇,搭配break,效果是for中所有if均为假的时候会运行else中的语句。
实际效果部分截图(超烂的检测结果):

『python』计算机视觉_OpenCV3目标检测器(待续)
标签:运行 port master 计算机视觉 imshow show bre read esc
原文地址:http://www.cnblogs.com/hellcat/p/7062139.html