我撰寫了一段代碼來檢測我家中發生的任何型別的入侵,如果檢測到任何型別的物件,就會形成邊界框,然后需要執行進一步的操作。當我在設備的相機(筆記本電腦相機)上測驗代碼時,它作業正常。但是當我在帶有攝像頭模塊(不是 USB 攝像頭)的 Raspberry Pi 3B 上運行相同的代碼時,它崩潰并且腳本停止執行。
我在這里附上代碼片段
import cv2
import numpy as np
cv2.namedWindow("Preview")
cam = cv2.VideoCapture(0)
while cam.isOpened():
ret, frame1 = cam.read()
ret, frame2 = cam.read()
diff = cv2.absdiff(frame1,frame2)
grey = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(grey, (5, 5), 0)
_, thresh = cv2.threshold(blur, 20, 255,cv2.THRESH_BINARY)
dilated = cv2.dilate(thresh, None, iterations=3)
contours, _ = cv2.findContours(dilated, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for c in contours:
if cv2.contourArea(c) >= 10000:
x,y,w,h = cv2.boundingRect(frame1)
cv2.rectangle(frame1,(x,y),(x w,y h), (0,255,0),3)
else:
continue
if not ret:
break
cv2.imshow("Preview", frame1)
if cv2.waitKey(10)==ord('q'):
break
cam.release()
cv2.destroyAllWindows()
我收到的錯誤訊息是
x,y,w,h = cv2.boundingRect(frame1)
cv2.error: OpenCV(4.4.0) /tmp/pip-wheel-ggn8r4df/opencv-contrib-python_52a54d5431f647899265a8f5082f7e73/opencv/modules/imgproc/src/shapedescr.cpp:1044: error: (-215:Assertion failed) img.depth() <= CV_8S && img.channels() == 1 in function 'maskBoundingRect'
uj5u.com熱心網友回復:
感謝@toyata Supra。錯誤已解決,程式按預期執行。
解決方案:
x,y,w,h = cv2.boundingRect(c)
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/479045.html
標籤:Python opencv 树莓派 物体检测 树莓派3
