-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_lovot_status.py
executable file
·211 lines (180 loc) · 7.94 KB
/
check_lovot_status.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import rospkg
import rospy
import cv2
import numpy as np
import copy
from datetime import datetime
from copy import deepcopy
from send_mail import SEND_MAIL
from yaml_util import load_yaminfo
from sensor_msgs.msg import Image
from cv_bridge import CvBridge
rpack = rospkg.RosPack()
PACKAGE_PATH = rpack.get_path('lovot_monitor')
LAMP_PATH = "{}/images/lamp.png".format(PACKAGE_PATH)
HORN_PATH = "{}/images/horn.png".format(PACKAGE_PATH)
NEST_PATH = "{}/images/nest.png".format(PACKAGE_PATH)
SAVE_PATH = "{}/images/view.png".format(PACKAGE_PATH)
YAML_PATH = "{}/info.yaml".format(PACKAGE_PATH)
class CheckStatus:
def __init__(self):
self.bridge = CvBridge()
self.img = None
self.nest_img = cv2.imread(NEST_PATH)
self.lamp_img = cv2.imread(LAMP_PATH)
self.horn_img = cv2.imread(HORN_PATH)
self.TH = 0.7
self.status_lst = [] # 0: 充電中でない, 1: 充電中
self.info = (datetime.now().hour, datetime.now().minute, -1) # (statusが変わった時間, status)
self.send_flag = False
self.search = 0 # -1: 夜の充電中, 0: カメラとネストの間に障害物がある, 1: ネストが認識できている
self.debug_mail_flag = -1
self.get_up_time = rospy.get_param('~start_time', 11)
self.go_bed_time = rospy.get_param('~end_time', 22)
self.debug_view = rospy.get_param('~debug_view', False)
yaml_path = rospy.get_param('~yaml_path', YAML_PATH)
from_addr, to_addr_p, to_addr_o, password = load_yaminfo(yaml_path)
self.sender = SEND_MAIL(from_addr, password, to_addr_p, to_addr_o)
rospy.Subscriber("/usb_cam/image_raw", Image, self.img_cb, queue_size=1)
def search_range(self, input_img):
H, W, _C = self.nest_img.shape
max_loc = -1
res = cv2.matchTemplate(input_img, self.nest_img, cv2.TM_CCOEFF_NORMED)
_min_val, max_val, _min_loc, max_loc = cv2.minMaxLoc(res)
if max_val < self.TH:
self.search = 0
else:
self.search = 1
top_left = max_loc
monitor_lt = (top_left[0] + 5, top_left[1] + H - 10)
monitor_rb = (monitor_lt[0] + 65, monitor_lt[1] + 60)
monitor_img = self.img[monitor_lt[1]: monitor_rb[1], monitor_lt[0]: monitor_rb[0], :]
panel_lt = (top_left[0] - 90, top_left[1] - 20)
panel_rb = (panel_lt[0] + 100, panel_lt[1] + 60)
panel_img = self.img[panel_lt[1]: panel_rb[1], panel_lt[0]: panel_rb[0], :]
if self.debug_view:
output_img = deepcopy(self.img)
bottom_right = (top_left[0] + W, top_left[1] + H)
cv2.rectangle(output_img, top_left, bottom_right, (255, 0, 0), thickness=2, lineType=cv2.LINE_4)
cv2.rectangle(output_img, monitor_lt, monitor_rb, (0, 255, 0), thickness=2, lineType=cv2.LINE_4)
cv2.rectangle(output_img, panel_lt, panel_rb, (0, 0, 255), thickness=2, lineType=cv2.LINE_4)
cv2.imwrite("debug.png", output_img)
if self.search == 1:
horn_status, lamp_status = self.check_status(monitor_img, panel_img)
else:
return -1, -1
return horn_status, lamp_status
def check_status(self, monitor_img, panel_img):
"""
return interger, ineteger (horn_status, lamp_status)
-1: None
0: False
1: True
"""
h1, w1, _ = panel_img.shape
h2, w2, _ = self.horn_img.shape
if h1 >= h2 and w1 >= w2:
res1 = cv2.matchTemplate(panel_img, self.horn_img, cv2.TM_CCOEFF_NORMED)
_min_val, max_val_1, _min_loc, max_loc_1 = cv2.minMaxLoc(res1)
if max_val_1 > self.TH:
horn_status = 1
else:
horn_status = 0
else:
return -1, -1
h1, w1, _ = monitor_img.shape
h2, w2, _ = self.lamp_img.shape
if h1 >= h2 and w1 >= w2:
res2 = cv2.matchTemplate(monitor_img, self.lamp_img, cv2.TM_CCOEFF_NORMED)
_min_val, max_val_2, _min_loc, max_loc_2 = cv2.minMaxLoc(res2)
if max_val_2 > self.TH:
lamp_status = 1
else:
lamp_status = 0
else:
return -1, -1
# print(max_val_1, max_val_2)
if self.debug_view:
H, W, _C = self.horn_img.shape
top_left = max_loc_1
bottom_right = (top_left[0] + W, top_left[1] + H)
cv2.rectangle(panel_img, top_left, bottom_right, (255, 0, 0), thickness=2, lineType=cv2.LINE_4)
cv2.imwrite("debug_horn.png", panel_img)
H, W, _C = self.lamp_img.shape
top_left = max_loc_2
bottom_right = (top_left[0] + W, top_left[1] + H)
cv2.rectangle(monitor_img, top_left, bottom_right, (255, 0, 0), thickness=2, lineType=cv2.LINE_4)
cv2.imwrite("debug_lamp.png", monitor_img)
return horn_status, lamp_status
def calc(self, cur_hour, cur_minute, prev_hour, prev_minute):
if cur_minute < prev_minute:
cur_hour -= 1
cur_minute += 60
diff = (cur_minute - prev_minute) + (cur_hour - prev_hour) * 60
# print(cur_hour, cur_minute, prev_hour, prev_minute, diff)
return diff
def img_cb(self, msg):
"""
(horn_status, lamp_status) = (1, 1) : LOVOTが充電中
else : LOVOTは充電中でない
"""
cur_time = datetime.now()
cur_hour = cur_time.hour
cur_minute = cur_time.minute
self.img = self.bridge.imgmsg_to_cv2(msg, desired_encoding="bgr8")
if self.search == -1 and cur_hour == self.get_up_time:
self.search = 0
cv2.imwrite(SAVE_PATH, self.img)
self.sender.send_mail_main(2, SAVE_PATH)
if cur_hour == self.go_bed_time and cur_minute == 30:
_hour, _minute, status_ = self.info
if status_ != 1:
self.info = (cur_hour, cur_minute, 1)
self.search = -1
# デバッグ用
if 11 <= cur_hour <= 22:
if (cur_minute == 0 or cur_minute == 30) and self.debug_mail_flag != cur_minute:
cv2.imwrite(SAVE_PATH, self.img)
self.sender.send_mail_debug_staus(self.info, SAVE_PATH)
self.debug_mail_flag = cur_minute
if self.search >= 0:
horn_status, lamp_status = self.search_range(self.img)
if horn_status == -1 or lamp_status == -1:
status = -1
elif horn_status == 1 and lamp_status == 1:
status = 1
else:
status = 0
"""
-1: 判定不可
0: LOVOTが充電中でない
1: LOVOTが充電中
"""
if status != -1:
self.status_lst.append(status)
if len(self.status_lst) == 30:
state_len = len(set(self.status_lst))
if state_len == 1: # ノイズがない情報
keep_hour, keep_minute, keep_status = self.info
if keep_status != status:
self.info = (cur_hour, cur_minute, status)
self.send_flag = False
if keep_status == 0 and status == 1:
cv2.imwrite(SAVE_PATH, self.img)
self.sender.send_mail_debug(SAVE_PATH)
elif keep_status == 0 and status == 0 and not self.send_flag:
if self.calc(cur_hour, cur_minute, keep_hour, keep_minute) >= 75: # 75分以上充電されていない
cv2.imwrite(SAVE_PATH, self.img)
if horn_status:
self.sender.send_mail_main(1, SAVE_PATH)
else:
self.sender.send_mail_main(0, SAVE_PATH)
self.send_flag = True
self.status_lst = []
# print(self.info)
if __name__ == '__main__':
rospy.init_node("LOVOT")
check = CheckStatus()
rospy.spin()