""" Test the new return_image feature """ import requests import json import base64 from pathlib import Path API_URL = "http://localhost:8000/api/verify" def test_with_annotated_image(): """Test verification with annotated image""" print("="*60) print("Testing Certificate Verification with Annotated Image") print("="*60) # Use one of the seal images test_file = "cropped_seals/temp_cert_264196_seal_1.png" if not Path(test_file).exists(): print(f"โŒ Test file not found: {test_file}") return print(f"\n๐Ÿ“„ Testing with: {test_file}") print(f"๐Ÿ“Š Testing WITH annotated image (return_image=true)\n") try: with open(test_file, 'rb') as f: files = {'files': (Path(test_file).name, f, 'image/png')} # Add return_image=true parameter params = {'return_image': 'true'} response = requests.post(API_URL, files=files, params=params) print(f"โœ… Status: {response.status_code}") result = response.json() # Display main results print(f"\n๐Ÿ“Š Verification Results:") print(f" Decision: {result.get('decision')}") print(f" Confidence: {result.get('confidence')}") print(f" Reason: {result.get('reason')}") print(f" Processing Time: {result.get('processing_time_seconds')}s") # Check if annotated image is present if 'annotated_image' in result: print(f"\n๐ŸŽจ Annotated Image:") print(f" โœ… Base64 image included") print(f" Size: {len(result['annotated_image'])} characters") # Save annotated image to file img_data = base64.b64decode(result['annotated_image']) output_path = "annotated_certificate.png" with open(output_path, 'wb') as f: f.write(img_data) print(f" ๐Ÿ’พ Saved to: {output_path}") if 'annotated_image_url' in result: print(f" ๐ŸŒ Data URL available (for direct display in browser)") print(f" URL length: {len(result['annotated_image_url'])} characters") else: print(f"\nโš ๏ธ No annotated image in response") # Show seal detection details seal_info = result.get('details', {}).get('seal_verification', {}) if seal_info: print(f"\n๐Ÿ” Seal Detection:") print(f" Total seals: {seal_info.get('total_seals', 0)}") print(f" Authentic: {seal_info.get('authentic_seals', 0)}") print(f" Fake: {seal_info.get('fake_seals', 0)}") print(f" Method: {seal_info.get('detection_method', 'N/A')}") return True except Exception as e: print(f"โŒ Error: {e}") import traceback traceback.print_exc() return False def test_without_annotated_image(): """Test verification without annotated image (default behavior)""" print("\n" + "="*60) print("Testing WITHOUT Annotated Image (return_image=false)") print("="*60) test_file = "cropped_seals/temp_cert_264196_seal_1.png" print(f"\n๐Ÿ“„ Testing with: {test_file}") print(f"๐Ÿ“Š Testing WITHOUT annotated image (default)\n") try: with open(test_file, 'rb') as f: files = {'files': (Path(test_file).name, f, 'image/png')} # Don't specify return_image parameter (defaults to false) response = requests.post(API_URL, files=files) result = response.json() print(f"โœ… Status: {response.status_code}") print(f" Decision: {result.get('decision')}") print(f" Time: {result.get('processing_time_seconds')}s") if 'annotated_image' in result: print(f" โš ๏ธ Annotated image included (unexpected)") else: print(f" โœ… No annotated image (as expected)") return True except Exception as e: print(f"โŒ Error: {e}") return False if __name__ == "__main__": print("\n๐Ÿงช ANNOTATED IMAGE API TESTING\n") # Wait for API import time print("โณ Waiting for API to start...") time.sleep(5) # Check API health try: response = requests.get("http://localhost:8000/health", timeout=5) print(f"โœ… API is running\n") except: print(f"โŒ API not running. Start it first!") exit(1) # Run tests test1 = test_with_annotated_image() test2 = test_without_annotated_image() print("\n" + "="*60) if test1 and test2: print("โœ… ALL TESTS PASSED!") print("\n๐Ÿ’ก Open 'annotated_certificate.png' to see the result!") else: print("โŒ SOME TESTS FAILED") print("="*60)