forked from ShiqiYu/libfacedetection
-
Notifications
You must be signed in to change notification settings - Fork 1
/
faceanno.sh
executable file
·70 lines (59 loc) · 1.91 KB
/
faceanno.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
#!/bin/bash
face_annotate()
{
local json=${1}
local jpeg=${2}
local colors=(blue red white yellow green orange magenta cyan)
local result
if [ -s "${json}" ] && [ -s "${jpeg}" ]; then
local length=$(jq '.results|length' ${json})
faces=
if [ ${length:0} -gt 0 ]; then
local i=0
local count=0
while [ ${i} -lt ${length} ]; do
local face=$(jq '.results['${i}']' ${json})
local left=$(echo "${face:-null}" | jq -r '.x')
local top=$(echo "${face:-null}" | jq -r '.y')
local width=$(echo "${face:-null}" | jq -r '.width')
local height=$(echo "${face:-null}" | jq -r '.height')
local bottom=$((top+height))
local right=$((left+width))
local confidence=$(echo "${face:-null}" | jq -r '.confidence')
if [ ${i} -eq 0 ]; then
file=${jpeg%%.*}-${i}.jpg
cp -f ${jpeg} ${file}
else
rm -f ${file}
file=${output}
fi
output=${jpeg%%.*}-$((i+1)).jpg
convert -pointsize 16 -stroke ${colors[${count}]} -fill none -strokewidth 2 -draw "rectangle ${left},${top} ${right},${bottom} push graphic-context stroke ${colors[${count}]} fill ${colors[${count}]} translate ${right},${bottom} text 3,6 '${confidence}' pop graphic-context" ${file} ${output}
if [ ! -s "${output}" ]; then
echo "Failed"
exit 1
fi
i=$((i+1))
count=$((count+1))
if [ ${count} -ge ${#colors[@]} ]; then count=0; fi
done
if [ ! -z "${output:-}" ]; then
rm -f ${file}
result=${jpeg%%.*}-face.jpg
mv ${output} ${result}
fi
fi
fi
echo "${result:-null}"
}
process_face_results()
{
local json=${1}.json
local jpeg=${1}.jpg
echo $(face_annotate ${json} ${jpeg})
}
if [ ! -z "${*}" ]; then
process_face_results ${*}
else
echo "Usage: ${0} <sample>; for example: ${0} ea7the" &> /dev/stderr
fi