forked from milnepe/pi-hq-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhq-camera.py
executable file
·131 lines (100 loc) · 2.87 KB
/
hq-camera.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#!/usr/bin/python3
from gpiozero import DigitalOutputDevice, LED, Button
from signal import pause
from subprocess import check_call, Popen
from picamera import PiCamera
from datetime import datetime
from time import sleep
user = "yourusername"
remote_dir = "hostname:~/remotepath"
image_dir = "/home/pi/Pictures/"
video_dir = "/home/pi/Videos/"
filename = None
states = ['shutdown', 'still', 'video']
state = states[0]
Button.was_held = False
video_state = False
camera = PiCamera()
# Set pin 24 HIGH
led_power = DigitalOutputDevice(24, initial_value=True)
# State Indicator
led = LED(23)
# Set pin 17 HIGH
btn_power = DigitalOutputDevice(17, initial_value=True)
# Button with external pull down resistor
btn = Button(27, pull_up=None, active_state=True, hold_time=2)
def take_still():
global filename
print("click...")
camera.stop_preview()
filename = datetime.now().strftime('%Y%m%dT%H%M%S')
# Take a shot
camera.capture(f'{image_dir}{filename}.jpg')
print(f'{image_dir}{filename}.jpg')
# copy to remote dir using secure copy
# Popen(f'scp {video_dir}{filename}.jpg {user}@{remote_dir}.jpg', shell=True)
# print(f'image transfered...')
# Re-start preview mode
camera.start_preview()
def take_video():
global video_state
global filename
if video_state is False:
filename = datetime.now().strftime('%Y%m%dT%H%M%S')
video_state = True
print(f'start recording {filename}.h264')
# record video to tmp dir
camera.start_recording(f'/tmp/{filename}.h264')
else:
video_state = False
camera.stop_recording()
print("stop video...")
sleep(2)
# convert to mp4 and save in video dir
Popen(f'MP4Box -add /tmp/{filename}.h264 {video_dir}{filename}.mp4', shell=True)
print(f'Done encoding...')
def do_shutdown():
print("shutdown...")
check_call(['sudo', 'poweroff'])
def change_state():
global state
if state is 'shutdown':
state = states[1]
led.blink()
camera.start_preview()
elif state is 'still':
state = states[2]
led.blink(on_time=0.25, off_time=0.25)
camera.start_preview()
elif state is 'video':
state = states[0]
led.on()
camera.stop_preview()
print("state: ", state)
def op():
global state
if state is 'still':
take_still()
elif state is 'video':
take_video()
elif state is 'shutdown':
do_shutdown()
def held(btn):
btn.was_held = True
print("button was held not just pressed")
change_state()
def released(btn):
if not btn.was_held:
pressed()
btn.was_held = False
def pressed():
print("button was pressed not held")
op()
def init_camera():
camera.rotation = 270
print("state: ", state)
led.on()
init_camera()
btn.when_held = held
btn.when_released = released
pause()