Python 使用红外传感器触发的Raspberry Pi摄像头将多个视频录制到不同的文件名
我目前正在设置我的Raspberry Pi(当由红外传感器触发时)录制第二十二个视频,每个视频都记录到一个新文件中 当前,当检测到移动时,它将返回此错误: “回溯(最近一次呼叫最后一次): 文件“pir_2filenametest2.py”,第57行,在 对于camera.start_录制('pivid{counter:03}.h264')中的文件名: TypeError:“非类型”对象不可编辑 我对python几乎是全新的,所以任何帮助都会很好 代码如下:Python 使用红外传感器触发的Raspberry Pi摄像头将多个视频录制到不同的文件名,python,Python,我目前正在设置我的Raspberry Pi(当由红外传感器触发时)录制第二十二个视频,每个视频都记录到一个新文件中 当前,当检测到移动时,它将返回此错误: “回溯(最近一次呼叫最后一次): 文件“pir_2filenametest2.py”,第57行,在 对于camera.start_录制('pivid{counter:03}.h264')中的文件名: TypeError:“非类型”对象不可编辑 我对python几乎是全新的,所以任何帮助都会很好 代码如下: # Author : Matt Ha
# Author : Matt Hawkins
# Date : 21/01/2013
# Import required Python libraries
import RPi.GPIO as GPIO
import time
import picamera
# Use BCM GPIO references
# instead of physical pin numbers
GPIO.setmode(GPIO.BCM)
# Define GPIO to use on Pi
GPIO_PIR = 7
print "Wilbur Cam! (CTRL-C to exit)"
# Set pin as input
GPIO.setup(GPIO_PIR,GPIO.IN) # Echo
Current_State = 0
Previous_State = 0
camera = picamera.PiCamera()
try:
print "Waiting for PIR to settle ..."
# Loop until PIR output is 0
while GPIO.input(GPIO_PIR)==1:
Current_State = 0
print " Ready"
# Loop until users quits with CTRL-C
while True :
# Read PIR state
Current_State = GPIO.input(GPIO_PIR)
if Current_State==1 and Previous_State==0:
# PIR is triggered
print " Motion detected!"
# Record previous state
Previous_State=1
# Camera begins to record
camera.resolution = (1360, 768)
for filename in camera.start_recording('pivid{counter:03}.h264'):
print('Captured %s' % filename)
time.sleep(20)
camera.stop_recording()
elif Current_State==0 and Previous_State==1:
# PIR has returned to ready state
print " Ready"
Previous_State=0
# Wait for 10 milliseconds
time.sleep(0.01)
except KeyboardInterrupt:
print " Quit"
# Reset GPIO settings
GPIO.cleanup()
不返回值,开始录制,第一个arg输出
是写入视频的内容,您可以将其作为字符串或文件对象传递,但无需迭代:
开始录制(输出,格式=无,大小=无,拆分器端口=1,**选项)
开始从摄像机录制视频,并将其存储在输出中。
如果输出是字符串,它将被视为视频将写入的新文件的文件名。否则,假设输出是一个类似文件的对象,并将视频数据附加到该对象上(实现仅假设该对象具有write()方法-不会调用其他方法)。
因此,在您的示例中,文件名是'pivid{counter:03}.h264')
如果您想要不同的文件名,可以使用如下内容:
i = 0 # set i to 0 outside the while
while True :
# Read PIR state
Current_State = GPIO.input(GPIO_PIR)
if Current_State==1 and Previous_State==0:
# PIR is triggered
print " Motion detected!"
# Record previous state
Previous_State=1
# Camera begins to record
camera.resolution = (1360, 768)
camera.start_recording('pivid{}.h264'.format(i)) # pass i to str.format
print('Captured pivid{}.264'.format(i))
i += 1 # increase i
time.sleep(20)
camera.stop_recording()