-
Notifications
You must be signed in to change notification settings - Fork 1
/
Camera.py
123 lines (113 loc) · 4.27 KB
/
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
# -*- coding: utf-8-*-
import os
import subprocess
import time
import sys
WORDS = [u"PAIZHAO", u"ZHAOPIAN"]
SLUG = "camera"
def handle(text, mic, profile, wxbot=None):
"""
Reports the current time based on the user's timezone.
Arguments:
text -- user-input, typically transcribed speech
mic -- used to interact with the user (for both input and output)
profile -- contains information related to the user (e.g., phone
number)
wxbot -- wechat bot instance
"""
sys.path.append(mic.dingdangpath.LIB_PATH)
from app_utils import sendToUser
quality = 100
count_down = 3
dest_path = os.path.expanduser('~/pictures')
vertical_flip = False
horizontal_flip = False
send_to_user = True
sound = True
usb_camera = False
# read config
if profile[SLUG] and 'enable' in profile[SLUG] and \
profile[SLUG]['enable']:
if 'count_down' in profile[SLUG] and \
profile[SLUG]['count_down'] > 0:
count_down = profile[SLUG]['count_down']
if 'quality' in profile[SLUG] and \
profile[SLUG]['quality'] > 0:
quality = profile[SLUG]['quality']
if 'dest_path' in profile[SLUG] and \
profile[SLUG]['dest_path'] != '':
dest_path = profile[SLUG]['dest_path']
if 'vertical_flip' in profile[SLUG] and \
profile[SLUG]['vertical_flip']:
vertical_flip = True
if 'horizontal_flip' in profile[SLUG] and \
profile[SLUG]['horizontal_flip']:
horizontal_flip = True
if 'send_to_user' in profile[SLUG] and \
not profile[SLUG]['send_to_user']:
send_to_user = False
if 'sound' in profile[SLUG] and \
not profile[SLUG]['sound']:
sound = False
if 'usb_camera' in profile[SLUG] and \
profile[SLUG]['usb_camera']:
usb_camera = True
if any(word in text for word in [u"安静", u"偷偷", u"悄悄"]):
sound = False
try:
if not os.path.exists(dest_path):
os.makedirs(dest_path)
except Exception:
mic.say(u"抱歉,照片目录创建失败")
return
dest_file = os.path.join(dest_path, "%s.jpg" % time.time())
if usb_camera:
command = "fswebcam --no-banner -r 1024x765 -q "
if vertical_flip:
command = command+' -s v '
if horizontal_flip:
command = command+'-s h '
command = command+dest_file
else:
command = ['raspistill', '-o', dest_file, '-q', str(quality)]
if count_down > 0 and sound:
command.extend(['-t', str(count_down*1000)])
if vertical_flip:
command.append('-vf')
if horizontal_flip:
command.append('-hf')
if sound and count_down > 0:
mic.say(u"收到,%d秒后启动拍照" % (count_down))
if usb_camera: time.sleep(count_down)
process = subprocess.Popen(command, shell=usb_camera)
res = process.wait()
if res != 0:
if sound:
mic.say(u"拍照失败,请检查相机是否连接正确")
return
if sound:
mic.play(mic.dingdangpath.data('audio', 'camera.wav'))
# send to user
if send_to_user:
target = '邮箱'
if wxbot is not None and wxbot.my_account != {} and \
('prefers_email' not in profile or
not profile['prefers_email']):
target = '微信'
if sound:
mic.say(u'拍照成功!正在发送照片到您的%s' % target)
if sendToUser(profile, wxbot, u"这是刚刚为您拍摄的照片", "", [dest_file], []):
if sound:
mic.say(u'发送成功')
else:
if sound:
mic.say(u'发送失败了')
else:
mic.say(u"请先在配置文件中开启相机拍照功能")
def isValid(text):
"""
Returns True if input is related to the time.
Arguments:
text -- user-input, typically transcribed speech
"""
return any(word in text for word in ["拍照", "拍张照"])