-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathneural_bleach.py
More file actions
125 lines (100 loc) · 4.91 KB
/
neural_bleach.py
File metadata and controls
125 lines (100 loc) · 4.91 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import torch
import numpy as np
import cv2
import argparse
import os
import sys
from diffusers import StableDiffusionControlNetImg2ImgPipeline, ControlNetModel
from PIL import Image
def stego_washer_ultimate(image_path, output_filename=None, strength=0.25):
# CHECK FILE EXISTENCE
if not os.path.exists(image_path):
print(f"[-] Error: File not found: {image_path}")
return
print(f"\n--- PROCESSING (ULTIMATE MODE): {image_path} ---")
# Generate output filename if not provided
if output_filename is None:
base, ext = os.path.splitext(image_path)
output_filename = f"{base}_clean.jpg"
# Setup Device (Force CUDA if available, warn if CPU)
device = "cuda" if torch.cuda.is_available() else "cpu"
if device == "cpu":
print("[!] WARNING: Running on CPU. This will be extremely slow.")
try:
# LOAD CONTROLNET (Structure Lock)
print("[*] Loading ControlNet (Canny Edge Detector)...")
controlnet = ControlNetModel.from_pretrained(
"lllyasviel/sd-controlnet-canny",
torch_dtype=torch.float16 if device == "cuda" else torch.float32
)
# LOAD REALISTIC VISION V5.1
model_id = "SG161222/Realistic_Vision_V5.1_noVAE"
print(f"[*] Loading High-Fidelity Model: {model_id}...")
pipe = StableDiffusionControlNetImg2ImgPipeline.from_pretrained(
model_id,
controlnet=controlnet,
torch_dtype=torch.float16 if device == "cuda" else torch.float32,
safety_checker=None
).to(device)
# Optimization for local VRAM
pipe.enable_attention_slicing()
# PREPARE IMAGE & UPSCALE
print("[*] Loading and Upscaling Image...")
original_img = Image.open(image_path).convert("RGB")
w, h = original_img.size
# Upscale 1.2x for better face details
target_w = int(w * 1.2)
target_h = int(h * 1.2)
# Snap to multiples of 8
target_w = (target_w // 8) * 8
target_h = (target_h // 8) * 8
img_input = original_img.resize((target_w, target_h), Image.Resampling.LANCZOS)
# EDGE DETECTION (OpenCV)
print("[*] Extracting structural wireframe...")
image_cv = np.array(img_input)
# Thresholds: 50/200 are good for general detail
canny_image = cv2.Canny(image_cv, 50, 200)
canny_image = canny_image[:, :, None]
canny_image = np.concatenate([canny_image, canny_image, canny_image], axis=2)
control_image = Image.fromarray(canny_image)
# LAUNDERING
print(f"[*] AI Laundering (Strength: {strength})...")
prompt = "RAW photo, subject, 8k uhd, dslr, soft lighting, high quality, film grain, Fujifilm XT3"
negative_prompt = "cgi, 3d, cartoon, anime, text, bad anatomy, deformed iris, deformed pupils, extra fingers, mutated hands, poorly drawn face, mutation, disfigured, blurry, missing limbs, fused fingers, ugly, long neck"
# Suppress progress bar slightly for cleaner CLI
laundered_img = pipe(
prompt=prompt,
image=img_input,
control_image=control_image,
strength=strength,
controlnet_conditioning_scale=1.0,
guidance_scale=5.0,
negative_prompt=negative_prompt
).images[0]
# ANALOG HUMANIZATION
print("[*] Applying Analog Simulation...")
# Resize back to original dimensions
laundered_img = laundered_img.resize((w, h), Image.Resampling.LANCZOS)
r, g, b = laundered_img.split()
# Chromatic Aberration
r = Image.fromarray(np.roll(np.array(r), -1, axis=1))
b = Image.fromarray(np.roll(np.array(b), 1, axis=1))
humanized_img = Image.merge("RGB", (r, g, b))
# Film Grain
img_array = np.array(humanized_img).astype(np.float32)
noise = np.random.normal(0, 4, img_array.shape).astype(np.float32)
final_img = Image.fromarray(np.clip(img_array + noise, 0, 255).astype(np.uint8))
# SAVE
print(f"[*] Stripping Metadata & Saving to {output_filename}...")
final_img.save(output_filename, "JPEG", quality=95, optimize=True)
print(f"[+] SUCCESS! Process complete.")
except Exception as e:
print(f"[-] Critical Error: {e}")
print(" Note: First run requires internet to download models (~4GB).")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Neural-Bleach Ultimate: AI Watermark Remover")
parser.add_argument("image", help="Path to the image file")
parser.add_argument("--out", help="Output filename", default=None)
parser.add_argument("--strength", type=float, default=0.20, help="Laundering strength (default: 0.20)")
args = parser.parse_args()
stego_washer_ultimate(args.image, args.out, args.strength)