forked from el-tocino/skill-camera
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__init__.py
213 lines (190 loc) · 7.87 KB
/
__init__.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
from mycroft.skills.core import MycroftSkill, intent_file_handler, Message
from mycroft.util.log import LOG
from mycroft.util import play_wav, play_mp3
from os.path import join
import time
from os.path import dirname, exists, expanduser
from os import makedirs
import json
try:
import cv2
from imutils.video import VideoStream
from shared_camera import Camera
import yagmail
except ImportError:
# re-install yourself
from py_msm import MycroftSkillsManager
msm = MycroftSkillsManager()
msm.install_by_url("https://github.com/JarbasAl/skill-camera", True)
# trigger reload
msm.reload_skill("skill-camera")
__author__ = 'jarbas'
class WebcamSkill(MycroftSkill):
def __init__(self):
self.create_settings_meta()
super(WebcamSkill, self).__init__()
platform = self.config_core.get("enclosure", {}) \
.get("platform", "unknown")
self.use_pi = platform.lower() in ["picroft", "mark_1", "mark1",
"raspbian", "raspberry", "pi"]
if "video_source" not in self.settings:
self.settings["video_source"] = 0
if "mail_picture" not in self.settings:
self.settings["mail_picture"] = False
if "play_sound" not in self.settings:
self.settings["play_sound"] = True
if "picture_path" not in self.settings:
self.settings["picture_path"] = expanduser("~/webcam")
if "camera_sound_path" not in self.settings:
self.settings["camera_sound_path"] = join(dirname(__file__),
"camera.wav")
if not exists(self.settings["picture_path"]):
makedirs(self.settings["picture_path"])
# private email
if yagmail is not None:
mail_config = self.config_core.get("email", {})
self.email = mail_config.get("email")
self.password = mail_config.get("password")
self.target_mail = mail_config.get("destinatary", self.email)
self.camera = None
self.last_timestamp = 0
def initialize(self):
LOG.info("initializing videostream")
def notify(text):
self.emitter.emit(Message(text))
self.camera = Camera(
VideoStream(src=self.settings["video_source"],
usePiCamera=self.use_pi), callback=notify)
self.last_timestamp = time.time()
def get_intro_message(self):
self.speak_dialog("priority")
def mail_picture(self, picture):
if self.settings["mail_picture"]:
title = "Mycroft Camera Skill"
body = ""
# try private sending
if yagmail is not None and self.email and self.password:
with yagmail.SMTP(self.email, self.password) as yag:
yag.send(self.target_email, title, [body, picture])
else:
self.speak_dialog("send.fail")
return False
self.speak_dialog("send")
return True
return False
@property
def last_frame(self):
self.last_timestamp = time.time()
if self.camera:
return self.camera.get().copy()
return None
def create_settings_meta(self):
meta = {
"name": "Camera Skill",
"skillMetadata": {
"sections": [
{
"name": "video_source",
"fields": [
{
"type": "label",
"label": "video source for webcam, usually 0"
},
{
"name": "video_source",
"type": "number",
"label": "video_source",
"placeholder": "0",
"value": "0"
}
]
},
{
"name": "camera_sound_path",
"fields": [
{
"type": "label",
"label": "path to wav or mp3 sound file to play on picture taken"
},
{
"name": "camera_sound_path",
"type": "text",
"label": "camera_sound_path",
"placeholder": join(dirname(__file__),
"camera.wav"),
"value": join(dirname(__file__),
"camera.wav")
},
{
"type": "checkbox",
"name": "play_sound",
"label": "play sound on taking picture",
"value": "true"
}
]
},
{
"name": "mail_picture",
"fields": [
{
"type": "label",
"label": "want to email taken pictures? check the repository for "
"additional instructions"
},
{
"type": "checkbox",
"name": "mail_picture",
"label": "mail_picture",
"value": "false"
}
]
},
{
"name": "picture_path",
"fields": [
{
"type": "label",
"label": "where to save taken pictures"
},
{
"name": "picture_path",
"type": "text",
"label": "picture_path",
"placeholder": expanduser("~/webcam"),
"value": expanduser("~/webcam")
}
]
}
]
}
}
meta_path = join(dirname(__file__), 'settingsmeta.json')
if not exists(meta_path):
with open(meta_path, 'w') as fp:
json.dump(meta, fp)
def initialize(self):
self.add_event("webcam.request", self.handle_get_picture)
@intent_file_handler("take_picture.intent")
def handle_take_picture(self, message):
if exists(self.settings["camera_sound_path"]) and \
self.settings["play_sound"]:
if ".wav" in self.settings["camera_sound_path"]:
play_wav(self.settings["camera_sound_path"])
elif ".mp3" in self.settings["camera_sound_path"]:
play_mp3(self.settings["camera_sound_path"])
pic_path = join(self.settings["picture_path"], time.asctime() +
".jpg")
cv2.imwrite(pic_path, self.last_frame)
self.mail_picture(pic_path)
self.speak_dialog("picture")
def handle_get_picture(self, message):
path = join(self.settings["picture_path"],
time.asctime() + ".jpg")
cv2.imwrite(path, self.last_frame)
self.emitter.emit(message.reply("webcam.picture", {"path": path}))
def shutdown(self):
self.camera.stop_stream()
self.camera.shutdown()
super(WebcamSkill, self).shutdown()
def create_skill():
return WebcamSkill()