利用HOG+SVM实现行人检测

很久以前做的行人检测,现在稍加温习,上传记录一下。

首先解析视频,提取视频的每一帧形成图片存到磁盘。代码如下

import os

import cv2

videos_src_path = 'D:\\test1'
videos_save_path = 'D:\\test2' videos = os.listdir(videos_src_path)
videos = filter(lambda x: x.endswith('avi'), videos) for each_video in videos:
print (each_video) # get the name of each video, and make the directory to save frames
each_video_name, _ = each_video.split('.')
os.mkdir(videos_save_path + '/' + each_video_name) each_video_save_full_path = os.path.join(videos_save_path, each_video_name) + '/' # get the full path of each video, which will open the video tp extract frames
each_video_full_path = os.path.join(videos_src_path, each_video) cap = cv2.VideoCapture(each_video_full_path)
frame_count = 1
success = True
while(success):
success, frame = cap.read()
print ('Read a new frame: ', success) params = []
params.append(1)
params.append(1)
cv2.imwrite(each_video_save_full_path + each_video_name + "_%05d.ppm" % frame_count, frame, params) frame_count = frame_count + 1 cap.release()

对于图片的行人检测应用了梯度方向直方图和支持向量机。代码如下

这段代码可以实现对行人的标记。

# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
import os # initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) list = []
path = 'D:\\test2\\111' videos = os.listdir(path)
videos = filter(lambda x: x.endswith('ppm'), videos)
for each in videos:
new_path=path + "\\" + each
list.append(new_path) # loop over the image paths
for imagePath in list:
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
image = cv2.imread(imagePath)
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy() # detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05) # draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
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) # 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(1)

在这里应用了非极大值抑制方法(NMS),处理了重叠标记的问题。但是这里存在一个问题就是,部分两个人物距离过近或者产生重叠的情况下,优化后会将两个人标记称为一个人,这个问题还没有解决。

最后,将多张标记后的图片按一定帧数还原成视频,就完成了对视频的行人检测。 完整代码如下

# import the necessary packages
from __future__ import print_function
from imutils.object_detection import non_max_suppression
from imutils import paths
import numpy as np
import argparse
import imutils
import cv2
import os
'''
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True, help="path to images directory")
args = vars(ap.parse_args())
'''
# initialize the HOG descriptor/person detector
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector()) list = []
path = 'D:\\test2\\111' videos = os.listdir(path)
videos = filter(lambda x: x.endswith('ppm'), videos)
for each in videos:
new_path=path + "\\" + each
list.append(new_path) fourcc = cv2.VideoWriter_fourcc(*'I420')
videoWriter = cv2.VideoWriter('D:\\test2\\111\\saveVideo.avi',-1,24,(720,404)) # loop over the image paths
for imagePath in list:
# load the image and resize it to (1) reduce detection time
# and (2) improve detection accuracy
image = cv2.imread(imagePath)
if image is None:
break
image = imutils.resize(image, width=min(400, image.shape[1]))
orig = image.copy() # detect people in the image
(rects, weights) = hog.detectMultiScale(image, winStride=(4, 4),
padding=(8, 8), scale=1.05) # draw the original bounding boxes
for (x, y, w, h) in rects:
cv2.rectangle(orig, (x, y), (x + w, y + h), (0, 0, 255), 2) # apply non-maxima suppression to the bounding boxes using a
# fairly large overlap threshold to try to maintain overlapping
# boxes that are still people
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) # 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)
videoWriter.write(image)
cv2.waitKey(1)
videoWriter.release()

运行截图如下



优化:预处理部分图像结果存在的磁盘上,导致运行速度偏难,可以先加载到内存中,以便加速。

关于视频,没有进行上下文处理,只是单纯的将图片合成视频,没有相互关联起来。

利用HOG+SVM实现行人检测的更多相关文章

  1. Hog SVM 车辆 行人检测

    HOG SVM 车辆检测 近期需要对卡口车辆的车脸进行检测,首先选用一个常规的检测方法即是hog特征与SVM,Hog特征是由dalal在2005年提出的用于道路中行人检测的方法,并且取的了不错的识别效 ...

  2. OpenCV中基于HOG特征的行人检测

    目前基于机器学习方法的行人检测的主流特征描述子之一是HOG(Histogram of Oriented Gradient, 方向梯度直方图).HOG特征是用于目标检测的特征描述子,它通过计算和统计图像 ...

  3. opencv+树莓PI的基于HOG特征的行人检测

    树莓PI远程控制摄像头请参考前文:http://www.cnblogs.com/yuliyang/p/3561209.html 参考:http://answers.opencv.org/questio ...

  4. HoG SVM 目标检测分析

    前一段时间开始了解HoG跟SVM行人识别,看了很多包括Dalal得前辈的文章及经验分享,对HoG理论有了些初步的认识. HoG 的全称是 Histogram of Oriented Gradient, ...

  5. paper 86:行人检测资源(上)综述文献【转载,以后使用】

    行人检测具有极其广泛的应用:智能辅助驾驶,智能监控,行人分析以及智能机器人等领域.从2005年以来行人检测进入了一个快速的发展阶段,但是也存在很多问题还有待解决,主要还是在性能和速度方面还不能达到一个 ...

  6. PCL行人检测

    首先我们知道Hog特征结合SVM分类器已经被广泛应用于图像识别中,尤其在行人检测中获得了极大的成功,HOG+SVM进行行人检测的方法是法国研究人员Dalal在2005的CVPR上提出的,而如今虽然有很 ...

  7. paper 87:行人检测资源(下)代码数据【转载,以后使用】

    这是行人检测相关资源的第二部分:源码和数据集.考虑到实际应用的实时性要求,源码主要是C/C++的.源码和数据集的网址,经过测试都可访问,并注明了这些网址最后更新的日期,供学习和研究进行参考.(欢迎补充 ...

  8. 行人检测(Pedestrian Detection)资源

    一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...

  9. 目标检测之行人检测(Pedestrian Detection)---行人检测之简介0

    一.论文 综述类的文章 [1]P.Dollar, C. Wojek,B. Schiele, et al. Pedestrian detection: an evaluation of the stat ...

随机推荐

  1. 转:Sql Server中的表访问方式Table Scan, Index Scan, Index Seek

    0.参考文献 Table Scan, Index Scan, Index Seek SQL SERVER – Index Seek vs. Index Scan – Diffefence and Us ...

  2. Fabric密码保存

    参考:https://segmentfault.com/a/1190000000497630 多个IP分别使用不同的账号.密码 from fabric.api import * env.hosts = ...

  3. MySQL 的 CURD 操作

    0. 说明 CURD 操作通常是使用关系型数据库系统中的结构化查询语言(Structured Query Language,SQL)完成的 CURD 定义了用于处理数据的基本原子操作 CURD 代表创 ...

  4. 【转】Java学习---10个测试框架介绍

    [原文]https://www.toutiao.com/i6594302925458113027/ JAVA 程序员需要用到 10 个测试框架和库 Java 程序员需要用到十大单元测试和自动化集成测试 ...

  5. 【转】Java学习---深入理解线程池

    [原文]https://www.toutiao.com/i6566022142666736131/ 我们使用线程的时候就去创建一个线程,这样实现起来非常简便,但是就会有一个问题: 如果并发的线程数量很 ...

  6. windows Server 2008R2 FTP服务器搭建详细图解

    一.安装ftp服务 1.打开服务器管理器,如图: 2.右键点击角色,如图: 3.点击添加角色,会出现添加角色向导对话框,如图: 4.点击下一步,选择要添加的“web服务器(IIS)” ‘’ 5.点击下 ...

  7. unbuntu 安装python包提示E: Unable to locate package python-timeout

    今天本想着在unbuntu环境下安装python的一个包,安装了几次都提示 E: Unable to locate package python-timeout 查阅了一些信息才知道,原来是一些软件源 ...

  8. EXI6.0的安装(找不到网卡、找不到磁盘)

    给一台华为2488 V5 服务器安装EXI6.0服务 (问过华为售后不支持EXI5.5的安装,建议EXI6.0及以上版本) 根据界面提示信息按“Del”.进入BIOS设置界面 参考博客地址:https ...

  9. 关于ORA-00257: archiver error. Connect internal only, until freed 错误处理方法

    产生原因:出现ORA-00257错误(空间不足错误),通过查找资料,绝大部分说这是由于归档日志太多,占用了全部的硬盘剩余空间导致的,通过简单删除日志或加大存储空间就能够解决 解决办法:(亲测有效) 1 ...

  10. Android Studio中新建和引用assets文件

    从eclipse转过的朋友们应该不太习惯AS中新建assets文件和对文件内容的引用.我也查找了网上很多资料发现很少有这样的解决答案,于是便把自己解决的方法总结在这里. 1.一般新建project后这 ...