[求解]用yolov5+deepsort对于汽车车速监测,现在视频窗口未响应退出

img


用yolov5+deepsort对于汽车车速监测,现视频窗口未响应退出,求解

以下答案由GPT-3.5大模型与博主波罗歌共同编写:
要对汽车车速进行监测,可以采用以下步骤使用yolov5+deepsort建立一个车辆跟踪系统:

  1. 安装yolov5:可以通过pip install yolov5安装

  2. 安装deepsort:

git clone https://github.com/theAIGuysCode/yolov5-deepsort.git
cd yolov5-deepsort/
pip install -r requirements.txt
  1. 下载yolov5权重:在https://github.com/ultralytics/yolov5/releases%E4%B8%AD%E4%B8%8B%E8%BD%BD%E7%9B%B8%E5%BA%94%E7%89%88%E6%9C%AC

  2. 编写Python代码进行车辆检测和跟踪, 如下所示:

from utils import detection, deep_sort
import cv2

yolo_weights = 'path/to/yolov5/weights' # yolov5权重文件路径
car_tracker = detection.CarTracker(yolo_weights, conf_thres=0.4, iou_thres=0.5)
deepsort_weights = 'path/to/deepsort/weights' # deepsort权重文件路径
car_tracker.deepsort = deep_sort.DeepSort(deepsort_weights)

cap = cv2.VideoCapture('path/to/video') # 视频文件路径
while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break

    # 检测并跟踪车辆
    detections = car_tracker.detect(frame)
    tracks = car_tracker.track(detections)

    # 绘制检测框和跟踪ID
    for track in tracks:
        bbox = track.to_tlbr()
        cv2.rectangle(frame, (int(bbox[0]), int(bbox[1])), (int(bbox[2]), int(bbox[3])), (255, 0, 0), 2)
        cv2.putText(frame, str(track.track_id),(int(bbox[0]), int(bbox[1]-10)),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 0), 2)

    # 在窗口中显示视频
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

在代码中,我们首先创建一个CarTracker对象,使用yolov5进行车辆检测和跟踪。然后载入deepsort并运用到CarTracker对象中,以对车辆进行ID跟踪。我们从视频中逐帧读取,对每一帧图像进行车辆检测和deepsort跟踪。最后,我们可视化车辆跟踪结果并在窗口中显示输出。

至于视频窗口未响应退出的问题,可以考虑加入一个break条件。例如,在代码中,当用户按下"q"键时,我们break掉循环并释放视频流资源。

希望这个解答能够对你有所帮助。
如果我的回答解决了您的问题,请采纳!

img


上面这个死循环了,根本不会计算到下面这个while循环的,你上面这个是在做什么我没看懂?直接把上面的循环去掉看下,如果下面的没问题的话应该可以了

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 你可以看下这个问题的回答https://ask.csdn.net/questions/752979
  • 这篇博客你也可以参考下:语义分割 图像增强(数据扩充)
  • 除此之外, 这篇博客: 旋转角度判断回归中的 给两张图片,判断旋转角度: 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:

    https://github.com/jidai-code/ExplicitOrientationEstimator

    自己复现了一下,并尝试改进:

    import torch
    import torch.nn as nn
    import torch.nn.functional as F
    from torch.nn import init
    
    
    def conv2d(in_channels, out_channels, kernel_size, stride, padding):
    	return nn.Sequential(
    		nn.Conv2d(in_channels=in_channels, out_channels=out_channels, kernel_size=kernel_size, stride=stride,
    				  padding=padding, bias=False), nn.BatchNorm2d(out_channels), nn.ReLU(inplace=True), )
    
    
    def make_layers(cfg, in_channels):
    	layers = []
    	for l in cfg:
    		if l == 'M':
    			layers += [nn.MaxPool2d(kernel_size=2, stride=2)]
    		else:
    			layers += [conv2d(in_channels=in_channels, out_channels=l, kernel_size=3, stride=1, padding=1)]
    			in_channels = l
    	return nn.Sequential(*layers)
    
    
    
    class Angle_Net(nn.Module):
    	def __init__(self,num_classes):
    		super(Angle_Net, self).__init__()
    
    		self.num_classes=num_classes
    		self.setup()
    		self.initialize_weights()
    
    
    	def setup(self):
    		cfg = [64, 64, 'M', 64, 64, 'M', 128, 128, 'M', 256, 256, 'M']
    		self.ConvNet = make_layers(cfg=cfg, in_channels=3)
    		self.FC = nn.Sequential(
    			nn.Linear(in_features=1024, out_features=1024),
    			nn.ReLU(inplace=True),
    			nn.Linear(in_features=1024, out_features=1024),
    			nn.ReLU(inplace=True),
    			nn.Linear(in_features=1024, out_features=self.num_classes),
    			)
    	def initialize_weights(self):
    		for m in self.modules():
    			if isinstance(m, nn.Conv2d):
    				if m.bias is not None:
    					init.uniform(m.bias)
    				nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
    
    			elif isinstance(m, nn.BatchNorm2d):
    				nn.init.constant_(m.weight, 1)
    				nn.init.constant_(m.bias, 0)
    
    			elif isinstance(m, nn.Linear):
    				nn.init.normal_(m.weight, 0, 0.01)
    				nn.init.constant_(m.bias, 0)
    	def forward(self, x):
    
    		conv_out0 = self.ConvNet(x)
    
    		conv_out0 = conv_out0.view(-1, 1024)
    
    		pred_angle0 = self.FC(conv_out0)
    
    		return pred_angle0
    
    if __name__ == '__main__':
    
    
    	model=Angle_Net(num_classes=4)
    	data = torch.randn(1, 3, 64, 64)  # .cuda()
    
    	model.eval()
    	for i in range(10):
    		out =model(data)
    		print(out.size())
    

    最后3层的Liner比较重要

            self.fc= nn.Sequential(
                nn.Linear(in_features=1024, out_features=1024),
                nn.ReLU(),
                nn.Linear(in_features=1024, out_features=128),
                nn.ReLU(),
                nn.Linear(in_features=128, out_features=1),
            )

    自己用peleenet尝试了一下,记录一下优化记录:

    1. 把平均池化去掉,精度有所提升,收敛加快。

    2. linear加层以后,收敛速度会加快,精度也有所提升。

            self.linear= nn.Sequential(
                nn.Linear(in_features=nin_transition_layer*4, out_features=128),
                nn.ReLU(),
                nn.Linear(in_features=128, out_features=1),
            )

    加了一层ReLU之后,效果更好:

            self.linear= nn.Sequential(
                nn.ReLU(),
                nn.Linear(in_features=nin_transition_layer*4, out_features=128),
                nn.ReLU(),
                nn.Linear(in_features=128, out_features=1),
            )

    3. 可能因为对称性,ReLU激活函数效果最好:

    self.relu = nn.ReLU()
    # self.relu = nn.ReLU6(True)
    # self.relu = mish.Mish()

    改进之后,收敛速度比以前快了好几倍。

    本机项目 1027

  • 您还可以看一下 夏曹俊老师的网络小说抓取软件的设计与研发——来自企业开发实践经验课程中的 需求功能设计与流程图规划小节, 巩固相关知识点

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^