Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env bash
- # Adjust line 52 to start with the same directory
- unsorted_dir="/media/username/Unsorted"
- mkdir -p "non_pictures/"
- mkdir -p "animated_gifs/"
- mkdir -p "damaged_jpgs/"
- tags_dir="tags"
- mkdir -p "$tags_dir"
- orig_path_dir="path"
- mkdir -p "$orig_path_dir"
- sorted_dir="Sorted"
- mkdir -p "$sorted_dir"
- # Remove non-images
- find "$unsorted_dir" -type f ! -name '*.jpg' ! -name '*.png' ! -name '*.gif' ! -name '*.jpeg' -exec mv -iv {} non_pictures/ \;
- # Remove animated gifs
- find "$unsorted_dir" -type f -name '*.gif' -exec identify -format '%n %i\n' -- {} \; | while read -r res ; do
- frame_cnt=$(echo -e "$res" | awk '{print $1}')
- if [ "$frame_cnt" != "1" ] ; then
- mv -iv $(echo -e "$res" | awk '{print $2}') animated_gifs/
- fi
- done
- # Remove damaged jpgs
- find "$unsorted_dir" -type f -name '*.jpg' -o -name '*.jpeg' | while read -r file ; do
- err_str=$(jpeginfo -c "$file")
- if echo -e "$err_str" | grep -qiE 'warning|error' ; then
- mv -iv "$file" damaged_jpgs/
- fi
- done
- # Estimate tags
- source /home/username/deepdanbooru/bin/activate
- deepdanbooru evaluate --project-path /home/username/deepdanbooru/upstream_model --allow-folder "$unsorted_dir" | tee combined_tags.txt
- # Split tags per image
- while read -r line ; do
- if echo "$line" | grep -qF 'Tags' ; then
- orig_path=$(echo $line | grep -oE '/media/username/.*' | tr -d ':')
- filename=$(basename "$orig_path")_tags.txt
- echo "$orig_path" > $orig_path_dir/$(basename "$orig_path")_path.txt
- fi
- echo "$line" | grep -vF 'Tags of ' | sed 's/([0-9]\.[0-9][0-9][0-9])//g' | tr -d ' ' >> "$tags_dir/$filename"
- done < combined_tags.txt
- # Move images according to char names
- find $tags_dir -type f | while read -r tags_file ; do
- echo -e "$sorted_dir" > /tmp/tags_sorted_path.txt
- while read -r tag ; do
- while read -r char_tag ;
- do
- if [[ "$char_tag" == "$tag" ]] ; then
- echo -e "$char_tag" >> /tmp/tags_sorted_path.txt
- fi
- done < "$tags_file"
- done < char_tags.txt
- echo ${tags_file::-9} >> /tmp/tags_sorted_path.txt
- # Form path with tags
- new_path="$(cat /tmp/tags_sorted_path.txt | awk 'NF' | tr '\n' '/')"
- old_path=$(cat "$orig_path_dir/$(basename ${tags_file::-9})_path.txt")
- # Create tags directories
- dirs=$(dirname "$new_path")
- mkdir -p "$dirs"
- mv -iv "$old_path" "${new_path::-1}"
- done
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement