前引

由于在对移动的人体进行拍照的效果并不好,所以对抓拍图片版进行了升级,通过人体红外感应器在来访人员进入房间后录制视频,视频能比较好的看清来访人员起到看家的作用。

拍照版

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import RPi.GPIO as GPIO  ##引入GPIO模块
import time
from picamera2 import Picamera2, Preview
from picamera2.encoders import H264Encoder
from picamera2.outputs import FfmpegOutput

def video():
now = time.strftime("%Y%m%d%H%M%S",time.localtime(time.time()))
file_name="/Disk/MyCloud/Safe/"+now+".mp4"
picam2 = Picamera2()
video_config = picam2.create_video_configuration()
picam2.configure(video_config)

encoder = H264Encoder(10000000)
output = FfmpegOutput(file_name)

picam2.start_recording(encoder, output)
time.sleep(5)
picam2.stop_recording()
picam2.close()

HC_Pin = 18

GPIO.setmode(GPIO.BCM)
GPIO.setup(HC_Pin, GPIO.IN)

while True:
if(GPIO.input(HC_Pin)):
now = time.strftime("%Y-%m-%d %H:%M:%S",time.localtime(time.time()))
f = open('/home/zh/code/.log','a',encoding='utf-8')
try:
video()
except:
str=now+' 相机调用失败\n'
f.writelines(str)
finally:
str=now+" 有人来访\n"
f.writelines(str)
f.close()
time.sleep(1)