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
# Step 1: Create and activate virtual environment
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
pip install .
@ -36,7 +36,6 @@ rm big-lama.zip
# get the code to run the models
cd big-lama
git clone https://github.com/advimman/lama.git
pip install torch==2.2.0 torchvision==0.17.0
cd lama
pip install -r requirements.txt
```

View File

@ -5,6 +5,6 @@ from .pixelation_process import pixelate
def cli():
print(f"Running secure_pixelation")
pixelate("assets/human_detection/test.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/test.png", generative_impaint=True)
# pixelate("assets/human_detection/humans.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
import subprocess
import sys
import os
import cv2
import numpy as np
@ -28,7 +29,7 @@ def get_mask(raw_image: RawImage) -> np.ndarray:
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()
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)
try:
pwd = os.getcwd()
subprocess.run([
sys.executable, "big-lama/lama/bin/predict.py",
"model.path=big-lama/big-lama",
f"indir={str(lama_dict_in)}",
f"outdir={str(lama_dict_out)}"
sys.executable, "lama/bin/predict.py",
f"model.path={pwd}/lama/models/big-lama",
f"indir={pwd}/{str(lama_dict_in)}",
f"outdir={pwd}/{str(lama_dict_out)}"
], check=True)
except subprocess.CalledProcessError as 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
result_path = os.path.join(output_dir, "image.png")
if os.path.exists(result_path):
inpainted_image = cv2.imread(result_path)
result_path = lama_dict_out / "image.png"
if result_path.exists():
return cv2.imread(str(result_path))
else:
print("Inpainted result not found, returning original.")
inpainted_image = image
# Cleanup
shutil.rmtree(base_dir)
return inpainted_image
print("Inpainted result not found, falling back to non generative inpaint")
return quick_impaint(raw_image=raw_image, image=image)
@ -115,7 +113,7 @@ def pixelate(to_detect: str, generative_impaint: bool = True, debug_drawings: bo
if generative_impaint:
step_2 = do_generative_impaint(raw_image, image=step_1)
else:
step_2 = impaint(raw_image, image=step_1)
step_2 = quick_impaint(raw_image, image=step_1)
write_image(step_2, "step_2")
step_3 = pixelate_regions(raw_image, image=step_2)