使用 OpenCV+Python-2.7 进行完全 body 检测和跟踪
Full body detection and tracking using OpenCV+Python-2.7
有很多资料可用于使用 C++ 执行此操作。我想知道是否有办法在 Python-2.7?
中使用 OpenCV 进行完整的 body 检测
给定一个人沿着矢状面行走的视频(相机从行走方向拍摄 90 度),我想界定一个覆盖该人整个 body 的感兴趣区域矩形并跟踪逐帧移动也一样。
这是使用 hog 描述符,您可以在 samples/python/peopledetect.py 中找到示例,我使用了 opencv 安装提供的示例视频。
import numpy as np
import cv2
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.
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() )
cap=cv2.VideoCapture('vid.avi')
while True:
_,frame=cap.read()
found,w=hog.detectMultiScale(frame, winStride=(8,8), padding=(32,32), scale=1.05)
draw_detections(frame,found)
cv2.imshow('feed',frame)
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()
结果
不太好。还是试试吧
有很多资料可用于使用 C++ 执行此操作。我想知道是否有办法在 Python-2.7?
中使用 OpenCV 进行完整的 body 检测给定一个人沿着矢状面行走的视频(相机从行走方向拍摄 90 度),我想界定一个覆盖该人整个 body 的感兴趣区域矩形并跟踪逐帧移动也一样。
这是使用 hog 描述符,您可以在 samples/python/peopledetect.py 中找到示例,我使用了 opencv 安装提供的示例视频。
import numpy as np
import cv2
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.
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() )
cap=cv2.VideoCapture('vid.avi')
while True:
_,frame=cap.read()
found,w=hog.detectMultiScale(frame, winStride=(8,8), padding=(32,32), scale=1.05)
draw_detections(frame,found)
cv2.imshow('feed',frame)
ch = 0xFF & cv2.waitKey(1)
if ch == 27:
break
cv2.destroyAllWindows()
结果
不太好。还是试试吧