树莓派4B目标/动物识别报错:resize函数断言失败(-215)
解决OpenCV resize断言错误:!ssize.empty()
错误原因
这个错误的核心是传入net.detect()的图像为空——你的代码中cap.read()没有成功读取到摄像头帧,导致img是无效的空对象,后续OpenCV的resize操作因缺少图像尺寸信息触发断言失败。
解决方案
1. 增加摄像头帧读取校验
修改主循环逻辑,先判断帧读取是否成功,避免传入空图像到检测函数:
if __name__ == "__main__": cap = cv2.VideoCapture(0) cap.set(3,640) cap.set(4,480) while True: success, img = cap.read() # 检查是否成功读取帧 if not success: print("摄像头帧读取失败,退出循环") break result, objectInfo = getObjects(img,0.45,0.2) cv2.imshow("Output", result) # 按q键退出循环,同时释放资源 if cv2.waitKey(1) & 0xFF == ord('q'): break # 释放摄像头资源和窗口 cap.release() cv2.destroyAllWindows()
2. 确认摄像头设备索引
树莓派的摄像头设备索引可能不是0,执行以下命令查看可用视频设备:
ls /dev/video*
如果输出包含/dev/video1,尝试修改VideoCapture的参数:
cap = cv2.VideoCapture(1) # 或者指定V4L2驱动确保兼容性 cap = cv2.VideoCapture(0, cv2.CAP_V4L2)
3. 验证摄像头硬件与系统设置
- 确认摄像头已正确连接到树莓派CSI接口
- 运行
sudo raspi-config,进入Interface Options->Camera启用摄像头,重启树莓派生效
4. 修复摄像头访问权限
将当前用户添加到video组以获取设备访问权限:
sudo usermod -aG video administrador
注销并重新登录后生效。
原问题代码与报错信息
import cv2 #thres = 0.45 # Threshold to detect object classNames = [] classFile = "/home/administrador/Desktop/Object_Detection_Files/coco.names" with open(classFile,"rt") as f: classNames = f.read().rstrip("\n").split("\n") configPath = "/home/administrador/Desktop/Object_Detection_Files/ssd_mobilenet_v3_large_coco_2020_01_14.pbtxt" weightsPath = "/home/administrador/Desktop/Object_Detection_Files/frozen_inference_graph.pb" net = cv2.dnn_DetectionModel(weightsPath,configPath) net.setInputSize(320,320) net.setInputScale(1.0/ 127.5) net.setInputMean((127.5, 127.5, 127.5)) net.setInputSwapRB(True) def getObjects(img, thres, nms, draw=True, objects=[]): classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms) #print(classIds,bbox) if len(objects) == 0: objects = classNames objectInfo =[] if len(classIds) != 0: for classId, confidence,box in zip(classIds.flatten(),confs.flatten(),bbox): className = classNames[classId - 1] if className in objects: objectInfo.append([box,className]) if (draw): cv2.rectangle(img,box,color=(0,255,0),thickness=2) cv2.putText(img,classNames[classId-1].upper(),(box[0]+10,box[1]+30), cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) cv2.putText(img,str(round(confidence*100,2)),(box[0]+200,box[1]+30), cv2.FONT_HERSHEY_COMPLEX,1,(0,255,0),2) return img,objectInfo if __name__ == "__main__": cap = cv2.VideoCapture(0) cap.set(3,640) cap.set(4,480) #cap.set(10,70) while True: success, img = cap.read() result, objectInfo = getObjects(img,0.45,0.2) #print(objectInfo) cv2.imshow("Output",img) cv2.waitKey(1)
报错回溯:
Traceback (most recent call last): File "/home/administrador/Desktop/Object_Detection_Files/object-ident.py", line 50, in <module> result, objectInfo = getObjects(img,0.45,0.2) File "/home/administrador/Desktop/Object_Detection_Files/object-ident.py", line 21, in getObjects classIds, confs, bbox = net.detect(img,confThreshold=thres,nmsThreshold=nms) cv2.error: OpenCV(4.4.0) /home/administrador/opencv-4.4.0/modules/imgproc/src/resize.cpp:3929: error: (-215:Assertion failed) !ssize.empty() in function 'resize'
内容的提问来源于stack exchange,提问作者Samuel LAZALDE HERNANDEZ
相关产品推荐
相关产品推荐

