I wrote a snippet of code to detect any kind of intrusions happening at my home and if any kind of object is detected, bounding Boxes are formed and then further actions needs to be performed. When i Test the code on my device's camera (Laptop camera) It is working fine. But when i run the same piece of code on the Raspberry Pi 3B with Camera Module (Not USB Camera), It crashes and script stops executing.
I'm attaching the snippet of code here
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()
The Error message I'm getting is
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'
CodePudding user response:
Thanks to @toyata Supra. The error was solved and program is executing as expected.
Solution:
x,y,w,h = cv2.boundingRect(c)