繁体   English   中英

python opencv人员检测

[英]python opencv people detection

我正在尝试使用opencv和python构建人员检测功能,但是我需要一些帮助来理解一些事情。 图像来自粘贴在窗户上的手机。 这是代码:

# USAGE
# python detect.py --images images

from __future__ import print_function
from glob import glob
from imutils.object_detection import non_max_suppression
import argparse
import cv2
import numpy as np
import os

# parse arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())

# strip last / if present
arg_images = args["images"].rstrip("/")

# check folder exists
if not os.path.isdir(arg_images):
    print(arg_images + " is not a folder, terminate")
    quit()

# load images names
imagePaths = sorted(glob(arg_images + "/*.jpg"))
# check folder is not empty
if len(imagePaths) == 0:
    print(arg_images + " is empty, terminate")
    quit()

hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

for imagePath in imagePaths:
    # INTER_NEAREST - a nearest-neighbor interpolation
    # INTER_LINEAR - a bilinear interpolation (used by default)
    # INTER_AREA - resampling using pixel area relation. It may be a preferred method for image decimation, as it gives moire’-free results. But when the image is zoomed, it is similar to the INTER_NEAREST method.
    # INTER_CUBIC - a bicubic interpolation over 4x4 pixel neighborhood
    # INTER_LANCZOS4 - a Lanczos interpolation over 8x8 pixel neighborhood
    image = cv2.imread(imagePath)
    image = cv2.resize(image, (min(800, image.shape[1]), min(600, image.shape[0])), interpolation = cv2.INTER_LINEAR)
    #image = cv2.resize(image, (min(1200, image.shape[1]), min(900, image.shape[0])), interpolation = cv2.INTER_LINEAR)

    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4), padding=(0, 0), scale=1.01)

    foldername = imagePath[0:imagePath.rfind("/")]
    filename = imagePath[imagePath.rfind("/") + 1:]

    if len(rects) == 0:
        print("- " + filename)
    else:
        rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
        pick = non_max_suppression(rects, probs=None, overlapThresh=0.65)

        for (xA, yA, xB, yB) in pick:
            cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)

        print("+ " + filename)
        cv2.imwrite(foldername + "/detected/" + filename, image)

当我在这些图像( test.zip )上运行此功能时,根据图像大小,我会得到非常不同的结果:

  • 800x600:误报率最少,晚上大约40%,白天大约10%
  • 1200x900:更多的误报
  • 原始大小:看起来更像是随机猜测而不是检测结果

我可以假设这是因为detectMultiScale可以在较小的检测窗口中工作并且可以更改步幅,但是不能更改大小吗?

另外,如果您查看IMG_20180329_061603.jpg,则始终会检测到误报,但我不明白为什么。 夜景图片(对我而言)看起来都一样,但是有太多的绿框。

欢迎对此提供任何帮助。 如果您需要澄清,请询问..谢谢

您看到的原因是用于人员检测的HOG描述符具有64x128的窗口。 因此,您想优化scale参数和图像尺寸,以获得大部分真实的正片。 您可能主要是关心一个人靠近自行车,所以远(又小)的人并不重要。 我将收集一组“训练”图像以找到最佳参数值。 同样,您也忽略了detectMultiScale()返回的权重,同样可以直观地看到真阳性和假阳性的值,并找到最佳阈值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM