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.
82 lines
2.6 KiB
Python
82 lines
2.6 KiB
Python
"""Text extraction from images using Tesseract or PaddleOCR."""
|
|
import sys
|
|
import json
|
|
|
|
|
|
def main():
|
|
input_path = sys.argv[1]
|
|
settings = json.loads(sys.argv[2]) if len(sys.argv) > 2 else {}
|
|
|
|
engine = settings.get("engine", "tesseract")
|
|
language = settings.get("language", "en")
|
|
|
|
try:
|
|
if engine == "paddleocr":
|
|
try:
|
|
from paddleocr import PaddleOCR
|
|
|
|
ocr = PaddleOCR(use_angle_cls=True, lang=language)
|
|
result = ocr.ocr(input_path, cls=True)
|
|
text = "\n".join(
|
|
[
|
|
line[1][0]
|
|
for res in result
|
|
if res
|
|
for line in res
|
|
if line and line[1]
|
|
]
|
|
)
|
|
print(
|
|
json.dumps({"success": True, "text": text, "engine": "paddleocr"})
|
|
)
|
|
except ImportError:
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"success": False,
|
|
"error": "PaddleOCR is not installed. Install with: pip install paddleocr paddlepaddle",
|
|
}
|
|
)
|
|
)
|
|
sys.exit(1)
|
|
else:
|
|
# Tesseract via subprocess
|
|
import subprocess
|
|
|
|
lang_map = {"en": "eng", "de": "deu", "fr": "fra", "es": "spa", "zh": "chi_sim", "ja": "jpn", "ko": "kor"}
|
|
tess_lang = lang_map.get(language, "eng")
|
|
|
|
try:
|
|
result = subprocess.run(
|
|
["tesseract", input_path, "stdout", "-l", tess_lang],
|
|
capture_output=True,
|
|
text=True,
|
|
timeout=120,
|
|
)
|
|
text = result.stdout.strip()
|
|
if result.returncode != 0 and not text:
|
|
raise RuntimeError(result.stderr.strip() or "Tesseract failed")
|
|
print(
|
|
json.dumps(
|
|
{"success": True, "text": text, "engine": "tesseract"}
|
|
)
|
|
)
|
|
except FileNotFoundError:
|
|
print(
|
|
json.dumps(
|
|
{
|
|
"success": False,
|
|
"error": "Tesseract is not installed. Install with: apt-get install tesseract-ocr",
|
|
}
|
|
)
|
|
)
|
|
sys.exit(1)
|
|
|
|
except Exception as e:
|
|
print(json.dumps({"success": False, "error": str(e)}))
|
|
sys.exit(1)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|