This commit is contained in:
Hazel 2025-04-24 15:15:46 +02:00
parent 061cc20046
commit 94b641cbd6
3 changed files with 19 additions and 22 deletions

View File

@ -13,7 +13,7 @@ I first realized that a normal mosaic algorithm isn't safe AT ALL seeing this pr
```bash ```bash
# Step 1: Create and activate virtual environment # Step 1: Create and activate virtual environment
python3 -m venv .venv python3 -m venv .venv
source venv/bin/activate source .venv/bin/activate
# Step 2: Install the local Python program add the -e flag for development # Step 2: Install the local Python program add the -e flag for development
pip install . pip install .
@ -36,7 +36,6 @@ rm big-lama.zip
# get the code to run the models # get the code to run the models
cd big-lama cd big-lama
git clone https://github.com/advimman/lama.git git clone https://github.com/advimman/lama.git
pip install torch==2.2.0 torchvision==0.17.0
cd lama cd lama
pip install -r requirements.txt pip install -r requirements.txt
``` ```

View File

@ -5,6 +5,6 @@ from .pixelation_process import pixelate
def cli(): def cli():
print(f"Running secure_pixelation") print(f"Running secure_pixelation")
pixelate("assets/human_detection/test.png", generative_impaint=False) pixelate("assets/human_detection/test.png", generative_impaint=True)
pixelate("assets/human_detection/humans.png", generative_impaint=False) # pixelate("assets/human_detection/humans.png", generative_impaint=False)
pixelate("assets/human_detection/rev1.png", generative_impaint=False) # pixelate("assets/human_detection/rev1.png", generative_impaint=False)

View File

@ -4,6 +4,7 @@ from typing import Optional
from pathlib import Path from pathlib import Path
import subprocess import subprocess
import sys import sys
import os
import cv2 import cv2
import numpy as np import numpy as np
@ -28,7 +29,7 @@ def get_mask(raw_image: RawImage) -> np.ndarray:
return mask return mask
def impaint(raw_image: RawImage, image: Optional[np.ndarray] = None) -> np.ndarray: def quick_impaint(raw_image: RawImage, image: Optional[np.ndarray] = None) -> np.ndarray:
image = image if image is not None else raw_image.get_image() image = image if image is not None else raw_image.get_image()
mask = get_mask(raw_image) mask = get_mask(raw_image)
@ -52,28 +53,25 @@ def do_generative_impaint(raw_image: RawImage, image: Optional[np.ndarray] = Non
# Run LaMa inference (adjust path if needed) # Run LaMa inference (adjust path if needed)
try: try:
pwd = os.getcwd()
subprocess.run([ subprocess.run([
sys.executable, "big-lama/lama/bin/predict.py", sys.executable, "lama/bin/predict.py",
"model.path=big-lama/big-lama", f"model.path={pwd}/lama/models/big-lama",
f"indir={str(lama_dict_in)}", f"indir={pwd}/{str(lama_dict_in)}",
f"outdir={str(lama_dict_out)}" f"outdir={pwd}/{str(lama_dict_out)}"
], check=True) ], check=True)
except subprocess.CalledProcessError as e: except subprocess.CalledProcessError as e:
print(f"Error running LaMa: {e}") print(f"Error running LaMa: {e}")
return image # fallback to original if it fails print("falling back to non generative inpaint")
return quick_impaint(raw_image=raw_image, image=image)
# Load inpainted result # Load inpainted result
result_path = os.path.join(output_dir, "image.png") result_path = lama_dict_out / "image.png"
if os.path.exists(result_path): if result_path.exists():
inpainted_image = cv2.imread(result_path) return cv2.imread(str(result_path))
else: else:
print("Inpainted result not found, returning original.") print("Inpainted result not found, falling back to non generative inpaint")
inpainted_image = image return quick_impaint(raw_image=raw_image, image=image)
# Cleanup
shutil.rmtree(base_dir)
return inpainted_image
@ -115,7 +113,7 @@ def pixelate(to_detect: str, generative_impaint: bool = True, debug_drawings: bo
if generative_impaint: if generative_impaint:
step_2 = do_generative_impaint(raw_image, image=step_1) step_2 = do_generative_impaint(raw_image, image=step_1)
else: else:
step_2 = impaint(raw_image, image=step_1) step_2 = quick_impaint(raw_image, image=step_1)
write_image(step_2, "step_2") write_image(step_2, "step_2")
step_3 = pixelate_regions(raw_image, image=step_2) step_3 = pixelate_regions(raw_image, image=step_2)