-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorise_focus.sh
174 lines (128 loc) · 3.71 KB
/
categorise_focus.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
# !/bin/bash -ue
# Categorise photos by focus quality
#
PROG=$0
set -ue
usage() {
cat <<-EOF
USAGE: categorise [-v][--verbose] [-tSEP] [--field-separator=SEP] [-k key][--key key][-r][--rename] best [worst] < scorefile > command-file
-h or --help emits this help text
-v or --verbose does the obvious thing
-k key or --key key selects which key (column) is used to decide best/worst defaults to 2 (whole image from checkfocus)
-r or --rename causes the output commands to rename the image file to prefix the chosen score (key) to filename, allows easy review in order
-t or --field-separator, needs to match the -t option of the generating tool, to cope with filenames containing spaces
<best> and <worst> are scores.
Images with values above <best> are categorised as good
Images with values below <worst> are categorised as bad
This script generates a set of shell commands to move the good images into BEST directory and bad images into WORST directory
normally you would redirect the output into a file. Edit it if required, then execute it:
find . -mtime -1 > filelist
checkfocus -f filelist > scorefile
${PROG} 7300000 100000 < scorefile > command-file
...possibly edit the command-file...
bash ./command-file
EOF
}
typeset -i verbose
typeset -i best
typeset -i worst
typeset -i keycolumn
typeset -i keyelement
typeset -i n
typeset -i col
typeset -i lineno
typeset -i help
typeset -a line
PROG=$0
verbose=0
rename=0
keycolumn=2
help=0
: ${best:=0}
: ${worst:=0}
sep=""
TEMP=`getopt -o hvrk:t: --long verbose,rename,key:,field-separator: -n ${PROG} -- "$@"`
if [ $? != 0 ] ; then echo "Getopt failed, terminating..." >&2 ; exit 1 ; fi
# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
case "$1" in
-h|--help) help=1 ; shift ;;
-v|--verbose) verbose=1 ; shift ;;
-r|--rename) rename=1 ; shift ;;
-k|--key) keycolumn="$2" ; shift 2 ;;
-t|--field-separator) sep="$2" ; shift 2 ;;
--) shift ; break ;;
*) echo "Internal error!" ; exit 1 ;;
esac
done
if [[ ${help} -ne 0 ]] ; then
usage
exit 1
fi
if [ $# -lt 1 -o $# -gt 2 ] ; then
usage
exit 1
fi
best=$1
if [ $# -gt 1 ] ; then
worst=$2
fi
if [ ${verbose} -eq 1 ] ; then
echo "verbose selected" >&2
echo "key=${keycolumn}" >&2
echo "best=${best}" >&2
echo "worst=${0}" >&2
fi
if [[ ${keycolumn} -lt 2 ]] ; then
echo "Error key must be 2 or greater (column 1 is the filename)" >&2
exit 1
fi
keyelement=keycolumn-1
if [[ ${best} -ne 0 ]] ; then
echo "mkdir -p BEST"
fi
if [[ ${worst} -ne 0 ]] ; then
echo "mkdir -p WORST"
fi
lineno=0
if [[ "${sep}" != "" ]] ; then
OLDIFS="${IFS}"
IFS="${sep}"
fi
while read -a line
do
lineno+=1
if [[ verbose -ne 0 ]] ; then
n=${#line[*]}
echo -n "${n} items on line ${lineno} " >&2
echo -n "Filename: ${line[0]} " >&2
echo -n "Fields: " >&2
echo -n "IFS: ${IFS}" >&2
for ((i=1; i<n; ++i))
do
col=i+1
echo -n "k[${col}]=${line[i]}," >&2 # sort is 1 based, arrrays are 0 based
done
echo >&2
fi
if [[ ${best} -ne 0 ]] ; then
if [[ line[keyelement] -gt best ]] ; then
if [[ ${rename} -eq 0 ]] ;then
echo "mv ${line[0]} BEST"
else
echo "mv ${line[0]} BEST/${line[keyelement]}-${line[0]}"
fi
fi
fi
if [[ ${worst} -ne 0 ]] ; then
if [[ line[keyelement] -lt worst ]] ; then
if [[ ${rename} -eq 0 ]] ;then
echo "mv ${line[0]} WORST"
else
echo "mv ${line[0]} WORST/${line[keyelement]}-${line[0]}"
fi
fi
fi
done
exit