-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcameraStreamBash1.py
103 lines (84 loc) · 2.63 KB
/
cameraStreamBash1.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
# Copyright 2016 Keith Ellis
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This Python script sends picamera video output to stout,
# it is intended to be called from a bash script which pipes
# the output into ffmpeg
#
# Bash script is startPythonStream.sh
import os
import time
import picamera
import sys
from signal import pause
from time import sleep, time
from datetime import datetime
import thingspeak
sys.stdout = os.fdopen(sys.stdout.fileno(), 'wb', 0)
snapshot_frequency = 60 * 60 #frequency of snapshops in seconds
temp_frequency = 2 * 60 #frequency of temperature updates
max_images = 24 # Maximum number of images to keep
#ThingSpeak keys
channel_id = "88282"
write_key = "LE6B6L2ZRZ592AA8"
channel = thingspeak.Channel(id=channel_id,write_key=write_key)
def get_temp(sensor):
if sensor == 'internal':
field_id = 2
else:
field_id = 1
temp = channel.get_field_last_text(field=field_id)
temp = str(round(float(temp),1))
return temp
def start_stream():
start= time()
last_temp = start
last_snap = start
snap_shot_count = 0
ext_temp = get_temp('external')
camera = picamera.PiCamera()
camera.resolution = (960, 540)
camera.framerate = 25
camera.vflip = True
camera.hflip = True
camera.annotate_background = picamera.Color('black')
camera.start_recording(sys.stdout, bitrate = 500000, format='h264')
while True:
if time()- last_temp > temp_frequency:
ext_temp = get_temp('external')
last_temp = time()
i = datetime.now()
now = i.strftime('%d %b %Y - %H:%M:%S')
camera.annotate_text = (' Bird Cam - {0} - Ext temp = {1}C '
.format(now, ext_temp))
try:
camera.wait_recording(0.2)
except picamera.exc.PiCameraError:
camera.close()
break
except BrokenPipeError:
camera.close()
break
if time() - last_snap > snapshot_frequency: # Take snap shot
snap_shot('image'+str(snap_shot_count), camera)
last_snap = time()
if snap_shot_count >= max_images:
snap_shot_count = 0
else:
snap_shot_count += 1
#Function to take a snapshop image
def snap_shot(name, camera):
camera.capture('images/'+ name +'.jpg', use_video_port=True)
start_stream()