I am using VS code as a IDE for writing my code for Moving object detection using Opencv python but I have error
Traceback (most recent call last):
File "d:\Programming\Python programming\Moving_Object_detection.py", line 13, in
img = imutils.resize(img, width=500)
File "D:\Python Vscode\lib\site-packages\imutils\convenience.py", line 69, in resize
(h, w) = image.shape[:2]
AttributeError: 'NoneType' object has no attribute 'shape'
So can you give me the solution for it. I am providing you my code
import cv2
import time
import imutils
cam = cv2.VideoCapture(2)
time.sleep(1)
firstFrame = None
area = 500
# count = 0
while True:
_, img = cam.read()
text = "Normal"
img = imutils.resize(img, width=500, height=500)
grayImg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gaussianImg = cv2.GaussianBlur(grayImg, (21, 21), 0)
if firstFrame is None:
firstFrame = gaussianImg
continue
imgDiff = cv2.absdiff(firstFrame, gaussianImg)
threshImg = cv2.threshold(imgDiff, 25, 255, cv2.THRESH_BINARY)[1]
threshImg = cv2.dilate(threshImg, None, iterations=2)
cnts = cv2.findContours(threshImg.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
for c in cnts:
if cv2.contourArea(c) < area:
continue
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(img, (x, y), (x w, y h), (0, 255, 0), 2)
# count = count 1
text = "Moving Object detected"
print(text)
cv2.putText(img, text, (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.imshow("cameraFeed", img)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cam.release()
cv2.destroyAllWindows()
CodePudding user response:
Most likely your issue is that openCV can't read you webcam either the index of your webcam might be wrong or it doesn't have permission to view your webcam. If you still need some more help here is a link to open CVs guide to resolving none type issues.
A page on resolving none type errors
https://www.pyimagesearch.com/2016/12/26/opencv-resolving-nonetype-errors/
CodePudding user response:
first, that is bad practice to read frames like you do. there is a purpose for the other return value from the cam.read().
success, cv_img = cam.read()
if success:
# do whatever
else:
print('capturing failed')
second, if as mentioned, this is a camera issue, try disabling the anti virus. I saw several of times that the cam wouldn't open because it was blocked by the anti virus.