/
front_camera_notify.yaml
171 lines (163 loc) · 10.3 KB
/
front_camera_notify.yaml
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
sequence:
- service: notify.logfile
data:
message: "Checking Doods feedback"
- service: script.front_camera_interesting_things
- delay:
seconds: "{{ 0 if (states('input_number.front_camera_interesting_things')|int) > 0 else 3 }}"
- service_template: >-
{% if (states('input_number.front_camera_interesting_things')|int) == 0 %}
script.process_front_camera
{% else %}
script.nop
{% endif %}
- service_template: >-
{% if (states('input_number.front_camera_interesting_things')|int) == 0 %}
script.front_camera_interesting_things
{% else %}
script.nop
{% endif %}
- service: notify.logfile
data:
message: "Setting flags"
- service_template: >-
{%- if states('input_number.front_camera_interesting_things')|int > 0 -%}
input_boolean.turn_on
{%- else -%}
input_boolean.turn_off
{%- endif -%}
entity_id: input_boolean.front_camera_motion_interesting
- service_template: >-
{%- if states('input_number.front_camera_interesting_things')|int > 0 and is_state('input_boolean.stream_front_camera_motion','on') -%}
input_boolean.turn_on
{%- else -%}
input_boolean.turn_off
{%- endif -%}
entity_id: input_boolean.stream_camera
- service: notify.logfile
data:
message: "Notifying"
- service: notify.discord
data_template:
# Coordinates from Doods, which are from Tensorflow, are in the order of ymin, ymax, xmin, xmax
message: >-
{#- Set the scene - extract the things we care about, and prepare some defaults -#}
{%- set totalPeople = state_attr('image_processing.doods_front_camera_snapshot','summary').person|default(0) -%}
{%- set totalCars = state_attr('image_processing.doods_front_camera_snapshot','summary').car|default(0) -%}
{%- set totalTrucks = state_attr('image_processing.doods_front_camera_snapshot','summary').truck|default(0) -%}
{%- set totalMotorcycles = state_attr('image_processing.doods_front_camera_snapshot','summary').motorcycle|default(0) -%}
{%- set totalBicycles = state_attr('image_processing.doods_front_camera_snapshot','summary').bicycle|default(0) -%}
{%- set parkedCars = 0 -%}
{%- set otherCars = 0 -%}
{%- set parkedTrucks = 0 -%}
{%- set otherTrucks = 0 -%}
{%- set falseDetections = 0 -%}
{%- set relevantPeople = 0 -%}
{%- set otherPeople = 0 -%}
{%- macro isParked(box) -%}
{{- True if (
((0.700 < box[0] < 0.755) and (0.340 < box[1] < 0.508) and (0.800 < box[2] < 0.862) and (0.490 < box[3] < 0.580))
or
((0.740 < box[0] < 0.775) and (0.317 < box[1] < 0.400) and (0.830 < box[2] < 0.854) and (0.380 < box[3] < 0.550))
) else X -}}
{%- endmacro -%}
{%- macro isFalseDetection(box) -%}
{{- True if (
((box[3]-box[1] > 0.6) and (box[2]-box[0] > 0.6))
and
((0.48 < box[0] < 0.52) and (0.23 < box[1] < 0.32) and (0.80 < box[2] < 0.84) and (0.57 < box[3] < 0.59))
) else X -}}
{%- endmacro -%}
{#- Split out parked cars from the rest - don't care about the parked ones -#}
{%- if totalCars > 0 -%}
{%- macro getParkedCars() -%}
{%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').car -%}{{- "X" if isParked(state.box) -}}{%- endfor -%}
{%- endmacro -%}
{%- set parkedCars = getParkedCars()|length -%}
{%- set otherCars = state_attr('image_processing.doods_front_camera_snapshot','summary').car - parkedCars -%}
{%- endif -%}
{%- if totalTrucks > 0 -%}
{%- macro getParkedTrucks() -%}
{%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').truck -%}{{- "X" if isParked(state.box) -}}{%- endfor -%}
{%- endmacro -%}
{%- set parkedTrucks = getParkedTrucks()|length -%}
{%- set otherTrucks = state_attr('image_processing.doods_front_camera_snapshot','summary').truck - parkedTrucks -%}
{%- endif -%}
{%- set parkedVehicles = parkedCars+parkedTrucks -%}
{%- if parkedVehicles > 0 -%}
{%- macro getFalseDetections() -%}
{%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').truck -%}{{- "X" if isFalseDetection(state.box) -}}{%- endfor -%}
{%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').car -%}{{- "X" if isFalseDetection(state.box) -}}{%- endfor -%}
{%- endmacro -%}
{%- set falseDetections = getFalseDetections()|length -%}
{%- endif -%}
{%- set otherVehicles = otherCars+otherTrucks-falseDetections -%}
{#- Split out people on the path/in the garden, from those on the road -#}
{%- if totalPeople > 0 -%}
{%- macro personOnPath(box) -%}
{{- True if (
( 0.885 < box[2] )
) else X -}}
{%- endmacro -%}
{%- macro getPeopleOnPath() -%}
{%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').person -%}{{- "X" if personOnPath(state.box) -}}{%- endfor -%}
{%- endmacro -%}
{%- set relevantPeople = getPeopleOnPath()|length -%}
{%- set otherPeople = state_attr('image_processing.doods_front_camera_snapshot','summary').person - relevantPeople -%}
{%- endif -%}
{#- total up the things into different categories -#}
{%- set importantThings = otherCars + otherTrucks + totalMotorcycles + totalBicycles + relevantPeople -%}
{%- set otherThings = parkedCars + parkedTrucks + otherPeople -%}
{#- Now produce the message -#}
There was motion at the front at {{ as_timestamp(states.input_boolean.front_camera_motion.last_updated)|timestamp_custom('%F %T', True) }}.
{%- if importantThings > 0 %}
There were {{ importantThings }} things that matter detected, these were:
{%- if relevantPeople > 0 %}
{{ relevantPeople }} people on the path ({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').person %}{% if personOnPath(state.box) %}{{ state.score|round(0) }}% confidence at {{state.box}}{%- if loop.last %}{% else %}, {% endif %}{% endif %}{% endfor %})
{% endif %}
{%- if otherCars > 0 %}
{{otherCars}} cars
({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').car %}{% if not isParked(state.box) %}{{ state.score|round(0) }}% confidence at {{state.box}}, {{ (state.box[3]-state.box[1])|round(4) }} wide by {{ (state.box[2]-state.box[0])|round(4) }} high, {{ ((state.box[3]-state.box[1])|round(4) * (state.box[2]-state.box[0])|round(4))|round(4) }} area){%- if loop.last %}{%else %}, {% endif %}{% endif %}{% endfor %}
{% endif %}
{%- if otherTrucks > 0 %}
{{otherTrucks}} trucks
({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').truck %}{% if not isParked(state.box) %}{{ state.score|round(0) }}% confidence at {{state.box}}, {{ (state.box[3]-state.box[1])|round(4) }} wide by {{ (state.box[2]-state.box[0])|round(4) }} high, {{ ((state.box[3]-state.box[1])|round(4) * (state.box[2]-state.box[0])|round(4))|round(4) }} area){%- if loop.last %}{% else %}, {% endif %}{% endif %}{% endfor %}
{% endif %}
{%- if totalMotorcycles > 0 %}
{{ state_attr('image_processing.doods_front_camera_snapshot','summary').motorcycle }} motorcycle(s) ({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').motorcycle %} {{ state.score|round(0) }}% confidence at {{state.box}}{%- if loop.last %}{% else %}, {%endif %}{% endfor %})
{% endif %}
{%- if totalBicycles > 0 %}
{{ state_attr('image_processing.doods_front_camera_snapshot','summary').bicycle }} bicycle(s) ({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').bicycle %}{{ state.score|round(0) }}% confidence at {{state.box}}{%- if loop.last %}{% else %}, {% endif %}{% endfor %})
{% endif %}
{% else %}
We saw nothing of importance.
{% endif -%}
{%- if otherThings > 0 %}
There were {{ otherThings }} things that probably don't matter detected, these were:
{%- if parkedVehicles > 0 %}
{{parkedVehicles}} parked vehicles
{%- if parkedCars > 0 %}
({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').car %}{% if isParked(state.box) %}car {{ state.score|round(0) }}% confidence at {{state.box}}, {{ (state.box[3]-state.box[1])|round(4) }} wide by {{ (state.box[2]-state.box[0])|round(4) }} high, {{ ((state.box[3]-state.box[1])|round(4) * (state.box[2]-state.box[0])|round(4))|round(4) }} area){%- if loop.last %}{% else %}, {% endif %}{% endif %}{% endfor %}
{%- endif -%}
{%- if parkedTrucks > 0 %}
({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').truck %}{% if isParked(state.box) %}truck {{ state.score|round(0) }}% confidence at {{state.box}}, {{ (state.box[3]-state.box[1])|round(4) }} wide by {{ (state.box[2]-state.box[0])|round(4) }} high, {{ ((state.box[3]-state.box[1])|round(4) * (state.box[2]-state.box[0])|round(4))|round(4) }} area){%- if loop.last %}{% else %}, {% endif %}{% endif %}{% endfor %}
{%- endif -%}
{%- endif -%}
{%- if otherPeople > 0 %}
{{otherPeople}} people on the road
({%- for state in state_attr('image_processing.doods_front_camera_snapshot','matches').person %}{% if not personOnPath(state.box) %}{{ state.score|round(0) }}% confidence at {{state.box}}){%- if loop.last %}{% else %}, {% endif %}{% endif %}{% endfor %}
{%- endif -%}
{%- endif -%}
target: >-
{% if (states('input_number.front_camera_interesting_things')|int) > 0 %}
!secret discord_camera_channel
{% else %}
!secret discord_camera_false_channel
{% endif %}
data:
images:
- "/data/homeassistant/cameras/front_camera_snapshot.jpg"
- "/data/homeassistant/cameras/front_camera_snapshot_latest.jpg"
- service: notify.logfile
data:
message: "Finished front camera notifications"