266 lines
8.8 KiB
Python
266 lines
8.8 KiB
Python
"""
|
|
Lemontropia Suite - Game Vision AI Example
|
|
Demonstrates usage of the Game Vision AI module.
|
|
"""
|
|
|
|
import sys
|
|
from pathlib import Path
|
|
import logging
|
|
|
|
# Setup logging
|
|
logging.basicConfig(level=logging.INFO, format='%(levelname)s: %(message)s')
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
def demo_gpu_detection():
|
|
"""Demonstrate GPU detection."""
|
|
print("\n" + "="*60)
|
|
print("GPU DETECTION DEMO")
|
|
print("="*60)
|
|
|
|
from modules.game_vision_ai import GPUDetector, GPUBackend
|
|
|
|
# Detect GPU
|
|
backend = GPUDetector.detect_backend()
|
|
print(f"\nDetected GPU Backend: {backend.value}")
|
|
|
|
# Get detailed info
|
|
info = GPUDetector.get_gpu_info()
|
|
print(f"\nGPU Details:")
|
|
print(f" Backend: {info['backend']}")
|
|
print(f" CUDA Available: {info['cuda_available']}")
|
|
print(f" MPS Available: {info['mps_available']}")
|
|
|
|
if info.get('devices'):
|
|
print(f"\n Devices:")
|
|
for dev in info['devices']:
|
|
mem_gb = dev.get('memory_total', 0) / (1024**3)
|
|
print(f" [{dev['id']}] {dev['name']} ({mem_gb:.1f} GB)")
|
|
|
|
print(f"\n PyTorch Device String: {GPUDetector.get_device_string(backend)}")
|
|
|
|
|
|
def demo_ocr(image_path: str = None):
|
|
"""Demonstrate OCR functionality."""
|
|
print("\n" + "="*60)
|
|
print("OCR TEXT EXTRACTION DEMO")
|
|
print("="*60)
|
|
|
|
from modules.game_vision_ai import OCRProcessor
|
|
|
|
# Initialize OCR
|
|
print("\nInitializing OCR (this may take a moment on first run)...")
|
|
ocr = OCRProcessor(use_gpu=True, lang='en')
|
|
|
|
if image_path and Path(image_path).exists():
|
|
print(f"\nProcessing: {image_path}")
|
|
regions = ocr.extract_text(image_path)
|
|
|
|
print(f"\nDetected {len(regions)} text regions:")
|
|
for i, region in enumerate(regions, 1):
|
|
print(f" {i}. '{region.text}' (confidence: {region.confidence:.2%})")
|
|
print(f" Position: ({region.bbox[0]}, {region.bbox[1]}) {region.bbox[2]}x{region.bbox[3]}")
|
|
else:
|
|
print(f"\nNo image provided or file not found: {image_path}")
|
|
print("Usage: python vision_example.py --ocr path/to/screenshot.png")
|
|
|
|
|
|
def demo_icon_detection(image_path: str = None):
|
|
"""Demonstrate icon detection."""
|
|
print("\n" + "="*60)
|
|
print("ICON DETECTION DEMO")
|
|
print("="*60)
|
|
|
|
from modules.game_vision_ai import IconDetector
|
|
import cv2
|
|
|
|
detector = IconDetector()
|
|
|
|
if image_path and Path(image_path).exists():
|
|
print(f"\nProcessing: {image_path}")
|
|
image = cv2.imread(image_path)
|
|
|
|
# Detect loot window
|
|
window = detector.detect_loot_window(image)
|
|
if window:
|
|
print(f"\nDetected loot window at: {window}")
|
|
|
|
# Extract icons
|
|
icons = detector.extract_icons_from_region(image, window)
|
|
print(f"\nExtracted {len(icons)} icons:")
|
|
|
|
for i, icon in enumerate(icons, 1):
|
|
print(f" {i}. Position: {icon.bbox}")
|
|
print(f" Hash: {icon.icon_hash[:32]}...")
|
|
else:
|
|
print("\nNo loot window detected. Trying full image...")
|
|
h, w = image.shape[:2]
|
|
icons = detector.extract_icons_from_region(image, (0, 0, w, h))
|
|
print(f"Found {len(icons)} potential icons in full image")
|
|
else:
|
|
print(f"\nNo image provided or file not found: {image_path}")
|
|
|
|
|
|
def demo_full_vision(image_path: str = None):
|
|
"""Demonstrate full vision processing."""
|
|
print("\n" + "="*60)
|
|
print("FULL VISION PROCESSING DEMO")
|
|
print("="*60)
|
|
|
|
from modules.game_vision_ai import GameVisionAI
|
|
|
|
# Initialize vision AI
|
|
print("\nInitializing Game Vision AI...")
|
|
vision = GameVisionAI(use_gpu=True, ocr_lang='en')
|
|
|
|
print(f"GPU Available: {vision.is_gpu_available()}")
|
|
print(f"Backend: {vision.backend.value}")
|
|
|
|
if image_path and Path(image_path).exists():
|
|
print(f"\nProcessing: {image_path}")
|
|
|
|
# Process screenshot
|
|
result = vision.process_screenshot(image_path)
|
|
|
|
print(f"\n--- Results ---")
|
|
print(f"Processing Time: {result.processing_time_ms:.1f}ms")
|
|
print(f"GPU Backend: {result.gpu_backend}")
|
|
|
|
print(f"\nText Regions ({len(result.text_regions)}):")
|
|
for region in result.text_regions:
|
|
print(f" • '{region.text}' ({region.confidence:.2%})")
|
|
|
|
print(f"\nIcon Regions ({len(result.icon_regions)}):")
|
|
for region in result.icon_regions:
|
|
print(f" • Position: {region.bbox}")
|
|
|
|
print(f"\nExtracted icons saved to: {vision.extracted_icons_dir}")
|
|
else:
|
|
print(f"\nNo image provided or file not found: {image_path}")
|
|
print("Usage: python vision_example.py --full path/to/screenshot.png")
|
|
|
|
|
|
def demo_icon_matching():
|
|
"""Demonstrate icon matching."""
|
|
print("\n" + "="*60)
|
|
print("ICON MATCHING DEMO")
|
|
print("="*60)
|
|
|
|
from modules.icon_matcher import IconMatcher, PerceptualHash
|
|
import cv2
|
|
import numpy as np
|
|
|
|
# Create matcher
|
|
matcher = IconMatcher()
|
|
|
|
print(f"\nIcon Database Stats:")
|
|
stats = matcher.get_database_stats()
|
|
print(f" Total Icons: {stats['total_icons']}")
|
|
print(f" Database Path: {stats['database_path']}")
|
|
|
|
# Demonstrate perceptual hashing
|
|
print(f"\nPerceptual Hashing:")
|
|
# Create a sample image
|
|
sample = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
|
|
|
|
avg_hash = PerceptualHash.average_hash(sample)
|
|
diff_hash = PerceptualHash.difference_hash(sample)
|
|
|
|
print(f" Average Hash: {avg_hash[:32]}...")
|
|
print(f" Difference Hash: {diff_hash[:32]}...")
|
|
|
|
# Show similarity calculation
|
|
similar = np.random.randint(0, 255, (64, 64, 3), dtype=np.uint8)
|
|
similar[20:40, 20:40] = sample[20:40, 20:40] # Make it somewhat similar
|
|
|
|
hash1 = PerceptualHash.average_hash(sample)
|
|
hash2 = PerceptualHash.average_hash(similar)
|
|
similarity = PerceptualHash.similarity(hash1, hash2)
|
|
|
|
print(f" Similarity between two images: {similarity:.2%}")
|
|
|
|
|
|
def demo_calibration():
|
|
"""Demonstrate calibration."""
|
|
print("\n" + "="*60)
|
|
print("CALIBRATION DEMO")
|
|
print("="*60)
|
|
|
|
from modules.game_vision_ai import GameVisionAI
|
|
|
|
vision = GameVisionAI(use_gpu=True)
|
|
|
|
print("\nTo calibrate, provide sample screenshots:")
|
|
print(" vision.calibrate_for_game([path1, path2, ...])")
|
|
print("\nThis will:")
|
|
print(" 1. Process each screenshot")
|
|
print(" 2. Measure detection accuracy")
|
|
print(" 3. Calculate average processing time")
|
|
print(" 4. Provide recommendations")
|
|
|
|
|
|
def main():
|
|
"""Main entry point."""
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(
|
|
description="Game Vision AI Examples",
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
epilog="""
|
|
Examples:
|
|
python vision_example.py --gpu # GPU detection demo
|
|
python vision_example.py --ocr image.png # OCR demo
|
|
python vision_example.py --icons image.png # Icon detection demo
|
|
python vision_example.py --full image.png # Full vision demo
|
|
python vision_example.py --matching # Icon matching demo
|
|
python vision_example.py --all # Run all demos
|
|
"""
|
|
)
|
|
|
|
parser.add_argument('--gpu', action='store_true', help='GPU detection demo')
|
|
parser.add_argument('--ocr', type=str, metavar='IMAGE', help='OCR demo with image')
|
|
parser.add_argument('--icons', type=str, metavar='IMAGE', help='Icon detection demo')
|
|
parser.add_argument('--full', type=str, metavar='IMAGE', help='Full vision demo')
|
|
parser.add_argument('--matching', action='store_true', help='Icon matching demo')
|
|
parser.add_argument('--calibration', action='store_true', help='Calibration demo')
|
|
parser.add_argument('--all', action='store_true', help='Run all demos')
|
|
|
|
args = parser.parse_args()
|
|
|
|
# If no args, show help
|
|
if not any([args.gpu, args.ocr, args.icons, args.full, args.matching, args.calibration, args.all]):
|
|
parser.print_help()
|
|
return
|
|
|
|
try:
|
|
if args.all or args.gpu:
|
|
demo_gpu_detection()
|
|
|
|
if args.all or args.ocr:
|
|
demo_ocr(args.ocr)
|
|
|
|
if args.all or args.icons:
|
|
demo_icon_detection(args.icons)
|
|
|
|
if args.all or args.full:
|
|
demo_full_vision(args.full)
|
|
|
|
if args.all or args.matching:
|
|
demo_icon_matching()
|
|
|
|
if args.all or args.calibration:
|
|
demo_calibration()
|
|
|
|
except ImportError as e:
|
|
print(f"\n❌ Import Error: {e}")
|
|
print("\nMake sure all dependencies are installed:")
|
|
print(" pip install -r requirements.txt")
|
|
except Exception as e:
|
|
print(f"\n❌ Error: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|