物体检测实战:使用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:

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数Python工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Python开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Python开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新

如果你觉得这些内容对你有帮助,可以添加V获取:vip1024c (备注Python)
img

一、Python所有方向的学习路线

Python所有方向路线就是把Python常用的技术点做整理,形成各个领域的知识点汇总,它的用处就在于,你可以按照上面的知识点去找对应的学习资源,保证自己学得较为全面。

二、学习软件

工欲善其事必先利其器。学习Python常用的开发软件都在这里了,给大家节省了很多时间。

三、入门学习视频

我们在看视频学习的时候,不能光动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

动眼动脑不动手,比较科学的学习方法是在理解之后运用它们,这时候练手项目就很适合了。

  • 19
    点赞
  • 26
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值