600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > 人流量统计 特定目标搜索+跟踪!

人流量统计 特定目标搜索+跟踪!

时间:2023-10-15 19:52:36

相关推荐

人流量统计 特定目标搜索+跟踪!

yolov5

deepsort

fastreid

前言

基于YOLOv5和DeepSort的目标跟踪 介绍过针对行人的检测与跟踪。本文介绍另一个项目,结合 FastReid 来实现行人的检测、跟踪和重识别。项目地址: /zengwb-lx/Yolov5-Deepsort-Fastreid,作者给出的2个主要实例,也是非常的实用,包括行人流量的统计、人群中特定目标的查找与跟踪。

项目复现

首先,创建个全新的虚拟环境

condacreate-npytorch1.6python=3.7condaactivatepytorch1.6

接着去拉取源码

gitclone/zengwb-lx/Yolov5-Deepsort-Fastreid.gitcdYolov5-Deepsort-Fastreid

然后安装下其它的依赖包

#如果没有gpu的话,就按照requirements.txt安装即可pipinstalltorch==1.6.0+cu101torchvision==0.7.0+cu101-f/whl/torch_stable.html#编辑requirements.txt,注释掉torch和torchvisionpipinstall-rrequirements.txt#使用cython加速pipinstallcythoncdfast_reid/fastreid/evaluation/rank_cylibmakeallcd../../../../

先来跑个行人计数的demo

pythonperson_count.py

yolov5 deepsort fastreid

yolov5的作者分别在googleapigithub上都保存了模型文件,但是这2个yolov5s.pt是不一样的,大家可以通过md5sum去查看一下,github上的模型文件是对的

如果你在运行过程中出现下面的错误

-07-1314:22:20[INFO]:Loadingweightsfrom./deep_sort/deep/checkpoint/ckpt.t7...Done!Traceback(mostrecentcalllast):File"person_count.py",line244,in<module>yolo_reid.deep_sort()File"person_count.py",line121,indeep_sortbbox_xywh,cls_conf,cls_ids,xy=self.person_detect.detect(video_path,img,ori_img,vid_cap)File"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/person_detect_yolov5.py",line95,indetectpred=self.model(img,augment=self.augment)[0]File"/home/xugaoxiang/anaconda3/envs/pytorch1.6/lib/python3.7/site-packages/torch/nn/modules/module.py",line722,in_call_implresult=self.forward(*input,**kwargs)File"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/models/yolo.py",line111,inforwardreturnself.forward_once(x,profile)#single-scaleinference,trainFile"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/models/yolo.py",line131,inforward_oncex=m(x)#runFile"/home/xugaoxiang/anaconda3/envs/pytorch1.6/lib/python3.7/site-packages/torch/nn/modules/module.py",line722,in_call_implresult=self.forward(*input,**kwargs)File"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/models/yolo.py",line36,inforwardself.training|=self.exportFile"/home/xugaoxiang/anaconda3/envs/pytorch1.6/lib/python3.7/site-packages/torch/nn/modules/module.py",line772,in__getattr__type(self).__name__,name))torch.nn.modules.module.ModuleAttributeError:'Detect'objecthasnoattribute'export'

这个就是模型的问题,建议使用源码中自带的shell脚本进行下载

shweights/download_weights.sh

我们来看看这个行人流量统计的基本原理:

首先,作者将yolov5的目标检测封装成了一个类Person_detect,通过它的detect方法可以检测到视频中的每一个行人目标

然后,在画面中设定一条基准线,给定线条两端的坐标即可

line=[(0,int(0.48*ori_img.shape[0])),(int(ori_img.shape[1]),int(0.48*ori_img.shape[0]))]cv2.line(ori_img,line[0],line[1],(0,255,255),4)

接着,创建跟踪器,开始对yolov5检测出的每一个目标进行跟踪。这里以目标预测框的中心点为基准,下图是它的计算方法

yolov5 deepsort fastreid

如果前后帧的中心点所连成的直线和预先设定的基准线相交,则判定为越线,但是这里还有个方向的问题,向上还是向下?来看另一张图

yolov5 deepsort fastreid

作者利用了三角形的正切与反正切原理,使用math模块中的degrees方法来判断,如果这个角度>0,说明是向上走,反之则为向下走

defvector_angle(midpoint,previous_midpoint):x=midpoint[0]-previous_midpoint[0]y=midpoint[1]-previous_midpoint[1]returnmath.degrees(math.atan2(y,x))

看完行人计数的示例,我们再来看看特定目标的重识别示例

pythonperson_search_reid.py

报错了

Fusinglayers...Traceback(mostrecentcalllast):File"person_search_reid.py",line120,in<module>yolo_reid=yolo_reid(cfg,args,path=args.video_path)File"person_search_reid.py",line35,in__init__self.deepsort=build_tracker(cfg,args.sort,use_cuda=use_cuda)File"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/deep_sort/__init__.py",line18,inbuild_trackermax_age=cfg.DEEPSORT.MAX_AGE,n_init=cfg.DEEPSORT.N_INIT,nn_budget=cfg.DEEPSORT.NN_BUDGET,use_cuda=use_cuda)File"/home/xugaoxiang/Works/Yolov5-Deepsort-Fastreid/deep_sort/deep_reid.py",line29,in__init__self.extractor=Reid_feature()File"./fast_reid/demo/demo.py",line84,in__init__cfg=setup_cfg(args)File"./fast_reid/demo/demo.py",line35,insetup_cfgcfg.merge_from_file(args.config_file)File"./fast_reid/fastreid/config/config.py",line107,inmerge_from_filecfg_filename,allow_unsafe=allow_unsafeFile"./fast_reid/fastreid/config/config.py",line50,inload_yaml_with_basewithPathManager.open(filename,"r")asf:File"./fast_reid/fastreid/utils/file_io.py",line357,inopenpath,mode,buffering=buffering,**kwargsFile"./fast_reid/fastreid/utils/file_io.py",line251,in_openopener=opener,FileNotFoundError:[Errno2]Nosuchfileordirectory:'../../kd-r34-r101_ibn/config-test.yaml'

这是缺少配置文件,到下面的链接去下载

链接: /s/1bMG3qy7npecCh6AzNO-Zyw

提取码:hy1m

把这2个文件都存放在目录kd-r34-r101_ibn下,然后修改源码fast_reid/demo/demo.py中的第45行,将

default='../../kd-r34-r101_ibn/config-test.yaml',

改成

default='kd-r34-r101_ibn/config-test.yaml',

将第68行的

default=['MODEL.WEIGHTS','../../kd-r34-r101_ibn/model_final.pth'],

改成

default=['MODEL.WEIGHTS','kd-r34-r101_ibn/model_final.pth'],

然后再次运行脚本person_search_reid.py,可以得到

yolov5 deepsort fastreid

可以看到,由于事先已经提了2位行人(a1111111111b222222222222)的特征,所以,画面中能够识别出这2个人并进行跟踪。默认,特征文件保存在fast_reid/query

特征提取

如果你也想要制作一个特征文件,可以按照下面的步骤进行

首先,需要截取目标人物的图片,存放在某个以特定目标命名的文件夹下,如我这里的,这样,后面进行识别的时候,就显示这个名字了。把这个文件夹拷贝到fast_reid/query目录下,目录结构如下

(pytorch1.6)xugaoxiang@1070Ti:~/Works/Yolov5-Deepsort-Fastreid/fast_reid/query$tree.├──names.npy├──query_features.npy└──├──10.png├──11.png├──12.png├──13.png├──14.png├──15.png├──1.png├──2.png├──3.png├──4.png├──5.png├──6.png├──7.png├──8.png└──9.png

接下来执行

cdfast_reid/demopythonperson_bank.py

执行完毕后,query目录下的query_features.npynames.npy就被更新了

最后,找个包含目标的视频测试下效果

yolov5 deepsort fastreid

yolov5 deepsort fastreid

工程下载

最后,将包含所需文件全部打包,需要的朋友自行下载

链接:/s/1JiFzo5_H7TKniZZPzjZK7A

提取码:nauu

参考资料

/zengwb-lx/Yolov5-Deepsort-Fastreid

/zengwubbb/article/details/113422048

//06/29/fastreid/

yunanwen

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。