-
Notifications
You must be signed in to change notification settings - Fork 3
/
darknetnx.sh
executable file
·258 lines (228 loc) · 6.9 KB
/
darknetnx.sh
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
#!/bin/bash
#july 2020 blackbox usa
SOURCE="darknet"
CAMID=$1
SERVER=$2
USER=$3
PASS=$4
CREDS="$3:$4"
MODEL="$5"
NAME="$6"
RUNTIME="$7"
THRESHOLD="$8"
TGTOKEN="$9"
TGID="${10}"
FILE="cameraThumbnail?cameraId=$CAMID"
THUMB=http://$CREDS@$SERVER/ec2/$FILE
DIR=/tmp/$CAMID
TIMESTAMP=`date -d "now" +%Y-%m-%dT%H:%M:%S`
loop (){
x="$7"
y=0
until [ $y = $RUNTIME ]; do
mkdir -p /tmp/$CAMID/
#get the screenshot of the camera from dw spectrum
wget --no-check-certificate "$THUMB"
mv $FILE /tmp/$CAMID/FILE
#interest region cropping
#convert -crop +0+200 FILE FILE
#running the deep learning function
#dev line for notification it is running
#curl "http://$CREDS@$SERVER/api/createEvent?source=$SOURCE&caption=Executed&description=$NAME%20using%20$MODEL&metadata=%7B%22cameraRefs%22%3A%5B%22$CAMID%22%5D%7D"
#replace this with model eventually
echo "Running $MODEL"
$MODEL
#getting the results from the deep learning
echo
results
y=$(($y+1))
#close the loop
echo "Graceful shutdown"
sleep 3
done
}
yolov3-tiny_xnor (){
cd /root/darknetnx
#run darknet classifier yolov3-tiny
echo "`date` on $CAMID"
./darknet detect cfg/yolov3-tiny_xnor.cfg yolov3-tiny_xnor.weights $DIR/FILE > $DIR/results.txt
}
yolov3-tiny-prn (){
cd /root/darknetnx
#run darknet classifier yolov3-tiny
echo "`date` on $CAMID"
./darknet detect cfg/yolov3-tiny-prn.cfg yolov3-tiny-prn.weights $DIR/FILE > $DIR/results.txt
}
darknet-yolov3-tiny (){
cd /root/darknetnx
#run darknet classifier yolov3-tiny
echo "`date` on $CAMID"
./darknet detect cfg/yolov3-tiny.cfg yolov3-tiny.weights $DIR/FILE > $DIR/results.txt
}
darknet-yolov3 (){
#run darknet classifier yolov3
echo "`date` on $CAMID"
./darknet detect cfg/yolov3.cfg yolov3.weights $DIR/FILE > $DIR/results.txt
}
darknet-yolov4 (){
#run darknet classifier yolov3
echo "`date` on $CAMID"
./alaxeyab/darknet detect cfg/yolov4.cfg yolov4.weights $DIR/FILE -ext_output $DIR/FILE.OUT $DIR/results.txt > $DIR/results.txt
}
darknet-yolov4-tiny (){
#run darknet classifier yolov3
echo "`date` on $CAMID"
./alaxeyab/darknet detect cfg/yolov4-tiny.cfg yolov4-tiny.weights $DIR/FILE -ext_output $DIR/FILE.OUT $DIR/results.txt > $DIR/results.txt
}
machinebox (){
#run machinebox image classifier
echo "Launching AI with the model: $STATE"
#upload saved state file
curl -X POST -F file=@states/$STATE $MACHINEBOX/tagbox/state 2>/dev/null
#pass the image to the classifier
curl -X POST -F 'file=@FILE' $MACHINEBOX/tagbox/check > results.txt 2>/dev/null
}
sendnx (){
echo "sending nx"
NUMBER=`cat /$DIR/results.txt | grep 'car\|person\|truck\|vehicle\|cat\|dog' | head -n1 | awk '{ print $2 }' | tr -d '%'`
echo "Send generic event to DW Server at $SERVER"
#send generic event to dw spectrum
echo "$NUMBER"
curl -k "http://$CREDS@$SERVER/api/createEvent?timestamp=$TIMESTAMP&source=$SOURCE-$NAME&caption=$RESULT%20$NUMBER%25&description=$NAME%20using%20$MODEL&metadata=%7B%22cameraRefs%22%3A%5B%22$CAMID%22%5D%7D"
#send notification via nxevents and pushover with a thumbnail
#echo "Send pushover via nxevents"
#curl --silent "$NXEVENT/hooks/quickmark?camid=$CAMID&server=720fd27e-04cc-4c1a-a2cb-990bd167e9c3.relay.vmsproxy.com&user=ai&pass=ai123456&pushovertoken=armxjs7pxawfkiczctemozi8iq8rkp&pushoveruser=uxon9seqnxonzkm4jb5rzmw1t3ynvm&desc=$RESULT"
#CLEANUP
rm $DIR/FILE
rm $DIR/FILE
rm $DIR/results.txt
#CLEANUP FINISHED
}
sendtelegram (){
#send to telegram
echo "sending to telegram"
echo "$TGTOKEN"
echo "$TGID"
./telegram -t $TGTOKEN -c $TGID -i /root/darknetnx/predictions.jpg "$RESULT $CONF detected on $NAME"
}
sendpushover (){
echo "sending pushover"
#send to pushover
curl -s \
--form-string "token=$PUSHOVERTOKEN" \
--form-string "user=$PUSHOVERUSER" \
--form-string "title=$NAME" \
--form-string "url=$OPENAPP" \
--form-string "message=$RESULT $CONF detected by blackboxusa.com" \
-F "attachment=@/root/darknetnx/predictions.jpg" \
https://api.pushover.net/1/messages.json
#CLEANUP
rm $DIR/FILE
rm $DIR/FILE
#rm $DIR/results.txt
#CLEANUP FINISHED
}
results (){
#show results
cat $DIR/results.txt
#search for Person or Vehicle on the results
if grep --ignore-case -q 'car' $DIR/results.txt
then
RESULT="Vehicle"
data
elif grep --ignore-case -q 'person' $DIR/results.txt
then
RESULT="Person"
echo "PERSON FOUND HERE"
data
elif grep --ignore-case -q 'truck' $DIR/results.txt
then
RESULT="Vehicle"
data
elif grep --ignore-case -q 'cat' $DIR/results.txt
then
RESULT="Animal"
data
elif grep --ignore-case -q 'vehicle' $DIR/results.txt
then
RESULT="Vehicle"
data
elif grep --ignore-case -q 'dog' $DIR/results.txt
then
RESULT="Animal"
data
elif grep --ignore-case -q 'animal' $DIR/results.txt
then
RESULT="Animal"
data
else
echo "No objects of interest detected"
RESULT="None"
fi
}
data (){
#get the coordinates
CONF=`cat /$DIR/results.txt | grep 'car\|person\|truck\|vehicle\|cat\|dog' | head -n1 | awk '{ print $2 }'`
PERCENT=`cat /$DIR/results.txt | grep 'car\|person\|truck\|vehicle\|cat\|dog' | head -n1 | awk '{ print $2 }' | tr -d '%'`
LEFT=`cat /$DIR/results.txt | grep 'car\|person\|truck\|vehicle\|cat\|dog' | head -n1 | awk '{ print $4 }'`
#TOP=`cat /$DIR/results.txt | head -n1 | grep 'car\|person\|vehicle' | awk '{ print $6 }'`
#WIDTH=`cat /$DIR/results.txt | head -n1 | grep 'car\|person\|vehicle' | awk '{ print $8 }'`
#HEIGHT=`cat /$DIR/results.txt | head -n1 | grep 'car\|person\|vehicle' | awk '{ print $10 }'`
#move previous results to old
mv $DIR/$RESULT-coords.txt $DIR/$RESULT-coordsold.txt
#mv $DIR/topcoords.txt $DIR/topcoordsold.txt
#create new result
#left variable pixel
echo $LEFT > $DIR/$RESULT-coords.txt
OLDCOORD=`cat $DIR/$RESULT-coordsold.txt`
NEWCOORD=`cat $DIR/$RESULT-coords.txt`
#sort the variables by size for easy subtraction
VAL1=`echo "$OLDCOORD $NEWCOORD" | tr " " "\n" | sort -rnu | head -n1`
VAL2=`echo "$OLDCOORD $NEWCOORD" | tr " " "\n" | sort -rnu | tail -n1`
#subtract the coordinates for a difference number
if test -f "$DIR/$RESULT-coordsold.txt"; then
DIFF=$(($VAL1 - $VAL2))
else
DIFF=$((200 - 100))
fi
echo "OBJECT: $RESULT"
echo "CONFIDENCE: $CONF"
echo "LEFT DIFFERENCE: $DIFF"
#confidence without percent to use as integer
CLEVEL=`echo $CONF | tr -d % | bc`
if [[ $CLEVEL -gt $THRESHOLD ]];
then
echo "$CLEVEL is greater than $THRESHOLD"
#if the difference between the object is greater than 50 pixels report as new object
if [[ "$RESULT" == *"Vehicle"* ]] && [ $DIFF -gt 50 ]
then
echo "New vehicle object event"
echo "using vehicle rules"
sendnx
sendtelegram
#sendpushover
#if the result is person and the pixel difference is 10
elif [[ "$RESULT" = *"Person"* ]] && [ $DIFF -gt 30 ]
then
echo "New person object event"
sendnx
#sendpushover
elif [[ "$RESULT" == *"Animal"* ]] && [ $DIFF -gt 30 ]
then
echo "New animal object event"
sendnx
#sendpushover
else
echo "Object: $RESULT"
echo "Similar object detected not reporting or low confidence"
fi
fi
#end the if statement
}
echo
echo
echo "Running AI with $MODEL on $CAMID"
#how many times are we running the loop after motion start in nx (3 times)
echo "STARTING NOW"
echo "Running the loop $RUNTIME times"
loop