使用HOG描述符的peopledetect.py报错求助(Rpi3+OpenCV3)
Hey there, let's figure out why you're hitting that frustrating assertion error with your Raspberry Pi 3, Logitech C170, and OpenCV 3.3.0. That error almost always means the frame you're passing to hog.detectMultiScale() is empty—so let's fix the root cause step by step:
1. 修正摄像头设备号的传入方式
First off, you wrote cap=cv2.VideoCapture('0')—but OpenCV expects an integer for camera device IDs, not a string. Change that line to:
cap = cv2.VideoCapture(0)
This is a super common gotcha, and it's often the reason the camera fails to initialize properly.
2. 添加摄像头初始化和帧读取的检查逻辑
Even if you fix the device ID, you need to make sure the camera is actually opening and returning valid frames. Add these checks right after initializing the capture object:
cap = cv2.VideoCapture(0) # 检查摄像头是否成功打开 if not cap.isOpened(): print("Failed to open camera! Check connections or device ID.") exit() while True: # 读取一帧,ret是布尔值表示是否读取成功 ret, frame = cap.read() # 如果帧读取失败(比如摄像头断开),退出循环 if not ret: print("Couldn't read frame—stream might have ended.") break # 把你的HOG检测代码放在这里 found, w = hog.detectMultiScale(frame, winStride=(8,8), padding=(32,32), scale=1.05) # ... 剩下的绘制框等代码
This prevents empty frames from being passed to the HOG detector, which is exactly what's triggering that dims <= 2 && step[0] > 0 error.
3. 检查摄像头设备权限
Sometimes the pi user doesn't have permission to access the USB camera device. To fix this:
- First, check what video devices are available with:
You should see something likels /dev/video*/dev/video0. - Then, temporarily grant access with:
sudo chmod 666 /dev/video0 - For a permanent fix, add your user to the
videogroup:
Log out and back in for this change to take effect.sudo usermod -aG video pi
4. 验证摄像头硬件连接
Double-check that your Logitech C170 is firmly plugged into a USB port on the Raspberry Pi. Sometimes loose connections can cause intermittent frame reads. You can also test the camera with a tool like fswebcam to confirm it works outside of OpenCV:
sudo apt install fswebcam fswebcam test.jpg
If this command saves a valid image, your camera is working fine, and the issue is definitely in your OpenCV code setup.
内容的提问来源于stack exchange,提问作者Marije Miedema

