Skip to content

Commit

Permalink
同名ファイル上書き防止、リファクタリング
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikanixonable committed Feb 10, 2024
1 parent 78d44b6 commit 4e7af9c
Show file tree
Hide file tree
Showing 76 changed files with 185 additions and 3 deletions.
148 changes: 148 additions & 0 deletions PaxKawaiina202402/PaxKawaiina.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⢀⢀⠠⡠⠠⡠⡰⣒⢄⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢌⢂⠢⡡⢑⠡⠪⡹⢮⢲⢱⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡔⡡⢘⠨⡂⢆⠢⡀⡯⣮⡪⢂⠕⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢐⢇⠧⣑⢄⠐⠈⠪⢢⡫⡟⠌⡂⡂⠢⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⡸⡱⡡⢳⠠⠠⠀⡀⠕⢍⠀⠀⢐⠨⢐⢐⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠐⠌⢎⡂⠀⠉⣂⡕⠅⠕⠅⢁⠀⠐⡨⢐⢐⠨⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠅⠅⢅⠢⠀⢕⢗⢎⢎⠌⢀⠀⠠⠀⢂⢂⠢⠨⡈⠄⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⢡⢑⢐⢅⢐⠡⡃⡂⡑⡌⠄⠂⠂⠀⠐⡐⡡⢁⠢⠐⡀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠠⢨⢪⢢⡣⠧⢑⢼⡐⡅⢎⢾⢜⢶⠅⠀⠀⢱⢐⠡⠨⢐⠀⠄⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠄⡇⣗⢵⢙⠈⣔⠅⡏⡪⠘⠨⣻⢮⡂⠀⠀⠨⡪⡊⠌⡀⠂⠀⠄⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣜⢮⡺⣜⡀⡢⡳⡅⢌⢆⠁⠀⡯⣗⡇⠀⠀⠀⢹⡪⡀⠄⠂⠀⠀⠄⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠠⡸⡪⢣⢙⠜⡰⡹⢸⢪⠡⠪⡄⠀⣯⣳⢣⠀⠀⠀⠐⡝⡆⡂⢂⠈⢀⠠⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠠⢱⡁⠆⣇⢕⢕⢜⠮⡨⠂⢕⣕⢌⠢⣗⢗⢽⡀⠀⠀⠀⢣⡳⡨⢐⠨⠀⠄⠐⡀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠠⣑⢵⢕⡼⣾⣺⢾⣗⢅⠕⡱⠘⡜⣌⡮⣯⡇⡎⣇⠀⠀⠀⠨⣪⠢⡂⢊⠌⢂⠡⠐⠀⠀
# ⠀⠀⠀⠀⠀⠠⣑⡾⣝⡆⢟⢼⡺⡯⡯⣳⡐⣄⡧⡯⡗⡫⣷⣳⠱⡽⡀⠀⠀⠀⡣⡣⠊⠄⢌⠐⠌⠌⡂⠀
# ⠀⠀⠀⠀⡠⠡⡢⡻⡮⡏⡢⢇⢋⠯⠯⡪⢏⠳⡽⠰⢈⢸⣗⣗⢕⢝⣆⠀⠀⠀⢪⢪⠨⠨⠠⠡⠡⠡⠨⡀
# ⠀⠀⠀⡐⠄⢅⣗⢥⢪⣂⠄⡂⠄⠩⡈⠀⠁⠃⠅⢃⢢⠻⣺⡪⣎⢮⠳⠀⠀⠀⢂⠇⢌⠌⠌⠌⠌⠌⡂⡂
# ⠀⠀⡐⡐⢡⡳⣕⢧⡣⣯⣻⢺⣪⠢⠂⠀⠀⠘⡽⡽⣕⢧⠀⠂⠂⠁⠀⠀⠀⠀⢐⠅⠅⠌⠌⠌⠌⡂⡂⡂
# ⠀⢐⢐⢈⢲⢝⢮⡳⣕⣗⣗⢗⡗⢅⠀⠀⠀⠀⢹⣝⢮⣳⡀⠀⠀⠀⠀⠀⠀⠀⡐⠌⢜⠨⡊⠌⡂⡂⠢⠨
# ⠀⢂⢂⠢⢣⢫⡳⣝⢮⣞⣮⡳⡝⠄⠀⠀⠀⠀⠐⡽⣕⡷⡅⠀⠀⠀⠀⠀⠀⠀⢌⢜⢜⢜⢌⢂⠢⠨⠨⠨
# ⠈⠄⡂⢌⠪⡪⡪⡮⡳⣗⣗⢯⡇⡅⠀⠀⠀⠀⠀⢸⣗⢯⢯⠀⠀⠀⠀⠀⠀⠨⡐⡼⣕⢕⢕⠔⡡⠡⢁⠡
# ⠄⠁⡂⠢⡑⢌⢺⡪⣏⡷⣝⢗⡗⡕⠀⠀⠀⠀⠀⠈⡮⣏⢯⡂⠀⠀⠀⠀⠀⡪⢰⢣⡣⡣⡣⡊⠄⠂⠀⠀
# ⡂⡐⠌⡂⡊⠔⡵⡹⣜⣟⣮⡳⡑⠩⡀⠀⠀⠀⠀⠀⠹⣮⡳⣇⠀⠀⠀⠀⡨⡪⡪⡪⡪⡪⡪⢌⢪⠠⠁⠀
# ⢂⠔⠡⡂⡪⢨⢪⢪⣺⣟⡮⡇⠀⠀⠂⡀⠀⠀⠀⠀⠀⠳⣯⣻⣆⠀⠀⢀⣗⢝⣜⢮⡪⣪⢪⡪⡂⠇⠁⠀
# ⠐⡨⢸⢌⢆⢕⢕⣕⡯⣗⡯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣾⢽⣆⠀⠐⡕⢕⢕⢧⡫⡮⡳⡑⠁⠀⠀⠀
# ⠀⠨⢪⢳⡱⡵⣕⠗⣯⣗⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠹⣽⣺⠄⠀⠨⠈⢇⠧⡳⡹⠈⠀⠀⠀⠀⠀
# ⠀⠀⠈⠑⢝⢜⢘⠀⡷⣯⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠸⣞⣗⠀⠀⠈⠀⠅⠃⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠁⠂⠁⡯⣗⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⢺⣝⠆⠀⠀⠀⠁⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⣯⠳⡅⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢸⡪⢯⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⢮⡫⡪⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⢇⢇⢗⡇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# ⠀⠀⠀⠀⠀⠀⠀⠀⠹⡎⠗⠇⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠘⢕⢕⠳⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀
# Thy Pax Kawaiina come

import json
import numpy as np
import tensorflow as tf
from PIL import Image
import PIL
from tensorflow.keras.preprocessing.image import img_to_array
import os
import re
import shutil
import pyexiv2

def isImg(item):
pattern = re.compile('.+\.(jpg|png|jfif|jpeg|JPG|PNG|webp)')
if pattern.match(item):
return True
else:
return False

def isKawaii(file):
#画像を配列に変換
try:
imgPIL = Image.open(file)
imgPIL = imgPIL.convert('RGB')
imgPIL = imgPIL.resize((input_width, input_height))
x = img_to_array(imgPIL) / 255 #正規化
x = x[None, ...]

# 推論
predict = infer(tf.constant(x))
label = classes['Label']
varr = predict['Confidences'][0].numpy()
value = label[np.argmax(varr)] #"Not2D"か "KawaiiPictures"がはいる

if value == "KawaiiPictures":
return True
else:
return False
except PIL.UnidentifiedImageError:
return False
except PIL.Image.DecompressionBombError:
return False
except OSError:
return False

def isPhoto(file):
try:
with pyexiv2.Image(file) as imgExif:
device = imgExif.read_exif()['Exif.Image.Model']
print(device)
return True
except KeyError:
return False
except RuntimeError:
return True
except UnicodeDecodeError:
return False

def moveTo(oldpath,dirName):
os.makedirs(dirName, exist_ok=True)
#同名のファイルが既にあったときのファイル名変更措置
k = 1
fileName0 = os.path.basename(oldpath)
while True:
if not os.path.isfile(os.path.join(dirName, fileName0)):
shutil.move(oldpath, os.path.join(dirName, fileName0))
break
else:
fileName0 = os.path.splitext(fileName0)[0] + "_" + str(k) + os.path.splitext(fileName)[1]
k += 1

def moveToOld(file,dirName):
os.makedirs(dirName, exist_ok=True)
shutil.move(file, dirName)

def moveImg(file):
loc = os.path.dirname(file)
if isImg(file):
if isKawaii(file) and not isPhoto(file):
moveTo(file,loc+"/nijis")

if isPhoto(file):
try:
moveTo(file,loc+"/photos")
except FileNotFoundError:
print("oh...")
else:
if isKawaii(file):
moveTo(file,loc+"/nijis")
else:
moveTo(file,loc+"/nonijis")


else:
moveTo(file,loc+"/zanyo")

def eachpax(dir):
files = [os.path.join(dir,file) for file in os.listdir(dir) if os.path.isfile(os.path.join(dir,file))]
for file in files:
moveImg(file)

if __name__ == "__main__":


# signature読込
with open('./signature.json', 'r') as f:
signature = json.load(f)
inputs = signature.get('inputs')
classes = signature.get('classes')
input_width, input_height = inputs['Image']['shape'][1:3] # モデルの学習画像サイズを取得

# model読込
model = tf.saved_model.load('') #ここで時刻がプリントされる
infer = model.signatures['serving_default']

eachpax('../')
3 changes: 3 additions & 0 deletions PaxKawaiina202402/PaxKawaiinaStop.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
call %userprofile%\anaconda3\Scripts\activate.bat font
python PaxKawaiina.py
cmd /k
Loading

0 comments on commit 4e7af9c

Please sign in to comment.