无法通过python中的raspberry pi相机捕获图像

时间:2017-07-13 04:15:41

标签: python python-3.x camera raspberry-pi raspbian

尝试使用Raspberry Pi相机拍摄图像时出现问题。

执行此代码时:

import sys

sys.path.append('/usr/local/lib/python3.4/site-packages')
import cv2
import numpy as np
import detect
cap = cv2.VideoCapture(0)

while(True):
    ret, frame = cap.read()
    img = detect.detect(frame)

    cv2.imshow('frame',img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()

我收到错误:

Traceback (most recent call last):
...
...
...
TypeError: 'NoneType' object is not subscriptable

我搜索解决了这个问题并找到了一个命令进入终端:

sudo modprobe bcm2835-v4l2

它会暂时解决问题,但在重新启动Raspberry Pi之后会再次出现。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

编辑文件/etc/modules(以root用户身份)并添加bcm2835-vl42行,如下所示:

# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.


# v4l2 added for OpenCV video capture
bcm2835-v4l2