#Project 13 - Burglar Detector With Photo Capture #latest code updates available at: https://github.com/RuiSantosdotme/RaspberryPiProject #project updates at: https://nostarch.com/RaspberryPiProject
#import the necessary packages from gpiozero import Button, MotionSensor from picamera import PiCamera from time import sleep from signal import pause
#create objects that refer to a button, #a motion sensor and the PiCamera button = Button(2) pir = MotionSensor(4) camera = PiCamera()
#start the camera camera.start_preview()
#image image names i = 0
#stop the camera when the pushbutton is pressed defstop_camera(): camera.stop_preview() #exit the program exit()
#take photo when motion is detected deftake_photo(): global i i = i + 1 # 自己选择拍照后要安装的位置 camera.capture('/home/pi/Desktop/image_%s.jpg' % i) print('A photo has been taken') sleep(10)
#assign a function that runs when the button is pressed button.when_pressed = stop_camera #assign a function that runs when motion is detected pir.when_motion = take_photo