-
Notifications
You must be signed in to change notification settings - Fork 10
/
lcm.py
110 lines (91 loc) · 4.26 KB
/
lcm.py
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
from pynput import keyboard
import pyperclip
import cv2
import numpy as np
import pygetwindow as gw
from diffusers import AutoPipelineForImage2Image, UNet2DConditionModel, LCMScheduler, AutoencoderKL
from diffusers.utils import load_image
import torch
from PIL import Image
import pygetwindow as gw
import io
import win32clipboard
from PIL import Image
def send_image_to_clipboard(image):
output = io.BytesIO()
image.convert("RGB").save(output, "BMP")
data = output.getvalue()[14:] # BMPファイルヘッダを削除
output.close()
win32clipboard.OpenClipboard() # クリップボードを開く
win32clipboard.EmptyClipboard() # クリップボードを空にする
win32clipboard.SetClipboardData(win32clipboard.CF_DIB, data) # クリップボードに画像を設定
win32clipboard.CloseClipboard() # クリップボードを閉じる
def get_pipe(config):
vae_model_path = config.vae_model_path.get()
vae_model_path = vae_model_path.replace("\\", "/")
LoRA_model_path = config.LoRA_model_path.get()
LoRA_model_path = LoRA_model_path.replace("\\", "/")
if config.vae_model_path.get() != "":
pipe = AutoPipelineForImage2Image.from_pretrained(
config.generation_model_name.get(), torch_dtype=torch.float16, use_safetensors=True,
vae = AutoencoderKL.from_single_file(vae_model_path, torch_dtype=torch.float16)
).to("cuda")
else:
pipe = AutoPipelineForImage2Image.from_pretrained(
config.generation_model_name.get(), torch_dtype=torch.float16, use_safetensors=True,
).to("cuda")
if config.LoRA_model_path.get() != "":
pipe.load_lora_weights(LoRA_model_path, adapter_name="LoRA")
pipe.load_lora_weights(config.lcm_model_name.get(), adapter_name="lcm")
pipe.set_adapters(["LoRA", "lcm"], adapter_weights=[config.LoRAstrength_value, 1.0])
pipe.fuse_lora()
else:
pipe.load_lora_weights(config.lcm_model_name.get(), adapter_name="lcm")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
if pipe.safety_checker is not None:
pipe.safety_checker = lambda images, **kwargs: (images, [False])
return pipe
def on_press(key):
global img # imgをグローバル変数として使用します
if hasattr(key, 'char') and key.char == 'p': # 'p'キーが押されたかチェック
# OpenCVのBGR形式からPILのRGB形式に変換し、クリップボードにコピーする
if isinstance(img, np.ndarray):
try:
# RGB形式に変換する前に、imgがNumPy配列であることを確認する
pil_img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
send_image_to_clipboard(pil_img)
except Exception as e:
print(f"An error occurred: {e}")
else:
print("The image is not in the correct format. img must be a NumPy array.")
def LCM_run(config, pipe):
global img # imgをグローバル変数として使用します
window_name = "Window Capture"
cv2.namedWindow(window_name, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(window_name, cv2.WND_PROP_TOPMOST, 1)
config.running = True
listener = keyboard.Listener(on_press=on_press)
listener.start() # キーボードリスナーを開始
try:
while config.running:
screenshot = config.screen_capture.capture()
input_img_np = np.array(screenshot)
generator = torch.Generator("cuda").manual_seed(2500)
input_img = Image.fromarray(input_img_np)
img_pil = pipe(
strength=config.strength_value,
prompt=config.prompt.get(),
image=input_img,
num_inference_steps=config.num_inference_steps_value,
guidance_scale=1,
generator=generator
).images[0]
img = np.array(img_pil)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
cv2.imshow(window_name, img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
finally:
cv2.destroyAllWindows()
listener.stop() # キーボードリスナーを停止する
listener.join() # リスナーの完全な停止を待つ# キーボードリスナーを停止