python 中的 raspberry pi 相机无法拍摄图像

can't capture image by raspberry pi camera in python

我在尝试使用 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。

请帮我解决这个问题。

编辑文件 /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