mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Add Python bridge (packages/ai/src/bridge.ts) that calls Python scripts via child_process with venv-first fallback to system python3. Implements 6 AI-powered tools: - Remove Background: rembg-based with U2-Net/IS-Net models - Image Upscaling: Real-ESRGAN with Lanczos fallback - OCR/Text Extraction: Tesseract + PaddleOCR engines - Face/PII Blur: MediaPipe face detection with configurable blur - Object Eraser: LaMa inpainting with mask-based input - Smart Crop: Sharp attention-based entropy cropping (no Python needed) Each tool includes: Python script, TypeScript wrapper, API route, and React settings component. All Python scripts handle ImportError gracefully with clear installation messages.
79 lines
2.1 KiB
Python
79 lines
2.1 KiB
Python
"""Image upscaling with Real-ESRGAN fallback to Lanczos."""
|
|
import sys
|
|
import json
|
|
|
|
|
|
def main():
|
|
input_path = sys.argv[1]
|
|
output_path = sys.argv[2]
|
|
settings = json.loads(sys.argv[3]) if len(sys.argv) > 3 else {}
|
|
|
|
scale = settings.get("scale", 2)
|
|
|
|
try:
|
|
from PIL import Image
|
|
|
|
img = Image.open(input_path)
|
|
new_size = (img.width * scale, img.height * scale)
|
|
|
|
# Try Real-ESRGAN first
|
|
try:
|
|
from basicsr.archs.rrdbnet_arch import RRDBNet
|
|
from realesrgan import RealESRGANer
|
|
import numpy as np
|
|
|
|
model = RRDBNet(
|
|
num_in_ch=3,
|
|
num_out_ch=3,
|
|
num_feat=64,
|
|
num_block=23,
|
|
num_grow_ch=32,
|
|
scale=scale,
|
|
)
|
|
upsampler = RealESRGANer(
|
|
scale=scale,
|
|
model_path=None,
|
|
model=model,
|
|
half=False,
|
|
)
|
|
img_array = np.array(img.convert("RGB"))
|
|
output, _ = upsampler.enhance(img_array, outscale=scale)
|
|
result = Image.fromarray(output)
|
|
result.save(output_path)
|
|
method = "realesrgan"
|
|
except (ImportError, Exception):
|
|
# Fallback to Lanczos upscaling
|
|
img_upscaled = img.resize(new_size, Image.LANCZOS)
|
|
img_upscaled.save(output_path)
|
|
method = "lanczos"
|
|
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"success": True,
|
|
"scale": scale,
|
|
"width": new_size[0],
|
|
"height": new_size[1],
|
|
"method": method,
|
|
}
|
|
)
|
|
)
|
|
|
|
except ImportError:
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"success": False,
|
|
"error": "Pillow is not installed. Install with: pip install Pillow",
|
|
}
|
|
)
|
|
)
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
print(json.dumps({"success": False, "error": str(e)}))
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|