Python+OpenCV内置方法实现行人检测

您是否知道 OpenCV 具有执行行人检测的内置方法?

OpenCV 附带一个预训练的 HOG + 线性 SVM 模型,可用于在图像和视频流中执行行人检测。

今天我们使用Opencv自带的模型实现对视频流中的行人检测,只需打开一个新文件,将其命名为 detect.py ,然后加入代码:

# import the necessary packages
from __future__ import print_function
import numpy as np
import argparse
import cv2
import os

导入需要的包,然后定义项目需要的方法。

def nms(boxes, probs=None, overlapThresh=0.3):
    # if there are no boxes, return an empty list
    if len(boxes) == 0:
        return []

    # if the bounding boxes are integers, convert them to floats -- this
    # is important since we'll be doing a bunch of divisions
    if boxes.dtype.kind == "i":
        boxes = boxes.astype("float")

    # initialize the list of picked indexes
    pick = []

    # grab the coordinates of the bounding boxes
    x1 = boxes[:, 0]
    y1 = boxes[:, 1]
    x2 = boxes[:, 2]
    y2 = boxes[:, 3]

    # compute the area of the bounding boxes and grab the indexes to sort
    # (in the case that no probabilities are provided, simply sort on the
    # bottom-left y-coordinate)
    area = (x2 - x1 + 1) * (y2 - y1 + 1)
    idxs = y2

    # if probabilities are provided, sort on them instead
    if probs is not None:
        idxs = probs

    # sort the indexes
    idxs = np.argsort(idxs)

    # keep looping while some indexes still remain in the indexes list
    while len(idxs) > 0:
        # grab the last index in the indexes list and add the index value
        # to the list of picked indexes
        last = len(idxs) - 1
        i = idxs[last]
        pick.append(i)

        # find the largest (x, y) coordinates for the start of the bounding
        # box and the smallest (x, y) coordinates for the end of the bounding
        # box
        xx1 = np.maximum(x1[i], x1[idxs[:last]])
        yy1 = np.maximum(y1[i], y1[idxs[:last]])
        xx2 = np.minimum(x2[i], x2[idxs[:last]])
        yy2 = np.minimum(y2[i], y2[idxs[:last]])

        # compute the width and height of the bounding box
        w = np.maximum(0, xx2 - xx1 + 1)
        h = np.maximum(0, yy2 - yy1 + 1)

        # compute the ratio of overlap
        overlap = (w * h) / area[idxs[:last]]

        # delete all indexes from the index list that have overlap greater
        # than the provided overlap threshold
        idxs = np.delete(idxs, np.concatenate(([last],
                                               np.where(overlap > overlapThresh)[0])))

    # return only the bounding boxes that were picked
    return boxes[pick].astype("int")
image_types = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")
def list_images(basePath, contains=None):
    # return the set of files that are valid
    return list_files(basePath, validExts=image_types, contains=contains)

def list_files(basePath, validExts=None, contains=None):
    # loop over the directory structure
    for (rootDir, dirNames, filenames) in os.walk(basePath):
        # loop over the filenames in the current directory
        for filename in filenames:
            # if the contains string is not none and the filename does not contain
            # the supplied string, then ignore the file
            if contains is not None and filename.find(contains) == -1:
                continue
            # determine the file extension of the current file
            ext = filename[filename.rfind("."):].lower()
            # check to see if the file is an image and should be processed
            if validExts is None or ext.endswith(validExts):
                # construct the path to the image and yield it
                imagePath = os.path.join(rootDir, filename)
                yield imagePath
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):
    dim = None
    (h, w) = image.shape[:2]
    # 如果高和宽为None则直接返回
    if width is None and height is None:
        return image
    # 检查宽是否是None
    if width is None:
        # 计算高度的比例并并按照比例计算宽度
        r = height / float(h)
        dim = (int(w * r), height)
    # 高为None
    else:
        # 计算宽度比例,并计算高度
        r = width / float(w)
        dim = (width, int(h * r))
    resized = cv2.resize(image, dim, interpolation=inter)
    # return the resized image
    return resized

nms函数:非极大值抑制。

list_images:读取图片。

resize:等比例改变大小。

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", default='test1', help="path to images directory")
args = vars(ap.parse_args())
# 初始化 HOG 描述符/人物检测器
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())

定义输入图片的文件夹路径。

初始化HOG检测器。

# loop over the image paths
for imagePath in list_images(args["images"]):
    # 加载图像并调整其大小以
    # (1)减少检测时间
    # (2)提高检测精度
    image = cv2.imread(imagePath)
    image = resize(image, width=min(400, image.shape[1]))
    orig = image.copy()
    print(image)
    # detect people in the image
    (rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
                                            padding=(8, 8), scale=1.05)
    # draw the original bounding boxes
    print(rects)
    for (x, y, w, h) in rects:
        cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2)
    # 使用相当大的重叠阈值对边界框应用非极大值抑制,以尝试保持仍然是人的重叠框
    rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects])
    pick = nms(rects, probs=None, overlapThresh=0.65)
    # draw the final bounding boxes
    for (xA, yA, xB, yB) in pick:
        cv2.rectangle(image, (xA, yA), (xB, yB), (0, 255, 0), 2)
    # show some information on the number of bounding boxes
    filename = imagePath[imagePath.rfind("/") + 1:]
    print("[INFO] {}: {} original boxes, {} after suppression".format(
        filename, len(rects), len(pick)))
    # show the output images
    cv2.imshow("Before NMS", orig)
    cv2.imshow("After NMS", image)
    cv2.waitKey(0)

遍历 --images 目录中的图像。

然后,将图像调整为最大宽度为 400 像素。尝试减少图像尺寸的原因有两个:

  • 减小图像大小可确保需要评估图像金字塔中的滑动窗口更少(即从线性 SVM 中提取 HOG 特征,然后将其传递给线性 SVM),从而减少检测时间(并提高整体检测吞吐量)。
  • 调整我们的图像大小也提高了我们行人检测的整体准确性(即更少的误报)。

通过调用 hog 描述符的 detectMultiScale 方法,检测图像中的行人。 detectMultiScale 方法构造了一个比例为1.05 的图像金字塔,滑动窗口步长分别为x 和y 方向的(4, 4) 个像素。

滑动窗口的大小固定为 64 x 128 像素,正如开创性的 Dalal 和 Triggs 论文《用于人体检测的定向梯度直方图》所建议的那样。 detectMultiScale 函数返回 rects 的 2 元组,或图像中每个人的边界框 (x, y) 坐标和 weights ,SVM 为每次检测返回的置信度值。

较大的尺度大小将评估图像金字塔中的较少层,这可以使算法运行得更快。然而,规模太大(即图像金字塔中的层数较少)会导致行人无法被检测到。同样,过小的比例尺会显着增加需要评估的图像金字塔层的数量。这不仅会造成计算上的浪费,还会显着增加行人检测器检测到的误报数量。也就是说,在执行行人检测时,比例是要调整的最重要的参数之一。我将在以后的博客文章中对每个参数进行更彻底的审查以检测到多尺度。

获取初始边界框并将它们绘制在图像上。

但是,对于某些图像,您会注意到每个人检测到多个重叠的边界框。

在这种情况下,我们有两个选择。我们可以检测一个边界框是否完全包含在另一个边界框内。或者我们可以应用非最大值抑制并抑制与重要阈值重叠的边界框。

应用非极大值抑制后,得到最终的边界框,然后输出图像。

运行结果:

nms前:

nms后:

结论:

相比现在的深度学习方法,机器学习的精度低了很多。 

到此这篇关于Python+OpenCV内置方法实现行人检测的文章就介绍到这了,更多相关Python OpenCV行人检测内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!

(0)

相关推荐

  • Python中人脸图像特征提取方法(HOG、Dlib、CNN)简述

    目录 人脸图像特征提取方法 (一)HOG特征提取 (二)Dlib库 (三)卷积神经网络特征提取(CNN) 人脸图像特征提取方法 (一)HOG特征提取 1.HOG简介 Histogram of Oriented Gridients,缩写为HOG,是目前计算机视觉.模式识别领域很常用的一种描述图像局部纹理的特征.它的主要思想是在一副图像中,局部目标的表象和形状能够被梯度或边缘的方向密度分布很好地描述.其本质为:梯度的统计信息,而梯度主要存在于边缘的地方. 2.实现方法 首先将图像分成小的连通区域,这

  • python+opencv3.4.0 实现HOG+SVM行人检测的示例代码

    参照opencv官网例程写了一个基于python的行人检测程序,实现了和自带检测器基本一致的检测效果. 网址 :https://docs.opencv.org/3.4.0/d5/d77/train_HOG_8cpp-example.html opencv版本:3.4.0 训练集和opencv官方用了同一个,可以从http://pascal.inrialpes.fr/data/human/下载,在网页的最下方"here(970MB处)",用迅雷下载比较快(500kB/s).训练集文件比较

  • 人脸识别实战之Opencv+SVM实现人脸识别

    目录 前言 项目结构 编码 训练人脸识别模型 识别图像中的人脸 摄像头识别人脸 前言 在本文中,您将学习如何使用 OpenCV 进行人脸识别.文章分三部分介绍: 第一,将首先执行人脸检测,使用深度学习从每个人脸中提取人脸量化为128位的向量. 第二, 在嵌入基础上使用支持向量机(SVM)训练人脸识别模型. 第三,最后使用 OpenCV 识别图像和视频流中的人脸. 项目结构 编码 新建face_embeddings.py脚本,写入如下代码: # import the necessary packa

  • Python+OpenCV内置方法实现行人检测

    您是否知道 OpenCV 具有执行行人检测的内置方法? OpenCV 附带一个预训练的 HOG + 线性 SVM 模型,可用于在图像和视频流中执行行人检测. 今天我们使用Opencv自带的模型实现对视频流中的行人检测,只需打开一个新文件,将其命名为 detect.py ,然后加入代码: # import the necessary packages from __future__ import print_function import numpy as np import argparse i

  • Python内置方法实现字符串的秘钥加解密(推荐)

    在实际编程开发中,我们会使用到各类的加密算法来对数据和信息进行加密.比如密码中比较常见的MD5加密,以及AES加密等等. 对于密码认证来说,MD5加密是比较适合的,因为其不需要接触到明文的数据,只需要比对MD5加密后的哈希值就能判断数据是否一致:而对于一些在加密后进行解密的数据而言,AES则更加的常用. 在Python中实现AES算法需要借助的第三方库Crypto,其在各个操作系统上的安装方法有些许复杂,所以对于简单的使用有点杀鸡用牛刀的意思.在Mrdoc的开发过程中,我们就遇到了这样的问题.一

  • Python基本数据类型及内置方法

    目录 一 引子 二 数字类型int与float 2.1 定义 2.2 类型转换 2.3 使用 三 字符串 3.1 定义 3.2 类型转换 3.3 使用 3.3.1 优先掌握的操作 3.3.2 需要掌握的操作 3.3.3 了解操作 四 列表 4.1 定义 4.2 类型转换 4.3 使用 4.3.1 优先掌握的操作 4.3.2 了解操作 五 元组 5.1 作用 5.2 定义方式 5.3 类型转换 5.4 使用 六 字典 6.1 定义方式 6.2 类型转换 6.3 使用 6.3.1 优先掌握的操作 6

  • Python中class内置方法__init__与__new__作用与区别解析

    目录 背景 __init__方法作用 __new__方法作用 __init__ && __new__联系 使用__new__的场景 定义.继承immutable class 使用metaclass 参考文献 背景 最近尝试了解Django中ORM实现的原理,发现其用到了metaclass(元类)这一技术,进一步又涉及到Python class中有两个特殊内置方法__init__与__new__,决定先尝试探究一番两者的具体作用与区别.PS: 本文中涉及的类均为Python3中默认的新式类,

  • Python3.5常见内置方法参数用法实例详解

    本文实例讲述了Python3.5常见内置方法参数用法.分享给大家供大家参考,具体如下: Python的内置方法参数详解网站为:https://docs.python.org/3/library/functions.html?highlight=built#ascii 1.abs(x):返回一个数字的绝对值.参数可以是整数或浮点数.如果参数是一个复数,则返回它的大小. #内置函数abs() print(abs(-2)) print(abs(4.5)) print(abs(0.1+7j)) 运行结果

  • Python反射和内置方法重写操作详解

    本文实例讲述了Python反射和内置方法重写操作.分享给大家供大家参考,具体如下: isinstance和issubclass isinstance(obj,cls)检查是否obj是否是类 cls 的对象,类似 type() class Foo(object): pass obj = Foo() isinstance(obj, Foo) issubclass(sub, super)检查sub类是否是 super 类的派生类 class Foo(object): pass class Bar(Fo

  • Python序列对象与String类型内置方法详解

    本文实例讲述了Python序列对象与String类型内置方法.分享给大家供大家参考,具体如下: 前言 在Python数据结构篇中介绍了Python的序列类型数据结构,这次继续深入的学习序列和String类型对象的内建方法. 软件环境 系统 UbuntuKylin 14.04 软件 Python 2.7.3 IPython 4.0.0 序列类型 序列类型,即由整数进行索引的对象的有序集合.其中又可以分为下面两类: 可变序列:列表,支持元素的插入.删除.替换 不可变序列:元组.字符串 序列的操作方法

  • Python使用内置json模块解析json格式数据的方法

    本文实例讲述了Python使用内置json模块解析json格式数据的方法.分享给大家供大家参考,具体如下: Python中解析json字符串非常简单,直接用内置的json模块就可以,不需要安装额外的模块. 一.json字符串转为python值 json字符串: 复制代码 代码如下: {"userAccount":"54321","date":"2016-12-06 10:26:17","ClickTime"

  • Python 的内置字符串方法小结

    字符串处理是非常常用的技能,但 Python 内置字符串方法太多,常常遗忘,为了便于快速参考,特地依据 Python 3.5.1 给每个内置方法写了示例并进行了归类,便于大家索引. PS: 可以点击概览内的绿色标题进入相应分类或者通过右侧边栏文章目录快速索引相应方法. 大小写转换 str.capitalize() 将首字母转换成大写,需要注意的是如果首字没有大写形式,则返回原字符串. 'adi dog'.capitalize() # 'Adi dog' 'abcd 徐'.capitalize()

  • python字符串string的内置方法实例详解

    下面给大家分享python 字符串string的内置方法,具体内容详情如下所示: #__author: "Pizer Wang" #__date: 2018/1/28 a = "Let's go" print(a) print("-------------------") a = 'Let\'s go' print(a) print("-------------------") print("hello"

随机推荐