-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrecognize_fish.bash
executable file
·260 lines (194 loc) · 4.73 KB
/
recognize_fish.bash
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
259
260
#!/usr/bin/env bash
set -euo pipefail
DEPENDENCIES=( basename curl file grep jq openssl seq wc )
print_help()
{
cat <<HELP
---- Fishial Recognition TM command line tool. ----
REQUIREMENTS
Following tools must be installed in order to run this program:
${DEPENDENCIES[*]}
USAGE
recognize_fish <options> <picture-file>
Note: this tool is not production grade. It lacks proper error checks, etc.
OPTIONS
-h, --help
Prints this message and exits.
-i, --identify
Identifies picture metadata and exits without performing fish
recognition.
-k, --key-id
Client key ID.
-s, --key-secret
Client key secret.
EXAMPLES
recognize_fish --help
recognize_fish -h
Prints this message.
recognize_fish --identify fishpic.jpg
recognize_fish -i fishpic.jpg
Prints metadata of fishpic.jpg.
recognize_fish --key-id=abc123 --key-secret=abcd1234 fishpic.jpg
recognize_fish -k abc123 -s abcd1234 fishpic.jpg
Requests recognition of fishes pictured on fishipic.jpg.
Client key "abc123" with secret "abcd1234" will be used.
HELP
}
#### HELPERS ####
err()
{
echo "$1"
echo "In order to learn more, run: recognize_fish --help"
exit 1
}
extract_from_json()
{
jq --raw-output "$1" <<<"$2"
}
#### ENTRY POINT ####
#### PARSE ARGUMENTS ####
while test $# -gt 0
do
case "$1" in
-h|--help)
print_help
exit 0
;;
-i|--identify)
ONLY_IDENTIFY=1
shift
;;
--key-id=*)
KEY_ID="${1#*=}"
shift
;;
-k)
KEY_ID="$2"
shift
shift
;;
--key-secret=*)
KEY_SECRET="${1#*=}"
shift
;;
-s)
KEY_SECRET="$2"
shift
shift
;;
*)
PICTURE="$1"
shift
;;
esac
done
#### CHECK ARGUMENTS ####
if ! [[ -f $PICTURE ]]; then
err "No picture file has been specified."
fi
#### CHECK DEPENDENCIES ####
for dep in ${DEPENDENCIES[*]}; do
if ! which -s "$dep"; then
err "Unsatisfied dependency: $dep"
fi
done
#### IDENTIFY METADATA ####
echo "Identifying picture metadata..."
NAME="$(basename -- "$PICTURE")"
MIME="$(file --mime-type -b "$PICTURE")"
SIZE="$(wc -c < "$PICTURE" | grep -oE '\d+')"
CSUM="$(openssl dgst -md5 -binary < "$PICTURE" | openssl enc -base64)"
echo
echo " file name: $NAME"
echo " MIME type: $MIME"
echo " byte size: $SIZE"
echo " checksum: $CSUM"
echo
#### CONDITIONAL EXIT ####
if ! [[ -z ${ONLY_IDENTIFY+x} ]]; then
exit 0
fi
#### OBTAIN TOKEN ####
echo "Obtaining auth token..."
D=$(cat <<JSON
{
"client_id": "$KEY_ID",
"client_secret": "$KEY_SECRET"
}
JSON
)
R=$(
curl --request POST \
--insecure \
--silent \
--url https://api-users.fishial.ai/v1/auth/token \
--header 'Content-Type: application/json' \
--data "$D"
)
AUTH_TOKEN=$(extract_from_json '.access_token' "$R")
AUTH="Authorization: Bearer $AUTH_TOKEN"
echo "Access token: $AUTH_TOKEN"
#### OBTAIN UPLOAD URL ####
echo "Obtaining upload url..."
D=$(
cat <<JSON
{
"blob": {
"filename": "$NAME",
"content_type": "$MIME",
"byte_size": $SIZE,
"checksum": "$CSUM"
}
}
JSON
)
R=$(
curl --request POST \
--insecure \
--silent \
--url https://api.fishial.ai/v1/recognition/upload \
--header "$AUTH" \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data "$D"
)
SIGNED_ID=$(extract_from_json '.["signed-id"]' "$R")
UPLOAD_URL=$(extract_from_json '.["direct-upload"]["url"]' "$R")
CONTENT_DISPOSITION=$(extract_from_json '.["direct-upload"]["headers"]["Content-Disposition"]' "$R")
#### UPLOAD FILE ####
echo "Uploading picture to the cloud..."
curl --request PUT \
--insecure \
--silent \
--url "$UPLOAD_URL" \
--header "Content-Disposition: $CONTENT_DISPOSITION" \
--header "Content-Md5: $CSUM" \
--header "Content-Type:" \
--data-binary "@${PICTURE}"
#### RUN RECOGNITION ####
echo "Requesting fish recognition..."
R=$(
curl --request GET \
--insecure \
--silent \
--url "https://api.fishial.ai/v1/recognition/image?q=$SIGNED_ID" \
--header "$AUTH"
)
FISH_COUNT=$(extract_from_json '.results | length' "$R")
#### PRINT RESULTS ####
echo
echo "Fishial Recognition found $FISH_COUNT fish(es) on the picture."
if [[ "$FISH_COUNT" -eq 0 ]]; then
exit 0
fi
for i in $(seq $FISH_COUNT); do
FISH_DATA=$(extract_from_json ".results[$(expr $i - 1)]" "$R")
echo
echo "Fish $i is:"
for j in $(seq $(extract_from_json '.species | length' "$FISH_DATA")); do
SPECIES_DATA=$(extract_from_json ".species[$(expr $j - 1)]" "$FISH_DATA")
SPECIES_NAME=$(extract_from_json ".name" "$SPECIES_DATA")
ACCURACY=$(extract_from_json ".accuracy" "$SPECIES_DATA")
echo " - $SPECIES_NAME [accuracy $ACCURACY]"
done
done