# coding:utf-8
import cv2
from timeit import default_timer as timer
class Predict(object):
def __init__(self):
# 获取hog检测器对象
self.hog = cv2.HOGDescriptor()
# 设置检测人的默认检测器
self.hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
print("模型加载完成!")
# 检测i方框 包含o方框
def is_inside(self, o, i):
ox, oy, ow, oh = o
ix, iy, iw, ih = i
return ox > ix and ox + ow < ix + iw and oy + oh < iy + ih
# 将人外面的方框画出来
def draw_person(self, image, person):
x, y, w, h = person
cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 255), 2)
def detect_img(self, img):
# 在图片中检测人,
# 返回found列表 每个元素是一个(x, y, w, h)的矩形,w是每一个矩形的置信度
found, w = self.hog.detectMultiScale(img)
found_filtered = []
# 如果方框有包含,只留下内部的小方块
for ri, r in enumerate(found):
for qi, q in enumerate(found):
if ri != qi and self.is_inside(r, q):
break
else:
found_filtered.append(r)
# 将每一个方块画出来
for person in found_filtered:
self.draw_person(img, person)
return img
def detect_video(self, video_path):
vid = cv2.VideoCapture(video_path)
if not vid.isOpened():
raise IOError("Couldn't open webcam or video")
while True:
return_value, frame = vid.read()
t1 = timer()
result = self.detect_img(frame)
t2 = timer()
fps = 1 / (t2 - t1)
print(fps)
cv2.imshow("person detection", result)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
if __name__ == '__main__':
predict = Predict()
# 检测图片
if False:
img = cv2.imread("person1.jpg")
result = predict.detect_img(img)
cv2.imshow("person detection", result)
cv2.waitKey()
cv2.destroyAllWindows()
# 检测视频
if True:
video_path = 'video1.mp4'
predict.detect_video(video_path=video_path)
04-23
1268
12-25
05-12
1万+