242 lines
7.6 KiB
Python
242 lines
7.6 KiB
Python
"""
|
|
Lemontropia Suite - OCR Demo
|
|
Demonstrates the multi-backend OCR system.
|
|
"""
|
|
|
|
import sys
|
|
import cv2
|
|
import numpy as np
|
|
from pathlib import Path
|
|
|
|
# Add parent directory to path
|
|
sys.path.insert(0, str(Path(__file__).parent))
|
|
|
|
|
|
def create_demo_image():
|
|
"""Create a demo image with text-like regions."""
|
|
# Create white image
|
|
img = np.ones((600, 800, 3), dtype=np.uint8) * 255
|
|
|
|
# Draw some "UI elements" that look like game text
|
|
# Header bar
|
|
cv2.rectangle(img, (50, 30), (750, 70), (50, 50, 50), -1)
|
|
|
|
# Loot window
|
|
cv2.rectangle(img, (450, 100), (780, 400), (200, 200, 200), -1)
|
|
cv2.rectangle(img, (450, 100), (780, 130), (100, 100, 100), -1)
|
|
|
|
# Item slots (grid)
|
|
slot_size = 50
|
|
gap = 10
|
|
start_x, start_y = 470, 150
|
|
for row in range(4):
|
|
for col in range(5):
|
|
x = start_x + col * (slot_size + gap)
|
|
y = start_y + row * (slot_size + gap)
|
|
cv2.rectangle(img, (x, y), (x + slot_size, y + slot_size), (150, 150, 150), -1)
|
|
# Add small colored squares (items)
|
|
if (row + col) % 3 == 0:
|
|
color = (0, 100, 200) if row % 2 == 0 else (200, 100, 0)
|
|
cv2.rectangle(img, (x+5, y+5), (x+slot_size-5, y+slot_size-5), color, -1)
|
|
|
|
# Text-like regions (rectangles that could contain text)
|
|
# Chat/log area
|
|
cv2.rectangle(img, (20, 450), (400, 580), (240, 240, 240), -1)
|
|
cv2.rectangle(img, (20, 450), (400, 475), (180, 180, 180), -1)
|
|
|
|
# Status bars
|
|
cv2.rectangle(img, (20, 150), (200, 170), (200, 200, 200), -1)
|
|
cv2.rectangle(img, (20, 150), (150, 170), (0, 150, 0), -1) # Health bar
|
|
|
|
cv2.rectangle(img, (20, 180), (200, 200), (200, 200, 200), -1)
|
|
cv2.rectangle(img, (20, 180), (120, 200), (0, 100, 200), -1) # Energy bar
|
|
|
|
return img
|
|
|
|
|
|
def demo_hardware_detection():
|
|
"""Demo hardware detection."""
|
|
print("\n" + "=" * 60)
|
|
print("HARDWARE DETECTION DEMO")
|
|
print("=" * 60)
|
|
|
|
from modules.hardware_detection import (
|
|
print_hardware_summary,
|
|
recommend_ocr_backend,
|
|
HardwareDetector
|
|
)
|
|
|
|
# Print summary
|
|
print_hardware_summary()
|
|
|
|
# Get recommendation
|
|
recommended = recommend_ocr_backend()
|
|
print(f"\n📋 Recommended OCR backend: {recommended}")
|
|
|
|
# Check for Windows Store Python issues
|
|
info = HardwareDetector.detect_all()
|
|
if info.is_windows_store_python:
|
|
print("\n⚠️ Note: Windows Store Python detected")
|
|
print(" If you see DLL errors, use 'opencv_east' or 'tesseract' backend")
|
|
|
|
|
|
def demo_ocr_backends():
|
|
"""Demo OCR backends."""
|
|
print("\n" + "=" * 60)
|
|
print("OCR BACKEND DEMO")
|
|
print("=" * 60)
|
|
|
|
from modules.ocr_backends import OCRBackendFactory
|
|
|
|
# Create demo image
|
|
demo_img = create_demo_image()
|
|
|
|
# Save for reference
|
|
demo_path = Path.home() / ".lemontropia" / "demo_screenshot.png"
|
|
demo_path.parent.mkdir(parents=True, exist_ok=True)
|
|
cv2.imwrite(str(demo_path), demo_img)
|
|
print(f"\nDemo image saved to: {demo_path}")
|
|
|
|
# Check available backends
|
|
print("\nChecking OCR backends...")
|
|
backends = OCRBackendFactory.check_all_backends(use_gpu=True)
|
|
|
|
for backend_info in backends:
|
|
status = "✅" if backend_info.available else "❌"
|
|
gpu = "🚀 GPU" if backend_info.gpu_accelerated else "💻 CPU"
|
|
print(f"\n{status} {backend_info.name.upper()}")
|
|
print(f" Available: {backend_info.available}")
|
|
print(f" GPU: {gpu}")
|
|
|
|
if backend_info.error_message:
|
|
print(f" Error: {backend_info.error_message}")
|
|
|
|
# Test if available
|
|
if backend_info.available:
|
|
print(f" Testing...", end=" ")
|
|
try:
|
|
backend = OCRBackendFactory.create_backend(
|
|
backend_info.name, use_gpu=True
|
|
)
|
|
if backend:
|
|
regions = backend.extract_text(demo_img)
|
|
print(f"Detected {len(regions)} text regions ✓")
|
|
except Exception as e:
|
|
print(f"Error: {e}")
|
|
|
|
|
|
def demo_game_vision():
|
|
"""Demo GameVisionAI."""
|
|
print("\n" + "=" * 60)
|
|
print("GAME VISION AI DEMO")
|
|
print("=" * 60)
|
|
|
|
from modules.game_vision_ai import GameVisionAI
|
|
|
|
# Create demo image
|
|
demo_img = create_demo_image()
|
|
demo_path = Path.home() / ".lemontropia" / "demo_screenshot.png"
|
|
cv2.imwrite(str(demo_path), demo_img)
|
|
|
|
# Initialize
|
|
print("\nInitializing GameVisionAI...")
|
|
vision = GameVisionAI(use_gpu=True)
|
|
|
|
print(f"✅ Initialized!")
|
|
print(f" OCR Backend: {vision.ocr.get_current_backend()}")
|
|
print(f" GPU: {vision.backend.value}")
|
|
|
|
# Process screenshot
|
|
print(f"\nProcessing demo screenshot...")
|
|
result = vision.process_screenshot(demo_path)
|
|
|
|
print(f"✅ Processing complete!")
|
|
print(f" Processing time: {result.processing_time_ms:.1f}ms")
|
|
print(f" Text regions: {len(result.text_regions)}")
|
|
print(f" Icons detected: {len(result.icon_regions)}")
|
|
|
|
# Show detected regions
|
|
if result.text_regions:
|
|
print("\n Detected text regions:")
|
|
for i, region in enumerate(result.text_regions[:5]):
|
|
x, y, w, h = region.bbox
|
|
print(f" {i+1}. bbox=({x},{y},{w},{h}), conf={region.confidence:.2f}")
|
|
|
|
if result.icon_regions:
|
|
print("\n Detected icons:")
|
|
for i, icon in enumerate(result.icon_regions[:5]):
|
|
x, y, w, h = icon.bbox
|
|
print(f" {i+1}. bbox=({x},{y},{w},{h}), hash={icon.icon_hash[:16]}...")
|
|
|
|
|
|
def demo_backend_switching():
|
|
"""Demo switching between backends."""
|
|
print("\n" + "=" * 60)
|
|
print("BACKEND SWITCHING DEMO")
|
|
print("=" * 60)
|
|
|
|
from modules.game_vision_ai import UnifiedOCRProcessor
|
|
|
|
# Create processor
|
|
processor = UnifiedOCRProcessor()
|
|
print(f"\nDefault backend: {processor.get_current_backend()}")
|
|
|
|
# List all available backends
|
|
print("\nAvailable backends:")
|
|
for info in processor.get_available_backends():
|
|
status = "✅" if info.available else "❌"
|
|
print(f" {status} {info.name}")
|
|
|
|
# Try switching backends
|
|
demo_img = create_demo_image()
|
|
|
|
for backend_name in ['opencv_east', 'tesseract', 'easyocr', 'paddleocr']:
|
|
print(f"\nTrying to switch to '{backend_name}'...", end=" ")
|
|
success = processor.set_backend(backend_name)
|
|
|
|
if success:
|
|
print(f"✅ Success!")
|
|
regions = processor.extract_text(demo_img)
|
|
print(f" Detected {len(regions)} regions")
|
|
else:
|
|
print(f"❌ Not available")
|
|
|
|
|
|
def main():
|
|
"""Run demos."""
|
|
print("\n" + "=" * 60)
|
|
print("LEMONTROPIA SUITE - OCR SYSTEM DEMO")
|
|
print("=" * 60)
|
|
print("\nThis demo shows the multi-backend OCR system.")
|
|
print("The system automatically handles PyTorch DLL errors")
|
|
print("and falls back to working backends.")
|
|
|
|
try:
|
|
demo_hardware_detection()
|
|
except Exception as e:
|
|
print(f"\n❌ Hardware detection failed: {e}")
|
|
|
|
try:
|
|
demo_ocr_backends()
|
|
except Exception as e:
|
|
print(f"\n❌ OCR backend demo failed: {e}")
|
|
|
|
try:
|
|
demo_game_vision()
|
|
except Exception as e:
|
|
print(f"\n❌ Game Vision demo failed: {e}")
|
|
|
|
try:
|
|
demo_backend_switching()
|
|
except Exception as e:
|
|
print(f"\n❌ Backend switching demo failed: {e}")
|
|
|
|
print("\n" + "=" * 60)
|
|
print("DEMO COMPLETE")
|
|
print("=" * 60)
|
|
print("\nFor more information, see OCR_SETUP.md")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|