【发布时间】:2016-02-17 00:45:10
【问题描述】:
我是 OpenCV 的新手,我正在尝试编写一个检测视频中人物的程序。我有这段代码,它是 peopledetect 示例的变体。
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)
def find_people(img):
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
img = frame
if img is None:
return None
# print('Failed to load image file:', fn)
# continue
# except:
# print('loading error')
# continue
found, w = hog.detectMultiScale(
img, winStride=(10, 10), padding=(32, 32), scale=1.05)
found_filtered = []
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)))
return img
if __name__ == '__main__':
import argparse
# import itertools as it
ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video",
help="path to the (optional) video file")
ap.add_argument("-b", "--buffer", type=int, default=64,
help="max buffer size")
# ap.add_argument("-f", "--blur-faces", action='blur_faces',
# help="Blur the faces contained in the video")
args = vars(ap.parse_args())
print(help_message)
camera = cv2.VideoCapture(args["video"])
# keep looping
while True:
# grab the current frame
(grabbed, frame) = camera.read()
# if we are viewing a video and we did not grab a frame,
# then we have reached the end of the video
# if args.get("video") and not grabbed:
# break
img = find_people(frame)
cv2.imshow('img', img)
#Waitkey must be called for something to show up on the screen
#It gives the computer time to process the image.
cv2.waitKey(30)
cv2.destroyAllWindows()
此代码查找人员并在他们周围绘制一个矩形。我将如何确定检测到的人是否与在视频的前一帧中检测到的人相同?或者我怎样才能知道 HOG 检测到的人是否以前被检测到?
我知道我可以保存 HOG 找到的位置并进行比较以查看大致相同的位置,但我认为如果视频中的人离开画面然后返回,这种方法将不起作用,因为他们会被视为一个新人。是否可以将他们衣服的颜色与特定的人联系起来并使用它?
【问题讨论】:
标签: python opencv computer-vision opencv3.0