typeerror:必须使用PiCamera实例作为第一个参数调用unbound方法start_preview()(没有任何内容)

时间:2016-10-25 03:00:01

标签: python raspberry-pi

使用raspberry-pi时遇到此问题的任何人?以下代码:

from gpiozero import MotionSensor
from picamera import PiCamera

camera = PiCamera()
pir = MotionSensor(4)
while True:
    pir.wait_for_motion()
    camera.start_preview()
    pir.wait_for_no_motion()
    camera.stop_preview()

完整追踪:

traceback(most recent call last);
file "/home/pi/motion.py", line 11, in<module>
camera.start_preview()
typeError : unbound method start_preview() must be called with PiCamera instance as first argument (got nothing instead)

@owenbradstreet

Traceback(most recent call last):
  File "/home/pi/motion.py",line 7,in<module>
   with picamera.PiCamera()as camera:
  File "/usr/lib/pyton2.7/dist-packages/picamera/camera`enter code here`.py",line 415, in_init_
   self.init_camera(camera_num,sereo_mode,stereo_decimate)
  File "/usr/lib/pyton2.7/dist-packages/picamera.py".line 444. in _init_camera
   "Camera is not enabled.Try running 'sudo raspi-config'"
PiCameraError:Camera is not enabled.

尝试运行&#39; sudo raspi-config&#39;并确保已启用相机。 事情是我已经启用了相机:(

2 个答案:

答案 0 :(得分:0)

Raspberry Pi相机可能非常挑剔。确保相机实例化不会引发错误,并且阳光充电器确实在那里。但更有可能的问题是,相机需要几秒钟才能在第一次预览时启动。尝试在第一个time.sleep(2)之后添加start_preview()(您可能希望在while循环之前添加一个)

答案 1 :(得分:0)

尝试改为:

from gpiozero import MotionSensor
import picamera

pir = MotionSensor(4)

while True:
    with picamera.PiCamera() as camera:
        camera.resolution = (*YOUR LENGTH HERE*, *YOUR WIDTH HERE*)
        pir.wait_for_motion()
        camera.start_preview()
        pir.wait_for_no_motion()
        camera.stop_preview()

所有这一切都是import picamera分开执行,然后使用相机作为相机执行代码&#39;。这也意味着您不需要关闭流。

希望这有帮助!