Advertisement
Guest User

Untitled

a guest
Jan 4th, 2022
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.48 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3.  
  4.  
  5. # Adjust line 52 to start with the same directory
  6. unsorted_dir="/media/username/Unsorted"
  7.  
  8.  
  9. mkdir -p "non_pictures/"
  10. mkdir -p "animated_gifs/"
  11. mkdir -p "damaged_jpgs/"
  12.  
  13.  
  14. tags_dir="tags"
  15. mkdir -p "$tags_dir"
  16.  
  17. orig_path_dir="path"
  18. mkdir -p "$orig_path_dir"
  19.  
  20. sorted_dir="Sorted"
  21. mkdir -p "$sorted_dir"
  22.  
  23.  
  24.  
  25. # Remove non-images
  26. find "$unsorted_dir" -type f ! -name '*.jpg' ! -name '*.png' ! -name '*.gif' ! -name '*.jpeg' -exec mv -iv {} non_pictures/ \;
  27.  
  28. # Remove animated gifs
  29. find "$unsorted_dir" -type f -name '*.gif' -exec identify -format '%n %i\n' -- {} \; | while read -r res ; do
  30.     frame_cnt=$(echo -e "$res" | awk '{print $1}')
  31.     if [ "$frame_cnt" != "1" ] ; then
  32.         mv -iv $(echo -e "$res" | awk '{print $2}') animated_gifs/
  33.     fi
  34. done
  35.  
  36. # Remove damaged jpgs
  37. find "$unsorted_dir" -type f -name '*.jpg' -o -name '*.jpeg' | while read -r file ; do
  38.     err_str=$(jpeginfo -c "$file")
  39.     if echo -e "$err_str" | grep -qiE 'warning|error' ; then
  40.         mv -iv "$file" damaged_jpgs/
  41.     fi
  42. done
  43.  
  44. # Estimate tags
  45. source /home/username/deepdanbooru/bin/activate
  46. deepdanbooru evaluate --project-path /home/username/deepdanbooru/upstream_model --allow-folder "$unsorted_dir" | tee combined_tags.txt
  47.  
  48.  
  49. # Split tags per image
  50. while read -r line ; do
  51.     if echo "$line" | grep -qF 'Tags' ; then
  52.         orig_path=$(echo $line | grep -oE '/media/username/.*' | tr -d ':')
  53.         filename=$(basename "$orig_path")_tags.txt
  54.         echo "$orig_path" > $orig_path_dir/$(basename "$orig_path")_path.txt
  55.     fi
  56.     echo "$line" | grep -vF 'Tags of ' | sed 's/([0-9]\.[0-9][0-9][0-9])//g' | tr -d ' ' >> "$tags_dir/$filename"
  57. done < combined_tags.txt
  58.  
  59.  
  60. # Move images according to char names
  61. find $tags_dir -type f | while read -r tags_file ; do
  62.  
  63.     echo -e "$sorted_dir" > /tmp/tags_sorted_path.txt
  64.     while read -r tag ; do
  65.         while read -r char_tag ;
  66.         do
  67.             if [[ "$char_tag" == "$tag" ]] ; then
  68.                 echo -e "$char_tag" >> /tmp/tags_sorted_path.txt
  69.             fi
  70.         done < "$tags_file"
  71.     done < char_tags.txt
  72.    
  73.     echo ${tags_file::-9} >> /tmp/tags_sorted_path.txt
  74.  
  75.     # Form path with tags
  76.     new_path="$(cat /tmp/tags_sorted_path.txt | awk 'NF' | tr '\n' '/')"
  77.     old_path=$(cat "$orig_path_dir/$(basename ${tags_file::-9})_path.txt")
  78.    
  79.     # Create tags directories
  80.     dirs=$(dirname "$new_path")
  81.     mkdir -p "$dirs"
  82.    
  83.     mv -iv "$old_path" "${new_path::-1}"
  84. done
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement