Skip to content

Commit

Permalink
Option to not load torch
Browse files Browse the repository at this point in the history
  • Loading branch information
ThePromidius committed Jan 9, 2024
1 parent c64e576 commit cb50fc2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 64 deletions.
14 changes: 11 additions & 3 deletions MangaManager/MangaManager.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,31 @@ block_cipher = None
added_files = [
( 'res/*', 'res'),
('ExternalSources', 'ExternalSources'),
('Extensions', 'Extensions')
('Extensions', 'Extensions'),
# THe following are picked from env variable. Please run python to include possible missing files
('../venv_3.11/Lib/site-packages/sv_ttk/*','sv_ttk'), # To save the sv_ttk.tcl file - MISSING IMPORT
('../venv_3.11/Lib/site-packages/sv_ttk/theme*','sv_ttk/theme'), # To save the sv_ttk.tcl file - MISSING IMPORT
('../venv_3.11/Lib/site-packages/open_clip/model_configs/ViT-B-16-plus-240.json','open_clip/model_configs'), # To save the sv_ttk.tcl file - MISSING IMPORT
('../venv_3.11/Lib/site-packages/open_clip/bpe_simple_vocab_16e6.txt.gz','open_clip') # To save the sv_ttk.tcl file - MISSING IMPORT

]

a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=added_files,
hiddenimports=['PIL._tkinter_finder'],
hookspath=['pyinstaller_hooks'],
hiddenimports=['PIL._tkinter_finder','tkinterdnd2.TkinterDnD'],
hookspath=['MangaManager/pyinstaller_hooks'],
hooksconfig={},
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher,
noarchive=False,
paths=["venv_3.11/Lib/site-packages"] # So it loads libraries from dev env first
# collect_all=True
)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

Expand Down
4 changes: 2 additions & 2 deletions MangaManager/src/DynamicLibController/extension_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ def load_extensions(extensions_directory,) -> list[IExtensionApp]:
try:
extension_module = importlib.import_module(f"Extensions.{'.'.join(extract_folder_and_module(extension_file))}",package=EXTENSIONS_DIRECTORY)
except ModuleNotFoundError:
logger.exception(f"Failed to Import Extension: {extension_file}")
logger.exception(f"Failed to Import Extension module: {extension_file}")
continue
except Exception:
logger.exception(f"Failed to Load extension {extension_file}")
logger.exception(f"Unhandled exception. Extension module {extension_file}")

# Get the ExtensionApp subclasses from the module
extension_classes = [
Expand Down
58 changes: 29 additions & 29 deletions MangaManager/src/MetadataManager/CoverManager/CoverManager.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

from src.Common import ResourceLoader
from src.Common.LoadedComicInfo.LoadedComicInfo import CoverActions, LoadedComicInfo

from src.MetadataManager.CoverManager import torchlib
from src.MetadataManager.GUI.MessageBox import MessageBoxWidgetFactory as mb
from src.MetadataManager.GUI.scrolledframe import ScrolledFrame
from src.MetadataManager.GUI.widgets import ButtonWidget
Expand Down Expand Up @@ -451,7 +451,7 @@ def select_similar(self):
:return:
"""

# from src.MetadataManager.CoverManager import torchlib


assert len(self.selected_frames) == 1
frame, pos = self.selected_frames[0]
Expand All @@ -466,33 +466,33 @@ def select_similar(self):
# Compare all covers:
delta = float(self.delta_entry.get())

# for comicframe in self.scrolled_widget.winfo_children():
# comicframe: ComicFrame
# lcinfo: LoadedComicInfo = comicframe.loaded_cinfo
# try:
# if self.scan_covers.get():
# photo_image = lcinfo.get_cover_cache()
# if photo_image is None:
# logger.error(f"Failed to compare front cover image. File is not loaded. File '{lcinfo.file_name}'")
#
# else:
# score = round(torchlib.generateScore(torchlib.convert_PIL(selected_image),torchlib.convert_PIL(ImageTk.getimage(photo_image))), 2)
# if score > delta:
# self.select_frame(None, frame=comicframe, pos="front")
#
# if self.scan_backcovers.get():
# photo_image = lcinfo.get_cover_cache(True)
# if photo_image is None:
# logger.error(f"Failed to compare back cover image. File is not loaded. File '{lcinfo.file_name}'")
# else:
# score = round(torchlib.generateScore(torchlib.convert_PIL(selected_image),
# torchlib.convert_PIL(
# ImageTk.getimage(photo_image))
# ),2)
# if score > delta:
# self.select_frame(None, frame=comicframe, pos="back")
# except Exception:
# logger.exception(f"Failed to compare images for file {comicframe.loaded_cinfo.file_name}")
for comicframe in self.scrolled_widget.winfo_children():
comicframe: ComicFrame
lcinfo: LoadedComicInfo = comicframe.loaded_cinfo
try:
if self.scan_covers.get():
photo_image = lcinfo.get_cover_cache()
if photo_image is None:
logger.error(f"Failed to compare front cover image. File is not loaded. File '{lcinfo.file_name}'")

else:
score = round(torchlib.generateScore(torchlib.convert_PIL(selected_image),torchlib.convert_PIL(ImageTk.getimage(photo_image))), 2)
if score > delta:
self.select_frame(None, frame=comicframe, pos="front")

if self.scan_backcovers.get():
photo_image = lcinfo.get_cover_cache(True)
if photo_image is None:
logger.error(f"Failed to compare back cover image. File is not loaded. File '{lcinfo.file_name}'")
else:
score = round(torchlib.generateScore(torchlib.convert_PIL(selected_image),
torchlib.convert_PIL(
ImageTk.getimage(photo_image))
),2)
if score > delta:
self.select_frame(None, frame=comicframe, pos="back")
except Exception:
logger.exception(f"Failed to compare images for file {comicframe.loaded_cinfo.file_name}")

def _scan_images(self, x, lcinfo:LoadedComicInfo, comicframe, is_backcover=False):
"""
Expand Down
81 changes: 51 additions & 30 deletions MangaManager/src/MetadataManager/CoverManager/torchlib.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,69 @@
import logging

print("Importing torchlib")
# pip requirements:
# OpenCV-Python
# sentence_transformers
# open_clip_torch
# requests
# git+https://github.com/openai/CLIP.git
import PIL
import torch
import open_clip
from PIL import Image
from cv2 import cvtColor, COLOR_BGR2RGB
from numpy import array
from sentence_transformers import util
from PIL import Image

try:
import PIL
import torch
import open_clip
from PIL import Image
from cv2 import cvtColor, COLOR_BGR2RGB
from numpy import array
from sentence_transformers import util
from PIL import Image


def imageEncoder(img):
img1 = Image.fromarray(img).convert('RGB')
img1 = preprocess(img1).unsqueeze(0).to(device)
img1 = model.encode_image(img1)
return img1


def generateScore(image1, image2):
img1 = imageEncoder(image1)
img2 = imageEncoder(image2)
cos_scores = util.pytorch_cos_sim(img1, img2)
score = round(float(cos_scores[0][0]) * 100, 2)
return score


def convert_PIL(pil_img: PIL.Image.Image):
# pil_image = image.convert('RGB')
# open_cv_image = numpy.array(pil_image)
# # Convert RGB to BGR
# return = open_cv_image[:, :, ::-1].copy()
# cv2_img = numpy.array(pil_img)
# return cv2.cvtColor(cv2_img, cv2.COLOR_RGB2BGR)
return cvtColor(array(pil_img), COLOR_BGR2RGB)
except ImportError:
logging.getLogger().error("Missing dependecies for torch. disabling similarity")


def convert_PIL(*_):
return None


def generateScore(*_):
return 0


def imageEncoder(*_):
return None

# image processing model
device = "cuda" if torch.cuda.is_available() else "cpu"
model, _, preprocess = open_clip.create_model_and_transforms('ViT-B-16-plus-240', pretrained="laion400m_e32")
model.to(device)


def imageEncoder(img):
img1 = Image.fromarray(img).convert('RGB')
img1 = preprocess(img1).unsqueeze(0).to(device)
img1 = model.encode_image(img1)
return img1


def generateScore(image1, image2):
img1 = imageEncoder(image1)
img2 = imageEncoder(image2)
cos_scores = util.pytorch_cos_sim(img1, img2)
score = round(float(cos_scores[0][0]) * 100, 2)
return score


def convert_PIL(pil_img: PIL.Image.Image):
# pil_image = image.convert('RGB')
# open_cv_image = numpy.array(pil_image)
# # Convert RGB to BGR
# return = open_cv_image[:, :, ::-1].copy()
# cv2_img = numpy.array(pil_img)
# return cv2.cvtColor(cv2_img, cv2.COLOR_RGB2BGR)
return cvtColor(array(pil_img), COLOR_BGR2RGB)

# if __name__ == '__main__':
# img_rgb = cvtColor(array(Image.open(img)), COLOR_BGR2RGB)
Expand Down

0 comments on commit cb50fc2

Please sign in to comment.