为什么我会收到这个错误?'NoneType' 对象在 opencv 中没有属性 'shape' [英] Why do I get this error? 'NoneType' object has no attribute 'shape' in opencv

查看:157
本文介绍了为什么我会收到这个错误?'NoneType' 对象在 opencv 中没有属性 'shape'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究实时服装检测.所以我像这样从 GitHub 借用了代码:https://github.com/rajkbharali/实时衣服检测但是 (H, W) = frame.shape[:2]:following error in last line.我应该在哪里修复它?

I'm working on real-time clothing detection. so i borrowed the code from GitHub like this:https://github.com/rajkbharali/Real-time-clothes-detection but (H, W) = frame.shape[:2]:following error in last line. Where should I fix it?

from time import sleep
import cv2 as cv
import argparse
import sys
import numpy as np
import os.path
from glob import glob
import imutils
from imutils.video import WebcamVideoStream
from imutils.video import FPS

from google.colab import drive
drive.mount('/content/drive')

%cd /content/drive/My Drive/experiment/Yolo_mark-master/x64/Release/

Labels = []
classesFile1 = "data/obj.names";
with open(classesFile1, 'rt') as f:
    Labels = f.read().rstrip('\n').split('\n')

np.random.seed(42)
COLORS = np.random.randint(0, 255, size=(len(Labels), 3), dtype="uint8")

weightsPath = "obj_4000.weights"
configPath = "obj.cfg"

net1 = cv.dnn.readNetFromDarknet(configPath, weightsPath)
net1.setPreferableBackend(cv.dnn.DNN_BACKEND_OPENCV)
net1.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)


image = WebcamVideoStream(src=0).start()
fps = FPS().start()
#'/home/raj/Documents/yolov3-Helmet-Detection-master/safety.mp4'

#while fps._numFrames<100:
while True:
#for fn in glob('images/*.jpg'):
    frame = image.read()
    #frame = imutils.resize(frame,width=500)
    (H, W) = frame.shape[:2]

推荐答案

出现错误的原因是框架是 None(Null).有时,从网络摄像头捕获的第一帧是 None 主要是因为(1) 网络摄像头还没有准备好(它需要一些额外的时间来准备好)或 (2) 操作系统不允许您的代码访问网络摄像头.

The reason behind your error is that the frame is None(Null). Sometimes, the first frame that is captured from the webcam is None mainly because (1) the webcam is not ready yet ( and it takes some extra second for it to get ready) or (2) the operating system does not allow your code to access the webcam.

在第一种情况下,在对框架执行任何操作之前,您需要检查框架是否有效:

In the first case, before you do anything on the frame you need to check whether the frame is valid or not :

while True:
 
    frame = image.read()
    if frame is not None:  # add this line
       
      (H, W) = frame.shape[:2]

在其他情况下,您需要检查操作系统中的相机设置.

In the other case, you need to check the camera setting in your Operating system.

此外,为了捕获网络摄像头帧,还有另一种基于 VideoCapure 的方法 Opencv 中可能更容易调试的类.

Also, for capturing the webcam frames there is another method based on the VideoCapure class in Opencv that might be easier to debug.

这篇关于为什么我会收到这个错误?'NoneType' 对象在 opencv 中没有属性 'shape'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆