视频行人检测系统

视频行人检测系统

目的

设计一个视频行人检测系统,能够分割出多个行人。

代码

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

#读取视频文件
cap= cv2.VideoCapture("lzq3.mp4")
#cap= cv2.VideoCapture(0)
while (True):
    #读取视频帧图像
    ret, frame = cap.read()

    # 进行行人检测
    hog = cv2.HOGDescriptor()
    hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
    found, w = hog.detectMultiScale(frame)
    found_filtered = []
    for ri, r in enumerate(found):
        for qi, q in enumerate(found):
            if ri != qi and is_inside(r, q):
                break
            else:
                found_filtered.append(r)

    # 用蓝线框出人像区域
    for person in found_filtered:
        x, y, w, h = person
        cv2.rectangle(frame, (x, y), (x + w, y + h), (255, 0, 0), 2)

    # 显示检测到的行人图像
    cv2.imshow("people detection", frame)

    #等待10毫秒,如果按下ESC键则停止检测
    if cv2.waitKey(10) == 27:
        camera.release()
        cv2.destroyAllWindows()
        break

结果

视频行人检测系统_第1张图片

你可能感兴趣的:(opencv,计算机视觉,计算机视觉课程)