我在树莓派中使用HOG(定向梯度直方图)进行人体检测,但它是如此缓慢,因此为了使其快速运行,我将运动检测与HOG一起使用。因此,如果有任何动作,该代码将从动作检测开始,将裁剪出该动作所在的部分,并将其输入到HOG中,以查看裁剪后的图像是否有人。但是我得到这个错误
(rects, weights) = hog.detectMultiScale(gray_frame)
cv2.error: OpenCV(3.4.3) /home/pi/packaging/opencv-
python/opencv/modules/core/src/matrix.cpp:423: error: (-215:Assertion
failed) 0 <= _rowRange.start && _rowRange.start <= _rowRange.end &&
_rowRange.end <= m.rows in function 'Mat'
和
Segmentation fault
这是我的代码:
from picamera.array import PiRGBArray
import picamera
import warnings
import datetime
import imutils
import time
import cv2
import numpy as np
client = None
camera = picamera.PiCamera()
camera.resolution = (368,240)
camera.framerate = 20
camera.rotation = 180
rawCapture = PiRGBArray(camera, size=(368,240))
hog = cv2.HOGDescriptor()
hog.setSVMDetector(cv2.HOGDescriptor_getDefaultPeopleDetector())
print("[INFO] warming up...")
time.sleep(2.5)
avg = None
for f in camera.capture_continuous(rawCapture, format="bgr",
use_video_port=True):
frame = f.array
orig = frame.copy()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (21, 21), 0)
if avg is None:
avg = gray.copy().astype("float")
rawCapture.truncate(0)
continue
cv2.accumulateWeighted(gray, avg, 0.5)
frameDelta = cv2.absdiff(gray, cv2.convertScaleAbs(avg))
thresh = cv2.threshold(frameDelta, 10, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
for c in cnts:
if cv2.contourArea(c) < 2500:
continue
print("MOTION DETECTED")
time.sleep(1)
(x, y, w, h) = cv2.boundingRect(c)
framea = frame[y:y+h, x:x+w]
gray_frame = cv2.cvtColor(framea, cv2.COLOR_RGB2GRAY)
(rects, weights) = hog.detectMultiScale(gray_frame)
for i, (x, y, w, h) in enumerate(rects):
if weights[i] < 0.7:
continue
cv2.rectangle(frame, (x,y), (x+w,y+h),(0,255,0),2)
print("*******WARNING******")
cv2.imshow("cropped image", framea)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
cv2.imshow("Security Feed", frame)
key = cv2.waitKey(1) & 0xFF
if key == ord("q"):
break
rawCapture.truncate(0)
我认为问题出在裁剪部分,但是我现在不解决该问题
(x, y, w, h) = cv2.boundingRect(c)
framea = frame[y:y+h, x:x+w]
gray_frame = cv2.cvtColor(framea, cv2.COLOR_RGB2GRAY)
(rects, weights) = hog.detectMultiScale(gray_frame)