我正在使用 VS 代碼作為 IDE 來撰寫我的代碼以使用 Opencv python 進行移動物件檢測,但我有錯誤
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'
那你能給我解決方案嗎。我正在為您提供我的代碼
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()
uj5u.com熱心網友回復:
您的問題很可能是 openCV 無法讀取您的網路攝像頭,或者您的網路攝像頭的索引可能是錯誤的,或者它沒有查看您的網路攝像頭的權限。如果您仍然需要更多幫助,請點擊此處打開 CVs 指南的鏈接,以解決無型別問題。
關于解決無型別錯誤的頁面
https://www.pyimagesearch.com/2016/12/26/opencv-resolving-nonetype-errors/
uj5u.com熱心網友回復:
首先,像你一樣閱讀框架是不好的做法。cam.read() 的另一個回傳值是有目的的。
success, cv_img = cam.read()
if success:
# do whatever
else:
print('capturing failed')
其次,如果如上所述,這是一個相機問題,請嘗試禁用防病毒。我多次看到凸輪無法打開,因為它被殺毒軟體阻止了。
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/426647.html
