Why do I get this error? NoneType object has no attribute shape in opencv

2024/10/8 20:35:06

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 FPSfrom 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]
Answer

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.

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

https://en.xdnf.cn/q/118669.html

Related Q&A

Efficiently concatenate two strings from tuples in a list?

I want to concatenate the two string elements in a list of tuplesI have this:mylist = [(a, b), (c, d), (e, f), (g, h)] myanswer = []for tup1 in mylist:myanswer.append(tup1[0] + tup[1])Its working but i…

How to assert that a function call does not return an error with unittest?

Is there anyway with unittest to just assert that a function call does not result in an error, whether it is a TypeError, IOError, etc.example:assert function(a,b) is not errororif not assertRaises fun…

How to calculate Python float-number-th root of float number

I found the following answer here on Stackoverflow:https://stackoverflow.com/a/356187/1829329But it only works for integers as n in nth root:import gmpy2 as gmpyresult = gmpy.root((1/0.213), 31.5).real…

Need help making a Hilbert Curve using numbers in Python

I want to make a function that will create a Hilbert Curve in python using numbers. The parameters for the function would be a number and that will tell the function how many times it should repeat. To…

How do I separate each list [closed]

Its difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying thi…

Why python doesnt see the members of quantumCircuit class qiskit

I`m trying to learn the programming on quantum computers. I have installed qiskit in VS Code (all qiskit extentions available in VS Code market) , python compilator (from Vs Code market "Python&qu…

Compare multiple lines in a text file using python

Im writing a program that is time tracking report that reports the time that was spent in each virtual machine, I`m having a problem comparing the txt file cause the only thing that changes are the num…

Error installing eomaps through conda and pip

I am getting the following error output when trying to install eomaps using Conda. I dont know how to solve this. I have also tried the same using pip but it didnt seem to solve the problem. Here is th…

DFS on a graph using a python generator

I am using a generator to do a full search on a graph, the real data set is fairly large, here is a portion of the code i wrote on a small data set:class dfs:def __init__(self):self.start_nodes = [1,2]…

Importing test libraries failed. No module named a

I have a folder /example which contains /Libs which further contains different folders /a, /b each containing python libraries. I am trying to run a robot framework code from /example.The error it show…