yolov5调用摄像头检测报错

文章讲述了在使用Yolov5的detect.py脚本从YouTube获取视频流时遇到的TypeError问题。错误出现在尝试检查URL是否包含youtube.com/或youtu.be/时,由于URL变量的类型错误导致。解决方案包括将URL转换为str类型以及调整与fps相关的代码。修改后,脚本能成功调用摄像头并避免错误。
摘要由CSDN通过智能技术生成

if ‘youtube.com/’ in url or ‘youtu.be/’ in url: # if source is YouTube videoTypeError: argument of type ‘int’ is not iterable

当我在使用yolov5中的detect.py文件调用摄像头时报了这个错误,我们需要在datasets.py文件中找到下面这行代码:

if 'youtube.com/' in url or 'youtu.be/' in url:  # if source is YouTube video
    check_requirements(('pafy', 'youtube_dl'))
    import pafy
    url = pafy.new(url).getbest(preftype="mp4").url
cap = cv2.VideoCapture(url)

方法一

将url改成str类型

if 'youtube.com/' in str(url) or 'youtu.be/' in str(url):  # if source is YouTube video
	check_requirements(('pafy', 'youtube_dl'))
	import pafy
	url = pafy.new(url).getbest(preftype="mp4").url
cap = cv2.VideoCapture(url)

方法二

我个人使用方法一之后依旧报错,需要修改这两处代码
第一处:
在这里插入图片描述
第二处:进行如下修改

# time.sleep(1 / self.fps)  # wait time
if (self.fps != 0): time.sleep(1 / self.fps)

使用上面的两个方法就能顺利调用摄像头了!

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
使用yolov5调用摄像头进行实时目标检测,可以通过以下步骤实现: 1. 安装yolov5和pyqt5库。 2. 在代码中导入yolov5和pyqt5库。 3. 加载yolov5模型。 4. 打开摄像头并读取视频流。 5. 对每一帧图像进行目标检测。 6. 在图像上绘制检测结果并显示。 下面是一个简单的示例代码: ``` import cv2 import torch from PyQt5.QtGui import QImage, QPixmap from PyQt5.QtWidgets import QApplication, QLabel from yolov5.models.experimental import attempt_load from yolov5.utils.general import non_max_suppression, scale_coords from yolov5.utils.torch_utils import select_device # 加载yolov5模型 weights = 'yolov5s.pt' device = select_device('') model = attempt_load(weights, map_location=device) model.eval() # 打开摄像头并读取视频流 cap = cv2.VideoCapture(0) # 创建Qt应用程序和标签 app = QApplication([]) label = QLabel() label.show() while True: # 读取视频流中的一帧图像 ret, frame = cap.read() # 将图像转换为PyTorch Tensor img = torch.from_numpy(frame).permute(2, 0, 1).float().div(255.0).unsqueeze(0) # 将图像输入yolov5模型进行目标检测 with torch.no_grad(): detections = model(img)[0] detections = non_max_suppression(detections, conf_thres=0.5, iou_thres=0.5) # 在图像上绘制检测结果 if detections[0] is not None: detections = detections[0].cpu().numpy() detections[:, :4] = scale_coords(img.shape[2:], detections[:, :4], frame.shape).round() for x1, y1, x2, y2, conf, cls in detections: cv2.rectangle(frame, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2) cv2.putText(frame, f'{int(cls)} {conf:.2f}', (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2) # 将图像转换为QImage并显示在标签上 h, w, c = frame.shape qimg = QImage(frame.data, w, h, w * c, QImage.Format_RGB888) pixmap = QPixmap.fromImage(qimg) label.setPixmap(pixmap) # 等待一段时间并检查是否按下了退出键 if cv2.waitKey(1) == 27: break # 释放摄像头并关闭窗口 cap.release() cv2.destroyAllWindows() ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕溪同学

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值