YOLOv7调用摄像头检测报错解决

yolov7detect.py文件调用本地摄像头,把source参数设为0

    parser.add_argument('--source', type=str, default='0', help='source')  # file/folder, 0 for webcam

报错:cv2.error: OpenCV(3.4.2) 一堆地址:The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support. If you are on Ubuntu or Debian, install libgtk2.0-dev and pkg-config, then re-run cmake or configure script in function 'cvShowImage'

我原本的环境里opencv-python版本是4.1.2.30

参考链接:The function is not implemented. Rebuild the library with Windows, GTK+ 2.x or Carbon support-CSDN博客

我的解决方法,卸载opencv-python

pip uninstall opencv-python

再重新安装

pip install opencv-python
pip install opencv-contrib-python

就好使了

  • 9
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 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
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值